diff --git a/tests/conftest.py b/tests/conftest.py index e2a5639..369e19f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,34 @@ """Provide utilities for test cases.""" +from enum import Enum from pathlib import Path SUBMODULES_DIR = Path(__file__).parents[1] / "submodules" / "va_spec" + + +class VaSpecSchema(str, Enum): + """Enum for VA-Spec schema""" + + AAC_2017 = "aac-2017" + ACMG_2015 = "acmg-2015" + BASE = "base" + CCV_2022 = "ccv-2022" + + +def get_va_spec_schema(label: str) -> str | None: + """Get VA-Spec schema given label + + :param label: Label + :return: VA-Spec label + """ + if label.endswith(VaSpecSchema.AAC_2017): + schema = VaSpecSchema.AAC_2017 + elif label.endswith(VaSpecSchema.ACMG_2015): + schema = VaSpecSchema.ACMG_2015 + elif label.endswith(VaSpecSchema.BASE): + schema = VaSpecSchema.BASE + elif label.endswith(VaSpecSchema.CCV_2022): + schema = VaSpecSchema.CCV_2022 + else: + schema = None + return schema diff --git a/tests/validation/test_va_spec_fixtures_validation.py b/tests/validation/test_va_spec_fixtures_validation.py new file mode 100644 index 0000000..0c94451 --- /dev/null +++ b/tests/validation/test_va_spec_fixtures_validation.py @@ -0,0 +1,42 @@ +"""Ensure that VA-Spec test fixtures validate against Pydantic models""" + +import yaml +from ga4gh.va_spec import aac_2017, acmg_2015, base, ccv_2022 + +from tests.conftest import SUBMODULES_DIR, VaSpecSchema, get_va_spec_schema + +VA_SPEC_TESTS_DIR = SUBMODULES_DIR / "tests" + + +with (VA_SPEC_TESTS_DIR / "test_definitions.yaml").open() as f: + data = yaml.load(f, Loader=yaml.SafeLoader) + test_definitions = data["tests"] + +SCHEMA_TO_PYDANTIC_MODULE = { + VaSpecSchema.AAC_2017: aac_2017, + VaSpecSchema.ACMG_2015: acmg_2015, + VaSpecSchema.CCV_2022: ccv_2022, + VaSpecSchema.BASE: base, +} +VA_SPEC_TEST_DEFINITIONS = {schema: [] for schema in VaSpecSchema} + + +for test_def in test_definitions: + if test_def["namespace"].startswith("va-spec."): + schema = get_va_spec_schema(test_def["namespace"].split("va-spec.")[-1]) + VA_SPEC_TEST_DEFINITIONS[schema].append(test_def) + + +def test_va_spec_fixtures(): + """Test that VA-Spec test fixtures validate against Pydantic models""" + for va_spec_schema, schema_test_defs in VA_SPEC_TEST_DEFINITIONS.items(): + pydantic_module = SCHEMA_TO_PYDANTIC_MODULE[va_spec_schema] + + for schema_test_def in schema_test_defs: + with ( + VA_SPEC_TESTS_DIR / "fixtures" / schema_test_def["test_file"] + ).open() as f: + test_fixture_dict = yaml.load(f, Loader=yaml.SafeLoader) + va_spec_class = schema_test_def["definition"] + pydantic_model = getattr(pydantic_module, va_spec_class) + assert pydantic_model(**test_fixture_dict) diff --git a/tests/validation/test_va_spec_schema.py b/tests/validation/test_va_spec_schema.py index 94777cb..192f737 100644 --- a/tests/validation/test_va_spec_schema.py +++ b/tests/validation/test_va_spec_schema.py @@ -1,27 +1,21 @@ """Test that VA-Spec Python Pydantic models match corresponding JSON schemas""" import json -from enum import Enum from pathlib import Path import pytest from ga4gh.va_spec import aac_2017, acmg_2015, base, ccv_2022 from pydantic import BaseModel -from tests.conftest import SUBMODULES_DIR +from tests.conftest import ( + SUBMODULES_DIR, + VaSpecSchema, + get_va_spec_schema, +) VA_SCHEMA_DIR = SUBMODULES_DIR / "schema" / "va-spec" -class VaSpecSchema(str, Enum): - """Enum for VA-Spec schema""" - - AAC_2017 = "aac-2017" - ACMG_2015 = "acmg-2015" - BASE = "base" - CCV_2022 = "ccv-2022" - - class VaSpecSchemaMapping(BaseModel): """Model for representing VA-Spec Schema concrete classes, primitives, and schema""" @@ -59,15 +53,8 @@ def _update_va_spec_schema_mapping( # Get core + profiles classes for child in VA_SCHEMA_DIR.iterdir(): child_str = str(child) - if child_str.endswith(VaSpecSchema.AAC_2017): - mapping_key = VaSpecSchema.AAC_2017 - elif child_str.endswith(VaSpecSchema.ACMG_2015): - mapping_key = VaSpecSchema.ACMG_2015 - elif child_str.endswith(VaSpecSchema.BASE): - mapping_key = VaSpecSchema.BASE - elif child_str.endswith(VaSpecSchema.CCV_2022): - mapping_key = VaSpecSchema.CCV_2022 - else: + mapping_key = get_va_spec_schema(child_str) + if not mapping_key: continue mapping = VA_SPEC_SCHEMA_MAPPING[mapping_key]