Skip to content

Commit

Permalink
fix tests after model update with version and model version, and exte…
Browse files Browse the repository at this point in the history
…nd forbidding extra fields to nested nodes
  • Loading branch information
sherwoodf committed Jul 31, 2024
1 parent e02f6a7 commit 708f2d2
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
],
"intrinsic_variable_description": [
"Test intrinsic variable 1\\nwith escaped character"
]
],
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
],
"intrinsic_variable_description": [
"Test intrinsic variable 2"
]
],
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
],
"submitted_in_study_uuid": "a2fdbd58-ee11-4cd9-bc6a-f3d3da7fff71",
"correlation_method": [],
"example_image_uri": []
"example_image_uri": [],
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"protocol_description": "Test image acquisition parameters 1",
"imaging_instrument_description": "Test imaging instrument 1",
"fbbi_id": [],
"imaging_method_name": "confocal microscopy"
"imaging_method_name": "confocal microscopy",
"version": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"title_id": "Test specimen 1",
"uuid": "7199d730-29f1-4ad8-b599-e9089cbb2d7b",
"protocol_description": "Test sample preparation protocol 1",
"signal_channel_information": []
"signal_channel_information": [],
"version": 1
}
5 changes: 5 additions & 0 deletions bia-export/test/input_data/studies/S-BIADTEST.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"uuid": "a2fdbd58-ee11-4cd9-bc6a-f3d3da7fff71",
"version": 1,
"model": {
"type_name": "Study",
"version": 1
},
"accession_id": "S-BIADTEST",
"licence": "CC0",
"author": [
Expand Down
10 changes: 10 additions & 0 deletions bia-export/test/output_data/bia_export.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"uuid": "a2fdbd58-ee11-4cd9-bc6a-f3d3da7fff71",
"version": 1,
"model": {
"type_name": "Study",
"version": 1
},
"accession_id": "S-BIADTEST",
"licence": "CC0",
"author": [
Expand Down Expand Up @@ -75,6 +80,11 @@
{
"title_id": "Study Component 1",
"uuid": "47a4ab60-c76d-4424-bfaa-c2a024de720c",
"version": 1,
"model": {
"type_name": "ExperimentalImagingDataset",
"version": 1
},
"description": "Description of study component 1",
"analysis_method": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def extract_annotation_method_dicts(submission: Submission) -> List[Dict[str, An
model_dict["accno"] = section.__dict__.get("accno", "")
model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_annotation_method_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.AnnotationMethod)

model_dicts.append(model_dict)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def extract_biosample_dicts(submission: Submission) -> List[Dict[str, Any]]:

model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_biosample_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.BioSample)
model_dicts.append(model_dict)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def get_experimental_imaging_dataset(
"analysis_method": analysis_method_list,
"correlation_method": correlation_method_list,
"example_image_uri": [],
"version": 1
}
model_dict["uuid"] = generate_experimental_imaging_dataset_uuid(model_dict)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def get_file_reference_by_study_component(
"accession_id": submission.accno,
"file_path": str(f.path),
"size_in_bytes": str(f.size),
"version": 1
}
fileref_uuid = dict_to_uuid(
file_dict, ["accession_id", "file_path", "size_in_bytes"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def extract_image_acquisition_dicts(submission: Submission) -> List[Dict[str, An
model_dict["accno"] = section.__dict__.get("accno", "")
model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_image_acquisition_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.ImageAcquisition)
model_dicts.append(model_dict)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def extract_specimen_growth_protocol_dicts(submission: Submission) -> List[Dict[
model_dict["accno"] = section.__dict__.get("accno", "")
model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_specimen_growth_protocol_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.SpecimenGrowthProtocol)

model_dicts.append(model_dict)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def extract_specimen_preparation_protocol_dicts(submission: Submission) -> List[
model_dict["accno"] = section.__dict__.get("accno", "")
model_dict["accession_id"] = submission.accno
model_dict["uuid"] = generate_specimen_imaging_preparation_uuid(model_dict)
model_dict["version"] = 1
model_dict = filter_model_dictionary(model_dict, bia_data_model.SpecimenImagingPrepartionProtocol)

model_dicts.append(model_dict)
Expand Down
1 change: 1 addition & 0 deletions bia-ingest-shared-models/bia_ingest_sm/conversion/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def get_study(
"author": [c.model_dump() for c in contributors],
"grant": [g.model_dump() for g in grants],
"attribute": study_attributes,
"version": 1
}
# study_uuid = dict_to_uuid(study_dict, ["accession_id",])
# study_dict["uuid"] = study_uuid
Expand Down
2 changes: 1 addition & 1 deletion bia-ingest-shared-models/bia_ingest_sm/conversion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,5 @@ def persist(object_list: List, object_path: str, sumbission_accno: str):

def filter_model_dictionary(dictionary: dict, target_model: Type[BaseModel]):
accepted_fields = target_model.model_fields.keys()
result_dict = {key: dictionary[key] for key in accepted_fields}
result_dict = {key: dictionary[key] for key in accepted_fields if key in dictionary}
return result_dict
15 changes: 15 additions & 0 deletions bia-ingest-shared-models/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def get_test_annotation_method() -> List[bia_data_model.AnnotationMethod]:
"annotation_coverage": "",
"method_type": "other",
"source_dataset": [],
"version": 1,
},
]

Expand All @@ -58,12 +59,14 @@ def get_test_specimen_growth_protocol() -> List[bia_data_model.SpecimenGrowthPro
"accession_id": "S-BIADTEST",
"title_id": "Test specimen 1",
"protocol_description": "Test growth protocol 1",
"version": 1,
},
{
"accno": "Specimen-2",
"accession_id": "S-BIADTEST",
"title_id": "Test specimen 2",
"protocol_description": "Test growth protocol 2",
"version": 1,
},
]

Expand Down Expand Up @@ -94,13 +97,15 @@ def get_test_specimen_imaging_preparation_protocol() -> (
"title_id": "Test specimen 1",
"protocol_description": "Test sample preparation protocol 1",
"signal_channel_information": [],
"version": 1,
},
{
"accno": "Specimen-2",
"accession_id": "S-BIADTEST",
"title_id": "Test specimen 2",
"protocol_description": "Test sample preparation protocol 2",
"signal_channel_information": [],
"version": 1,
},
]

Expand Down Expand Up @@ -158,6 +163,7 @@ def get_test_biosample() -> List[bia_data_model.BioSample]:
"intrinsic_variable_description": [
"Test intrinsic variable 1\nwith escaped character",
],
"version": 1,
},
{
"accno": "Biosample-2",
Expand All @@ -176,6 +182,7 @@ def get_test_biosample() -> List[bia_data_model.BioSample]:
"intrinsic_variable_description": [
"Test intrinsic variable 2",
],
"version": 1,
},
]

Expand Down Expand Up @@ -206,6 +213,7 @@ def get_test_image_acquisition() -> List[bia_data_model.ImageAcquisition]:
"imaging_instrument_description": "Test imaging instrument 1",
"imaging_method_name": "confocal microscopy",
"fbbi_id": [],
"version": 1,
},
{
"accno": "Image acquisition-7",
Expand All @@ -215,6 +223,7 @@ def get_test_image_acquisition() -> List[bia_data_model.ImageAcquisition]:
"imaging_instrument_description": "Test imaging instrument 2",
"imaging_method_name": "fluorescence microscopy",
"fbbi_id": [],
"version": 1,
},
]
image_acquisition = []
Expand Down Expand Up @@ -272,6 +281,7 @@ def get_test_experimental_imaging_dataset() -> (
],
"example_image_uri": [],
"description": "Description of study component 1",
"version": 1,
}
experimental_imaging_dataset_uuid = dict_to_uuid(
experimental_imaging_dataset_dict,
Expand All @@ -294,16 +304,19 @@ def get_test_experimental_imaging_dataset() -> (
"accession_id": "S-BIADTEST",
"file_path": "study_component2/im06.png",
"size_in_bytes": 3,
"version": 1,
},
{
"accession_id": "S-BIADTEST",
"file_path": "study_component2/im08.png",
"size_in_bytes": 123,
"version": 1,
},
{
"accession_id": "S-BIADTEST",
"file_path": "study_component2/ann01-05",
"size_in_bytes": 11,
"version": 1,
},
]
experimental_imaging_dataset_dict = {
Expand All @@ -317,6 +330,7 @@ def get_test_experimental_imaging_dataset() -> (
],
"example_image_uri": [],
"description": "Description of study component 2",
"version": 1,
}
experimental_imaging_dataset_uuid = dict_to_uuid(
experimental_imaging_dataset_dict,
Expand Down Expand Up @@ -495,6 +509,7 @@ def get_test_study() -> bia_data_model.Study:
"Test keyword3",
],
"grant": [g.model_dump() for g in grant],
"version": 1,
}
study_uuid = dict_to_uuid(
study_dict,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class DocumentMixin(BaseModel):
Optional because for some usecases (e.g. api) we want to accept objects without it because we have the info we need to set it."""
)

# Throw error if you try to validate/create model from a dictionary with keys that aren't a field in the model
model_config = ConfigDict(extra="forbid")

def __init__(self, *args, **data):
model_version_spec = self.model_config.get("model_version_latest")
if model_version_spec is None:
Expand All @@ -41,6 +38,7 @@ def __init__(self, *args, **data):
)
model_metadata_existing = data.get("model", None)
if model_metadata_existing:
model_metadata_existing = ModelMetadata(**model_metadata_existing)
if model_metadata_existing != model_metadata_expected:
raise exceptions.UnexpectedDocumentType(
f"Document {str(data.get('uuid'))} has model metadata {model_metadata_existing}, expected : {model_metadata_expected}"
Expand Down
Loading

0 comments on commit 708f2d2

Please sign in to comment.