Skip to content

Commit 25ceb9e

Browse files
committed
[#82] updating tests for ResourcePreview model
1 parent de7c3ac commit 25ceb9e

File tree

1 file changed

+77
-33
lines changed

1 file changed

+77
-33
lines changed

tests/test_json_models.py

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,83 @@ def test_null_subject_areas():
2828
assert o.subject_areas == []
2929

3030

31-
# AuthorsFieldResourcePreviewTestData = (
32-
# json.dumps({"authors": None}),
33-
# json.dumps({"authors": []}),
34-
# json.dumps({"authors": [None]}),
35-
# json.dumps({"authors": [""]}),
36-
# json.dumps({"authors": [[]]}),
37-
# )
38-
#
39-
#
40-
# @pytest.mark.parametrize("test_data", AuthorsFieldResourcePreviewTestData)
41-
# def test_resource_preview_authors_field_handles_none_cases(test_data):
42-
# """verify all `authors` fields are instantiated with [] values.
43-
#
44-
# coerced `authors` field should be [] with following input:
45-
# None
46-
# []
47-
# [None]
48-
# [None, ""]
49-
# """
50-
#
51-
# from_json = ResourcePreview.model_validate_json(test_data)
52-
#
53-
# assert from_json.authors == []
54-
#
55-
#
56-
# def test_resource_preview_authors_raises_validation_error_on_string_input():
57-
# """verify that a string passed to authors field raises pydantic.ValidationError"""
58-
# from pydantic import ValidationError
59-
#
60-
# data = json.dumps({"authors": "should_fail"})
61-
#
62-
# with pytest.raises(ValidationError):
63-
# ResourcePreview.model_validate_json(data)
31+
def generate_resource_preview_test_data(authors_field):
32+
data = ResourcePreviewTestRequiredData.copy()
33+
data.update(authors_field)
34+
data = json.dumps(data)
35+
return data
36+
37+
38+
ResourcePreviewTestRequiredData = {
39+
"resource_type": "CompositeResource",
40+
"resource_title": "Test Resource",
41+
"resource_id": "97523bdb7b174901b3fc2d89813458f1",
42+
"creator": "John Doe",
43+
"date_created": "2021-01-01T00:00:00.000Z",
44+
"date_last_updated": "2021-01-01T00:00:00.000Z",
45+
"public": True,
46+
"discoverable": True,
47+
"shareable": True,
48+
"immutable": False,
49+
"published": False,
50+
"resource_url": "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/",
51+
"resource_map_url": "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/map/",
52+
"science_metadata_url": "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/science-metadata/",
53+
}
54+
55+
authors = [{"authors": x} for x in [None, [], [None], [""], [[]]]]
56+
AuthorsFieldResourcePreviewTestData = map(generate_resource_preview_test_data, authors)
57+
58+
59+
@pytest.mark.parametrize("test_data", AuthorsFieldResourcePreviewTestData)
60+
def test_resource_preview_authors_field_handles_none_cases(test_data):
61+
"""verify all `authors` fields are instantiated with [] values.
62+
63+
coerced `authors` field should be [] with following input:
64+
None
65+
[]
66+
[None]
67+
[None, ""]
68+
"""
69+
70+
from_json = ResourcePreview.model_validate_json(test_data)
71+
72+
assert from_json.authors == []
73+
74+
75+
def test_resource_preview_authors_raises_validation_error_on_string_input():
76+
"""verify that a string passed to authors field raises pydantic.ValidationError"""
77+
from pydantic import ValidationError
78+
79+
data = ResourcePreviewTestRequiredData.copy()
80+
data.update({"authors": "should_fail"})
81+
data = json.dumps(data)
82+
83+
with pytest.raises(ValidationError):
84+
ResourcePreview.model_validate_json(data)
85+
86+
87+
def test_resource_preview_required_fields():
88+
resource_preview = ResourcePreview.model_validate_json(json.dumps(ResourcePreviewTestRequiredData))
89+
assert resource_preview.resource_type == "CompositeResource"
90+
assert resource_preview.resource_title == "Test Resource"
91+
assert resource_preview.resource_id == "97523bdb7b174901b3fc2d89813458f1"
92+
assert resource_preview.creator == "John Doe"
93+
assert resource_preview.date_created == "2021-01-01T00:00:00.000Z"
94+
assert resource_preview.date_last_updated == "2021-01-01T00:00:00.000Z"
95+
assert resource_preview.public
96+
assert resource_preview.discoverable
97+
assert resource_preview.shareable
98+
assert not resource_preview.immutable
99+
assert not resource_preview.published
100+
assert resource_preview.resource_url == "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/"
101+
assert resource_preview.resource_map_url == "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/map/"
102+
assert resource_preview.science_metadata_url == "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/science-metadata/"
103+
# check the optional fields
104+
assert resource_preview.abstract is None
105+
assert resource_preview.authors == []
106+
assert resource_preview.doi is None
107+
assert resource_preview.coverages == []
64108

65109

66110
def test_user_info(user):

0 commit comments

Comments
 (0)