Skip to content

Commit

Permalink
minor improvements to code readability (#110)
Browse files Browse the repository at this point in the history
* minor improvements to code readability

* pass in uuids as fixture
  • Loading branch information
sherwoodf authored Jun 10, 2024
1 parent 944ca5b commit 1e9a3a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 68 deletions.
27 changes: 14 additions & 13 deletions api/src/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
AsyncIOMotorDatabase,
)
import pydantic
from typing import List, Any, Callable, Type, Union
from typing import List, Any, Type, Union
from enum import Enum
import uuid
import pymongo
Expand Down Expand Up @@ -535,8 +535,7 @@ async def validate_object_dependency(
if isinstance(getattr(doc_to_verify, field), uuid.UUID):
uuid_to_check = getattr(doc_to_verify, field)
await self.validate_uuid_type(uuid_to_check, doc_type.__name__)

if isinstance(getattr(doc_to_verify, field), List):
elif isinstance(getattr(doc_to_verify, field), List):
for uuid_to_check in getattr(doc_to_verify, field):
await self.validate_uuid_type(uuid_to_check, doc_type.__name__)

Expand Down Expand Up @@ -607,34 +606,36 @@ async def validate_uuid_type(self, target_uuid: uuid.UUID, expected_type_name: s

return

@staticmethod
def append_dependency(
self,
dependency_map: dict,
dependency_map_collector: dict,
dependency: Union[uuid.UUID, List[uuid.UUID]],
index: int,
expected_type: Type[models.DocumentMixin],
):
if isinstance(dependency, uuid.UUID):
if dependency_map.get(dependency, None) is None:
dependency_map[dependency] = {
if dependency_map_collector.get(dependency, None) is None:
dependency_map_collector[dependency] = {
"index": [index],
"type": {expected_type.__name__},
}
else:
dependency_map[dependency]["index"].append(index)
dependency_map_collector[dependency]["index"].append(index)
# We expect a single unique doc type but will sort out throwing an error below
dependency_map[dependency]["type"].add(expected_type.__name__)
dependency_map_collector[dependency]["type"].add(expected_type.__name__)
elif isinstance(dependency, List):
for dependency_uuid in dependency:
if dependency_map.get(dependency_uuid, None) is None:
dependency_map[dependency_uuid] = {
if dependency_map_collector.get(dependency_uuid, None) is None:
dependency_map_collector[dependency_uuid] = {
"index": [index],
"type": {expected_type.__name__},
}
else:
dependency_map[dependency_uuid]["index"].append(index)
dependency_map_collector[dependency_uuid]["index"].append(index)
# We expect a single unique doc type but will sort out throwing an error below
dependency_map[dependency_uuid]["type"].add(expected_type.__name__)
dependency_map_collector[dependency_uuid]["type"].add(
expected_type.__name__
)
return


Expand Down
68 changes: 13 additions & 55 deletions api/src/tests/test_biosample_specimen_image_acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from fastapi.testclient import TestClient
from .util import (
get_template_biosample,
get_template_specimen,
get_template_image_acquisition,
)
import pytest

Expand Down Expand Up @@ -36,20 +38,9 @@ def test_biosample_create_retrieve_update(api_client: TestClient, uuid: str):


def test_specimen_create_retrieve_update(
api_client: TestClient, uuid: str, existing_biosample: dict
api_client: TestClient, existing_biosample: dict, uuid: str
):
specimen = {
"uuid": uuid,
"version": 0,
"biosample_uuid": existing_biosample["uuid"],
"title": "placeholder_title",
"sample_preparation_protocol": "placeholder_sample_preparation_protocol",
"growth_protocol": "placeholder_growth_protocol",
"attributes": {},
"annotations": [],
"annotations_applied": False,
"@context": "https://placeholder/context",
}
specimen = get_template_specimen(existing_biosample) | {"uuid": uuid}
rsp = api_client.post(f"private/specimens", json=specimen)
assert rsp.status_code == 201, rsp.json()

Expand All @@ -70,28 +61,18 @@ def test_specimen_create_retrieve_update(


def test_image_acquisition_create_retrieve_update(
api_client: TestClient, uuid: str, existing_specimen: dict
api_client: TestClient, existing_specimen: dict, uuid: str
):
image_acquisition = {
"uuid": uuid,
"version": 0,
"specimen_uuid": existing_specimen["uuid"],
"title": "placeholder_title",
"imaging_instrument": "placeholder_imaging_instrument",
"image_acquisition_parameters": "placeholder_image_acquisition_parameters",
"imaging_method": "placeholder_imaging_method",
"attributes": {},
"annotations": [],
"annotations_applied": False,
"@context": "https://placeholder/context",
image_acquisition = get_template_image_acquisition(existing_specimen) | {
"uuid": uuid
}
rsp = api_client.post(f"private/image_acquisitions", json=image_acquisition)
assert rsp.status_code == 201, rsp.json()

rsp = api_client.get(f"image_acquisitions/{uuid}")
specimen_fetched = rsp.json()
del specimen_fetched["model"]
assert specimen_fetched == image_acquisition
image_acquisition_fetched = rsp.json()
del image_acquisition_fetched["model"]
assert image_acquisition_fetched == image_acquisition

image_acquisition["title"] = "title_updated"
image_acquisition["version"] += 1
Expand All @@ -106,24 +87,12 @@ def test_image_acquisition_create_retrieve_update(

def test_create_update_with_badly_typed_uuid(
api_client: TestClient,
uuid: str,
existing_specimen: dict,
existing_image_acquisition: dict,
existing_study: dict,
uuid: str,
):
image_acquisition = {
"uuid": uuid,
"version": 0,
"specimen_uuid": existing_study["uuid"],
"title": "placeholder_title",
"imaging_instrument": "placeholder_imaging_instrument",
"image_acquisition_parameters": "placeholder_image_acquisition_parameters",
"imaging_method": "placeholder_imaging_method",
"attributes": {},
"annotations": [],
"annotations_applied": False,
"@context": "https://placeholder/context",
}
image_acquisition = get_template_image_acquisition(existing_study) | {"uuid": uuid}
rsp = api_client.post(f"private/image_acquisitions", json=image_acquisition)
assert rsp.status_code == 400, rsp.json()

Expand All @@ -134,18 +103,7 @@ def test_create_update_with_badly_typed_uuid(
)
assert rsp.status_code == 400, rsp.json()

specimen = {
"uuid": uuid,
"version": 0,
"biosample_uuid": existing_study["uuid"],
"title": "placeholder_title",
"sample_preparation_protocol": "placeholder_sample_preparation_protocol",
"growth_protocol": "placeholder_growth_protocol",
"attributes": {},
"annotations": [],
"annotations_applied": False,
"@context": "https://placeholder/context",
}
specimen = get_template_specimen(existing_study, add_uuid=True)
rsp = api_client.post(f"private/specimens", json=specimen)
assert rsp.status_code == 400, rsp.json()

Expand Down

0 comments on commit 1e9a3a4

Please sign in to comment.