Skip to content

Commit

Permalink
Merge pull request #333 from AllenNeuralDynamics/release-v0.19.1
Browse files Browse the repository at this point in the history
release-v0.19.1
  • Loading branch information
jtyoung84 authored Jan 30, 2025
2 parents 1dd50c9 + f9477a1 commit 4dc48eb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/aind_metadata_service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""REST service to retrieve metadata from databases."""

__version__ = "0.19.0"
__version__ = "0.19.1"
25 changes: 19 additions & 6 deletions src/aind_metadata_service/tars/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ def get_virus_strains(response: ModelResponse) -> List:
viruses.extend(virus_strains)
return viruses

# TODO: Refactor this method
@staticmethod
def integrate_injection_materials(
def integrate_injection_materials( # noqa: C901
response: ModelResponse, tars_mapping: Dict[str, JSONResponse]
) -> ModelResponse:
"""
Expand Down Expand Up @@ -262,7 +263,8 @@ def integrate_injection_materials(
virus_strain = injection_material.name.strip()
tars_response = tars_mapping.get(virus_strain)
if (
tars_response.status_code
tars_response
and tars_response.status_code
== StatusCodes.DB_RESPONDED.value
or tars_response.status_code
== StatusCodes.VALID_DATA.value
Expand All @@ -272,10 +274,21 @@ def integrate_injection_materials(
data = json.loads(tars_response.body)[
"data"
]
new_material = ViralMaterial(**data)
new_material.titer = (
injection_material.titer
)
try:
new_material = ViralMaterial(**data)
new_material.titer = (
injection_material.titer
)
except ValidationError as e:
logging.error(f"{e}")
new_material = (
ViralMaterial.model_construct(
**data
)
)
new_material.titer = (
injection_material.titer
)
procedure.injection_materials[idx] = (
new_material
)
Expand Down
73 changes: 72 additions & 1 deletion tests/tars/test_mapping.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Module to test TARS mapping."""

import unittest
from copy import deepcopy
from datetime import date
from unittest.mock import MagicMock, patch

Expand Down Expand Up @@ -365,7 +366,7 @@ def test_integrate_injection_materials(self):
"12345": tars_response1.map_to_json_response(),
"67890": tars_response2.map_to_json_response(),
}
procedures_response = self.procedures_response
procedures_response = deepcopy(self.procedures_response)
merged_response = self.handler.integrate_injection_materials(
response=procedures_response, tars_mapping=tars_mapping
)
Expand Down Expand Up @@ -487,6 +488,76 @@ def test_integrate_injection_materials_no_data(self):
)
self.assertEqual(merged_response.status_code, StatusCodes.DB_RESPONDED)

@patch("logging.error")
def test_integrate_injection_materials_no_name(
self, mock_error: MagicMock
):
"""Tests that injection materials are integrated into
procedures response as expected and invalid viral material"""
expected_injection_material = ViralMaterial.model_construct(
name="rAAV-MGT_789",
tars_identifiers=TarsVirusIdentifiers.model_construct(
virus_tars_id="AiV456",
plasmid_tars_alias="AiP123",
prep_lot_number="12345",
prep_date=date(2023, 12, 15),
prep_type=VirusPrepType.CRUDE.value,
prep_protocol="SOP#VC002",
),
)
expected_injection_material2 = ViralMaterial.model_construct(
tars_identifiers=TarsVirusIdentifiers.model_construct(
virus_tars_id="AiV456",
plasmid_tars_alias="AiP123",
prep_lot_number="12345",
prep_date=date(2023, 12, 15),
prep_type=VirusPrepType.CRUDE.value,
prep_protocol="SOP#VC002",
),
)
tars_response1 = ModelResponse(
aind_models=[expected_injection_material],
status_code=StatusCodes.DB_RESPONDED,
)
tars_response2 = ModelResponse(
aind_models=[expected_injection_material2],
status_code=StatusCodes.DB_RESPONDED,
)
tars_mapping = {
"12345": tars_response1.map_to_json_response(),
"67890": tars_response2.map_to_json_response(),
}
procedures_response = self.procedures_response
merged_response = self.handler.integrate_injection_materials(
response=procedures_response, tars_mapping=tars_mapping
)
expected_surgery = Surgery.model_construct(
procedures=[
NanojectInjection.model_construct(
injection_materials=[expected_injection_material]
),
NanojectInjection.model_construct(
injection_materials=[expected_injection_material2]
),
]
)
expected_merged_response = ModelResponse(
aind_models=[
Procedures(
subject_id="12345",
subject_procedures=[expected_surgery],
)
],
status_code=StatusCodes.DB_RESPONDED,
)
self.assertEqual(
expected_merged_response.aind_models[0].model_dump_json(
warnings=False
),
merged_response.aind_models[0].model_dump_json(warnings=False),
)
mock_error.assert_called_once()


if __name__ == "__main__":
unittest.main()

0 comments on commit 4dc48eb

Please sign in to comment.