-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
321 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,94 @@ | ||
from uuid import uuid4 | ||
import pytest | ||
from pydantic import ValidationError | ||
from pydantic import ValidationError, BaseModel | ||
from . import utils | ||
from bia_shared_datamodels import bia_data_model | ||
|
||
from bia_shared_datamodels import bia_data_model, semantic_models | ||
from typing import Callable | ||
|
||
@pytest.mark.parametrize( | ||
("expected_model_type", "dict_creation_func"), | ||
( | ||
"expected_model_type", | ||
"model_creation_func", | ||
), | ||
( | ||
(semantic_models.Taxon, utils.get_taxon_dict), | ||
(semantic_models.Channel, utils.get_channel_dict), | ||
(semantic_models.RenderedView, utils.get_rendered_view_dict), | ||
( | ||
bia_data_model.FileReference, | ||
utils.get_template_file_reference, | ||
semantic_models.SignalChannelInformation, | ||
utils.get_signal_channel_information_dict, | ||
), | ||
( | ||
bia_data_model.ImageRepresentation, | ||
utils.get_template_image_representation, | ||
), | ||
( | ||
bia_data_model.ExperimentalImagingDataset, | ||
utils.get_template_experimental_imaging_dataset, | ||
bia_data_model.SpecimenImagingPrepartionProtocol, | ||
utils.get_specimen_imaging_preparation_protocol_dict, | ||
), | ||
( | ||
bia_data_model.Specimen, | ||
utils.get_template_specimen, | ||
bia_data_model.SpecimenGrowthProtocol, | ||
utils.get_specimen_growth_protocol_dict, | ||
), | ||
(bia_data_model.BioSample, utils.get_biosample_dict), | ||
(bia_data_model.Specimen, utils.get_specimen_dict), | ||
(bia_data_model.AnnotationMethod, utils.get_annotation_method_dict), | ||
( | ||
bia_data_model.ExperimentallyCapturedImage, | ||
utils.get_template_experimentally_captured_image, | ||
), | ||
( | ||
bia_data_model.ImageAcquisition, | ||
utils.get_template_image_acquisition, | ||
), | ||
( | ||
bia_data_model.SpecimenImagingPrepartionProtocol, | ||
utils.get_template_specimen_imaging_preparation_protocol, | ||
), | ||
( | ||
bia_data_model.BioSample, | ||
utils.get_template_biosample, | ||
utils.get_experimentally_captured_image_dict, | ||
), | ||
(bia_data_model.DerivedImage, utils.get_derived_image_dict), | ||
( | ||
bia_data_model.ImageAnnotationDataset, | ||
utils.get_template_image_annotation_dataset, | ||
utils.get_image_annotation_dataset_dict, | ||
), | ||
(bia_data_model.ImageAcquisition, utils.get_image_acquisition_dict), | ||
(semantic_models.ImageAnalysisMethod, utils.get_image_analysis_method_dict), | ||
( | ||
bia_data_model.AnnotationFileReference, | ||
utils.get_template_annotation_file_reference, | ||
semantic_models.ImageCorrelationMethod, | ||
utils.get_image_correlation_method_dict, | ||
), | ||
( | ||
bia_data_model.DerivedImage, | ||
utils.get_template_derived_image, | ||
), | ||
( | ||
bia_data_model.AnnotationMethod, | ||
utils.get_template_annotation_method, | ||
bia_data_model.ExperimentalImagingDataset, | ||
utils.get_experimental_imaging_dataset_dict, | ||
), | ||
( | ||
bia_data_model.SpecimenGrowthProtocol, | ||
utils.get_template_specimen_growth_protocol, | ||
bia_data_model.AnnotationFileReference, | ||
utils.get_annotation_file_reference_dict, | ||
), | ||
(bia_data_model.FileReference, utils.get_file_reference_dict), | ||
(bia_data_model.ImageRepresentation, utils.get_image_representation_dict), | ||
(semantic_models.Affiliation, utils.get_affiliation_dict), | ||
(semantic_models.Contributor, utils.get_contributor_dict), | ||
(bia_data_model.Study, utils.get_study_dict), | ||
), | ||
) | ||
def test_create_models(expected_model_type, model_creation_func): | ||
expected_model = model_creation_func() | ||
assert type(expected_model) is expected_model_type | ||
|
||
|
||
def test_create_study(): | ||
def test_create_study( | ||
expected_model_type: BaseModel, | ||
dict_creation_func: Callable[[utils.Completeness], dict], | ||
): | ||
|
||
complete_dict = utils.get_study_dict(utils.Completeness.COMPLETE) | ||
study_complete = bia_data_model.Study(**complete_dict) | ||
complete_dict = dict_creation_func(utils.Completeness.COMPLETE) | ||
complete_model: BaseModel = expected_model_type(**complete_dict) | ||
|
||
# Check that the model is created | ||
assert type(study_complete) is bia_data_model.Study | ||
assert type(complete_model) is expected_model_type | ||
# Check that the dictionary is indeed "Complete" - no optional fields missed | ||
assert study_complete.model_dump() == complete_dict | ||
assert complete_model.model_dump() == complete_dict | ||
# Check that there are no inconsistencies in the model definition | ||
assert ( | ||
type(bia_data_model.Study(**study_complete.model_dump())) | ||
is bia_data_model.Study | ||
type(expected_model_type(**complete_model.model_dump())) is expected_model_type | ||
) | ||
|
||
mimimal_dict = utils.get_study_dict(utils.Completeness.MINIMAL) | ||
study_minimal = bia_data_model.Study(**mimimal_dict) | ||
mimimal_dict = dict_creation_func(utils.Completeness.MINIMAL) | ||
minimal_model: BaseModel = expected_model_type(**mimimal_dict) | ||
|
||
# Check that the model is created | ||
assert type(study_minimal) is bia_data_model.Study | ||
# Check that the dictionary is indeed "Minimal" - no optional fields included | ||
assert type(minimal_model) is expected_model_type | ||
# Check that the dictionary is indeed "Minimal" - no optional fields included, and list fields are of minimum length | ||
for key in mimimal_dict: | ||
less_than_minimal_dict = mimimal_dict.copy() | ||
if isinstance(less_than_minimal_dict[key], list) and len(less_than_minimal_dict[key]) > 0: | ||
less_than_minimal_dict[key] = [] | ||
with pytest.raises(ValidationError): | ||
expected_model_type(**less_than_minimal_dict) | ||
|
||
del less_than_minimal_dict[key] | ||
with pytest.raises(ValidationError): | ||
bia_data_model.Study(**less_than_minimal_dict) | ||
expected_model_type(**less_than_minimal_dict) | ||
# Check that there are no inconsistencies in the model definition's optional fields | ||
assert ( | ||
type(bia_data_model.Study(**study_minimal.model_dump())) is bia_data_model.Study | ||
type(expected_model_type(**minimal_model.model_dump())) is expected_model_type | ||
) | ||
|
||
|
||
def test_create_specimen_with_empty_lists_fails(): | ||
with pytest.raises(ValidationError): | ||
specimen = bia_data_model.Specimen.model_validate( | ||
{ | ||
"sample_of": [], | ||
"preparation_method": [], | ||
} | ||
) | ||
specimen = bia_data_model.Specimen.model_validate( | ||
{ | ||
"sample_of": [uuid4()], | ||
"preparation_method": [], | ||
} | ||
) | ||
specimen = bia_data_model.Specimen.model_validate( | ||
{ | ||
"sample_of": [], | ||
"preparation_method": [uuid4()], | ||
} | ||
) |
Oops, something went wrong.