test_suite.py 3.36 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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33


def test_gcis_client_version():
    assert True


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)

34
35
36
    assert len(f.contributors) == 2
    assert all([isinstance(cont, Contributor) for cont in f.contributors])

37
38
39
40
41
42
43
44

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, '')

45
    #Make sure fields specifically omitted are actually omitted
46
47
48
49
50
51
52
53
54
55
56
    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']])

57
58
59
60
61
62
63
64
65
66
67
    #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

68

69
70
71
72
73
def test_chapter_parsing():
    webform_fig = Figure(json.loads(webform_json_precip))
    gcis_fig = Figure(json.loads(test_figure_json))

    assert isinstance(webform_fig.ordinal, int)
74
    assert webform_fig.figure_num == '2.17'
75
    assert webform_fig.ordinal == 17
76
    assert webform_fig.chapter == 2
77
78
79
80
81

    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)

82
83
    assert isinstance(gcis_fig.chapter, Chapter)

84
85
86
87
88
    assert merged_figure.figure_num == '2.17'
    assert merged_figure.ordinal == 17
    assert isinstance(merged_figure.chapter, Chapter)
    assert merged_figure.chapter.number == 2

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

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

105
106
107
108
109
    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'
110
111


112
113
114
115
116
117
# def test_contributors():
#     f = Figure(json.loads(test_figure_json))
#     contribs = f.contributors



118
119
if __name__ == "__main__" and __package__ is None:
    __package__ = "gcis_client.test.test_suite"
120
    # test_contributors()