Skip to content

Commit

Permalink
[#82] updating tests for ResourcePreview model
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdash committed Nov 22, 2024
1 parent de7c3ac commit 25ceb9e
Showing 1 changed file with 77 additions and 33 deletions.
110 changes: 77 additions & 33 deletions tests/test_json_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,83 @@ 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": [[]]}),
# )
#
#
# @pytest.mark.parametrize("test_data", AuthorsFieldResourcePreviewTestData)
# def test_resource_preview_authors_field_handles_none_cases(test_data):
# """verify all `authors` fields are instantiated with [] values.
#
# coerced `authors` field should be [] with following input:
# None
# []
# [None]
# [None, ""]
# """
#
# from_json = ResourcePreview.model_validate_json(test_data)
#
# assert from_json.authors == []
#
#
# 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"})
#
# with pytest.raises(ValidationError):
# ResourcePreview.model_validate_json(data)
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)
def test_resource_preview_authors_field_handles_none_cases(test_data):
"""verify all `authors` fields are instantiated with [] values.
coerced `authors` field should be [] with following input:
None
[]
[None]
[None, ""]
"""

from_json = ResourcePreview.model_validate_json(test_data)

assert from_json.authors == []


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 = 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):
Expand Down

0 comments on commit 25ceb9e

Please sign in to comment.