Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#82] Fix resource preview model #74

Merged
merged 7 commits into from
Nov 26, 2024
43 changes: 25 additions & 18 deletions hsclient/json_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Dict, List, Tuple, Optional
from typing import Dict, List, Tuple, Optional, Union, Any

from hsmodels.schemas.enums import UserIdentifierType
from pydantic import AnyUrl, BaseModel, HttpUrl, field_validator
Expand Down Expand Up @@ -34,24 +34,24 @@ def handle_empty_website(cls, v):


class ResourcePreview(BaseModel):
resource_type: str = None
resource_title: str = None
resource_id: str = None
abstract: str = None
resource_type: str
resource_title: str
resource_id: str
abstract: Optional[str] = None
authors: List[str] = []
creator: str = None
doi: str = None
date_created: str = None
date_last_updated: str = None
public: bool = None
discoverable: bool = None
shareable: bool = None
coverages: Dict[str, str] = None
immutable: bool = None
published: bool = None
resource_url: str = None
resource_map_url: str = None
resource_metadata_url: str = None
creator: str
doi: Optional[str] = None
date_created: str
date_last_updated: str
public: bool
discoverable: bool
shareable: bool
coverages: Union[List[Dict[str, Any]], List] = []
devincowan marked this conversation as resolved.
Show resolved Hide resolved
immutable: bool
published: bool
resource_url: str
resource_map_url: str
science_metadata_url: str

@field_validator("authors", mode='before')
def handle_null_author(cls, v):
Expand All @@ -64,3 +64,10 @@ def handle_null_author(cls, v):

# filter to remove all empty x's in v ("", None, or equivalent)
return list(filter(lambda x: x, v))

@field_validator("coverages", mode='before')
def handle_null_coverages(cls, v):
# return empty list when supplied coverages field is None.
if v is None:
return []
return v
8 changes: 4 additions & 4 deletions hsclient/oauth2_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
class Token(BaseModel):
access_token: str
token_type: str
scope: Optional[str]
state: Optional[str]
expires_in: Optional[int]
refresh_token: Optional[str]
scope: Optional[str] = None
state: Optional[str] = None
expires_in: Optional[int] = None
refresh_token: Optional[str] = None

class Config:
# do not allow extra fields
Expand Down
60 changes: 52 additions & 8 deletions tests/test_json_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 == "castronova.anthony@gmail.com"
Expand Down
Loading