@@ -28,13 +28,32 @@ def test_null_subject_areas():
28
28
assert o .subject_areas == []
29
29
30
30
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
- )
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 )
38
57
39
58
40
59
@pytest .mark .parametrize ("test_data" , AuthorsFieldResourcePreviewTestData )
@@ -57,12 +76,37 @@ def test_resource_preview_authors_raises_validation_error_on_string_input():
57
76
"""verify that a string passed to authors field raises pydantic.ValidationError"""
58
77
from pydantic import ValidationError
59
78
60
- data = json .dumps ({"authors" : "should_fail" })
79
+ data = ResourcePreviewTestRequiredData .copy ()
80
+ data .update ({"authors" : "should_fail" })
81
+ data = json .dumps (data )
61
82
62
83
with pytest .raises (ValidationError ):
63
84
ResourcePreview .model_validate_json (data )
64
85
65
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 == []
108
+
109
+
66
110
def test_user_info (user ):
67
111
assert user .name == "Castronova, Anthony M."
68
112
assert user .
email == "[email protected] "
0 commit comments