-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from hydroshare/fix-ResourcePreview-model
Fix resource preview model
- Loading branch information
Showing
3 changed files
with
81 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,32 @@ def test_null_subject_areas(): | |
assert o.subject_areas == [] | ||
|
||
|
||
AuthorsFieldResourcePreviewTestData = ( | ||
json.dumps({"authors": None}), | ||
json.dumps({"authors": []}), | ||
json.dumps({"authors": [None]}), | ||
json.dumps({"authors": [""]}), | ||
json.dumps({"authors": [[]]}), | ||
) | ||
def generate_resource_preview_test_data(authors_field): | ||
data = ResourcePreviewTestRequiredData.copy() | ||
data.update(authors_field) | ||
data = json.dumps(data) | ||
return data | ||
|
||
|
||
ResourcePreviewTestRequiredData = { | ||
"resource_type": "CompositeResource", | ||
"resource_title": "Test Resource", | ||
"resource_id": "97523bdb7b174901b3fc2d89813458f1", | ||
"creator": "John Doe", | ||
"date_created": "2021-01-01T00:00:00.000Z", | ||
"date_last_updated": "2021-01-01T00:00:00.000Z", | ||
"public": True, | ||
"discoverable": True, | ||
"shareable": True, | ||
"immutable": False, | ||
"published": False, | ||
"resource_url": "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/", | ||
"resource_map_url": "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/map/", | ||
"science_metadata_url": "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/science-metadata/", | ||
} | ||
|
||
authors = [{"authors": x} for x in [None, [], [None], [""], [[]]]] | ||
AuthorsFieldResourcePreviewTestData = map(generate_resource_preview_test_data, authors) | ||
|
||
|
||
@pytest.mark.parametrize("test_data", AuthorsFieldResourcePreviewTestData) | ||
|
@@ -57,12 +76,37 @@ def test_resource_preview_authors_raises_validation_error_on_string_input(): | |
"""verify that a string passed to authors field raises pydantic.ValidationError""" | ||
from pydantic import ValidationError | ||
|
||
data = json.dumps({"authors": "should_fail"}) | ||
data = ResourcePreviewTestRequiredData.copy() | ||
data.update({"authors": "should_fail"}) | ||
data = json.dumps(data) | ||
|
||
with pytest.raises(ValidationError): | ||
ResourcePreview.model_validate_json(data) | ||
|
||
|
||
def test_resource_preview_required_fields(): | ||
resource_preview = ResourcePreview.model_validate_json(json.dumps(ResourcePreviewTestRequiredData)) | ||
assert resource_preview.resource_type == "CompositeResource" | ||
assert resource_preview.resource_title == "Test Resource" | ||
assert resource_preview.resource_id == "97523bdb7b174901b3fc2d89813458f1" | ||
assert resource_preview.creator == "John Doe" | ||
assert resource_preview.date_created == "2021-01-01T00:00:00.000Z" | ||
assert resource_preview.date_last_updated == "2021-01-01T00:00:00.000Z" | ||
assert resource_preview.public | ||
assert resource_preview.discoverable | ||
assert resource_preview.shareable | ||
assert not resource_preview.immutable | ||
assert not resource_preview.published | ||
assert resource_preview.resource_url == "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/" | ||
assert resource_preview.resource_map_url == "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/map/" | ||
assert resource_preview.science_metadata_url == "http://beta.hydroshare.org/resource/97523bdb7b174901b3fc2d89813458f1/science-metadata/" | ||
# check the optional fields | ||
assert resource_preview.abstract is None | ||
assert resource_preview.authors == [] | ||
assert resource_preview.doi is None | ||
assert resource_preview.coverages == [] | ||
|
||
|
||
def test_user_info(user): | ||
assert user.name == "Castronova, Anthony M." | ||
assert user.email == "[email protected]" | ||
|