test_suite.py 6.27 KB
Newer Older
1
2
3
4
5
6
7
__author__ = 'abuddenberg'

#To run the tests:
# py.test -v test_suite.py


import json
8
9
10
11

import pytest

from test_data import test_figure_json, test_image_json, webform_json_precip, test_dataset_json
12
from gcis_clients.domain import Gcisbase, Figure, Image, Dataset, Chapter, Contributor, Parent
13
14
15
16
from gcis_clients import GcisClient
import __builtin__
from os import getenv
import gcis_clients
17
import gcis_clients.webform_transforms as trans
18
19
20
21
22
23


def test_gcis_client_version():
    assert True


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def test_gcis_client_init_modes(monkeypatch, capsys):
    test_user = 'test.user@domain.com'
    test_apikey = 'testkey'

    monkeypatch.setattr(__builtin__, 'raw_input', lambda u: test_user)
    monkeypatch.setattr('getpass.getpass', lambda k: test_apikey)

    #Test no args
    gcis = GcisClient()

    out, err = capsys.readouterr()
    assert gcis.base_url == 'http://data.globalchange.gov'
    assert gcis.s.auth == (test_user, test_apikey)

    #Test one arg (url) mode
    gcis = GcisClient('http://data.gcis-dev-front.joss.ucar.edu')
    assert gcis.base_url == 'http://data.gcis-dev-front.joss.ucar.edu'
    assert gcis.s.auth == (test_user, test_apikey)

    #Test invalid mode
    gcis = GcisClient('http://data.globalchange.gov', 'garbage')
    assert gcis.base_url == 'http://data.globalchange.gov'

    #assert garbage was ignored
    assert gcis.s.auth == (test_user, test_apikey)

    #Test all three args mode
    gcis = GcisClient('http://data.globalchange.gov', test_user, test_apikey)
    assert gcis.base_url == 'http://data.globalchange.gov'
    assert gcis.s.auth == (test_user, test_apikey)

55
56
57
58
    #Test trailing slash in base URL
    gcis = GcisClient('http://data.globalchange.gov/', test_user, test_apikey)
    assert gcis.base_url == 'http://data.globalchange.gov'

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

def test_get_credentials_modes(monkeypatch):
    test_user = 'test.user@domain.com'
    test_apikey = 'testkey'

    monkeypatch.setattr(__builtin__, 'raw_input', lambda u: test_user)
    monkeypatch.setattr('getpass.getpass', lambda k: test_apikey)

    #Test environment variable mode
    monkeypatch.setenv('GCIS_USER', 'env.user@domain.com')
    monkeypatch.setenv('GCIS_KEY', 'env.apikey')
    assert getenv('GCIS_USER') == 'env.user@domain.com'

    #Force __init__ to reread mock env variables
    reload(gcis_clients)

    gcis = GcisClient('http://data.globalchange.gov')
    assert gcis.base_url == 'http://data.globalchange.gov'
    assert gcis.s.auth == ('env.user@domain.com', 'env.apikey')


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def test_domain():
    f = Figure(json.loads(test_figure_json))

    assert isinstance(f, Gcisbase)
    assert isinstance(f, Figure)

    assert len(f.images) == 11
    assert all([isinstance(i, Image) for i in f.images])

    assert isinstance(f.chapter, Chapter)
    assert f.chapter.identifier == 'our-changing-climate'

    i = Image(json.loads(test_image_json))
    assert isinstance(i, Image)

95
96
97
    assert len(f.contributors) == 2
    assert all([isinstance(cont, Contributor) for cont in f.contributors])

98
99
100
    empty_img = Image({})
    assert empty_img.identifier is None

101
102
103
104
105
106
107
108

def test_domain_as_json():
    f = Figure(json.loads(test_figure_json))

    assert f.original['chapter']['identifier'] == 'our-changing-climate'
    assert f.original['images'] not in (None, '')
    assert f.original['uri'] not in (None, '')

109
    #Make sure fields specifically omitted are actually omitted
110
111
112
113
114
115
116
117
118
119
120
    fig_json_out = json.loads(f.as_json())
    assert all([omitted_key not in fig_json_out for omitted_key in ['chapter', 'images', 'uri', 'href']])

    i = Image(json.loads(test_image_json))

    assert i.original['uri'] not in (None, '')
    assert i.original['href'] not in (None, '')

    img_json_out = json.loads(i.as_json())
    assert all([omitted_key not in img_json_out for omitted_key in ['uri', 'href']])

121
122
123
124
125
126
127
128
129
130
131
    #Make sure merges work
    f2_json = json.loads(test_figure_json)
    f2_json['caption'] = ''
    f2 = Figure(f2_json)

    assert f2.caption in ('', None)

    f2.merge(f)

    assert f2.caption == f.caption

132

133
def test_chapter_parsing():
134
    webform_fig = Figure(json.loads(webform_json_precip), trans=trans.FIG_TRANSLATIONS)
135
136
137
    gcis_fig = Figure(json.loads(test_figure_json))

    assert isinstance(webform_fig.ordinal, int)
138
    assert webform_fig.figure_num == '2.17'
139
    assert webform_fig.ordinal == 17
140
    assert webform_fig.chapter == 2
141
142
143
144
145

    merged_figure = webform_fig.merge(gcis_fig)
    #FYI, these are identical; I just wanted the variable name to reflect the merge
    assert id(merged_figure) == id(webform_fig)

146
147
    assert isinstance(gcis_fig.chapter, Chapter)

148
149
150
151
152
    assert merged_figure.figure_num == '2.17'
    assert merged_figure.ordinal == 17
    assert isinstance(merged_figure.chapter, Chapter)
    assert merged_figure.chapter.number == 2

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

def test_dataset_special_properties():
    ds = Dataset(json.loads(test_dataset_json))

    assert isinstance(ds, Dataset)

    assert ds.release_dt == '2014-01-01T00:00:00'
    with pytest.raises(ValueError):
        ds.release_dt = '2014-01-01T99:00:00'

    assert ds.publication_year == '2014'
    ds.publication_year = '2014 (projected)'
    assert ds.publication_year == '2014'
    ds.publication_year = 'TBD'
    assert ds.publication_year is None

169
170
171
172
173
    assert ds.access_dt == '2011-12-31T00:00:00'

    #This one was subtle. Make sure as_json() still works with properties.
    ds_json_out = json.loads(ds.as_json())
    assert ds_json_out['access_dt'] == '2011-12-31T00:00:00'
174

175
176
177
    #Make sure identifier doesn't get nulled out if we don't specify known_ids
    assert ds.identifier == 'cddv2'

178

179
180
181
182
183
# def test_contributors():
#     f = Figure(json.loads(test_figure_json))
#     contribs = f.contributors


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def test_parent():
    f = Figure(json.loads(test_figure_json))
    assert f.parents == []

    ds = Dataset(json.loads(test_dataset_json))
    assert isinstance(ds, Dataset)

    p = Parent.from_obj(ds)
    assert isinstance(p, Parent)
    assert isinstance(p.publication, Dataset)

    f.add_parent(p)
    assert len(f.parents) == 1
    assert f.parents[0] == p
    assert f.parents[0].relationship == 'prov:wasDerivedFrom'
    assert f.parents[0].publication_type_identifier == 'dataset'
    assert f.parents[0].url == '/dataset/cddv2'
    assert f.parents[0].label == 'Climate Division Database Version 2'


204

205
206
if __name__ == "__main__" and __package__ is None:
    __package__ = "gcis_client.test.test_suite"
207
    # test_get_credentials_modes()