Skip to content

Commit 33729ac

Browse files
authored
test: add tests for validating va-spec test fixtures against pydantic models (#36)
close #33
1 parent 0ca7f3c commit 33729ac

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
"""Provide utilities for test cases."""
22

3+
from enum import Enum
34
from pathlib import Path
45

56
SUBMODULES_DIR = Path(__file__).parents[1] / "submodules" / "va_spec"
7+
8+
9+
class VaSpecSchema(str, Enum):
10+
"""Enum for VA-Spec schema"""
11+
12+
AAC_2017 = "aac-2017"
13+
ACMG_2015 = "acmg-2015"
14+
BASE = "base"
15+
CCV_2022 = "ccv-2022"
16+
17+
18+
def get_va_spec_schema(label: str) -> str | None:
19+
"""Get VA-Spec schema given label
20+
21+
:param label: Label
22+
:return: VA-Spec label
23+
"""
24+
if label.endswith(VaSpecSchema.AAC_2017):
25+
schema = VaSpecSchema.AAC_2017
26+
elif label.endswith(VaSpecSchema.ACMG_2015):
27+
schema = VaSpecSchema.ACMG_2015
28+
elif label.endswith(VaSpecSchema.BASE):
29+
schema = VaSpecSchema.BASE
30+
elif label.endswith(VaSpecSchema.CCV_2022):
31+
schema = VaSpecSchema.CCV_2022
32+
else:
33+
schema = None
34+
return schema
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Ensure that VA-Spec test fixtures validate against Pydantic models"""
2+
3+
import yaml
4+
from ga4gh.va_spec import aac_2017, acmg_2015, base, ccv_2022
5+
6+
from tests.conftest import SUBMODULES_DIR, VaSpecSchema, get_va_spec_schema
7+
8+
VA_SPEC_TESTS_DIR = SUBMODULES_DIR / "tests"
9+
10+
11+
with (VA_SPEC_TESTS_DIR / "test_definitions.yaml").open() as f:
12+
data = yaml.load(f, Loader=yaml.SafeLoader)
13+
test_definitions = data["tests"]
14+
15+
SCHEMA_TO_PYDANTIC_MODULE = {
16+
VaSpecSchema.AAC_2017: aac_2017,
17+
VaSpecSchema.ACMG_2015: acmg_2015,
18+
VaSpecSchema.CCV_2022: ccv_2022,
19+
VaSpecSchema.BASE: base,
20+
}
21+
VA_SPEC_TEST_DEFINITIONS = {schema: [] for schema in VaSpecSchema}
22+
23+
24+
for test_def in test_definitions:
25+
if test_def["namespace"].startswith("va-spec."):
26+
schema = get_va_spec_schema(test_def["namespace"].split("va-spec.")[-1])
27+
VA_SPEC_TEST_DEFINITIONS[schema].append(test_def)
28+
29+
30+
def test_va_spec_fixtures():
31+
"""Test that VA-Spec test fixtures validate against Pydantic models"""
32+
for va_spec_schema, schema_test_defs in VA_SPEC_TEST_DEFINITIONS.items():
33+
pydantic_module = SCHEMA_TO_PYDANTIC_MODULE[va_spec_schema]
34+
35+
for schema_test_def in schema_test_defs:
36+
with (
37+
VA_SPEC_TESTS_DIR / "fixtures" / schema_test_def["test_file"]
38+
).open() as f:
39+
test_fixture_dict = yaml.load(f, Loader=yaml.SafeLoader)
40+
va_spec_class = schema_test_def["definition"]
41+
pydantic_model = getattr(pydantic_module, va_spec_class)
42+
assert pydantic_model(**test_fixture_dict)

tests/validation/test_va_spec_schema.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
"""Test that VA-Spec Python Pydantic models match corresponding JSON schemas"""
22

33
import json
4-
from enum import Enum
54
from pathlib import Path
65

76
import pytest
87
from ga4gh.va_spec import aac_2017, acmg_2015, base, ccv_2022
98
from pydantic import BaseModel
109

11-
from tests.conftest import SUBMODULES_DIR
10+
from tests.conftest import (
11+
SUBMODULES_DIR,
12+
VaSpecSchema,
13+
get_va_spec_schema,
14+
)
1215

1316
VA_SCHEMA_DIR = SUBMODULES_DIR / "schema" / "va-spec"
1417

1518

16-
class VaSpecSchema(str, Enum):
17-
"""Enum for VA-Spec schema"""
18-
19-
AAC_2017 = "aac-2017"
20-
ACMG_2015 = "acmg-2015"
21-
BASE = "base"
22-
CCV_2022 = "ccv-2022"
23-
24-
2519
class VaSpecSchemaMapping(BaseModel):
2620
"""Model for representing VA-Spec Schema concrete classes, primitives, and schema"""
2721

@@ -59,15 +53,8 @@ def _update_va_spec_schema_mapping(
5953
# Get core + profiles classes
6054
for child in VA_SCHEMA_DIR.iterdir():
6155
child_str = str(child)
62-
if child_str.endswith(VaSpecSchema.AAC_2017):
63-
mapping_key = VaSpecSchema.AAC_2017
64-
elif child_str.endswith(VaSpecSchema.ACMG_2015):
65-
mapping_key = VaSpecSchema.ACMG_2015
66-
elif child_str.endswith(VaSpecSchema.BASE):
67-
mapping_key = VaSpecSchema.BASE
68-
elif child_str.endswith(VaSpecSchema.CCV_2022):
69-
mapping_key = VaSpecSchema.CCV_2022
70-
else:
56+
mapping_key = get_va_spec_schema(child_str)
57+
if not mapping_key:
7158
continue
7259

7360
mapping = VA_SPEC_SCHEMA_MAPPING[mapping_key]

0 commit comments

Comments
 (0)