Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
korikuzma committed Jan 27, 2025
1 parent df10969 commit 704602a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Initialize the tests namespace for shared utilities."""
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import pytest
from deepdiff import DeepDiff
from ga4gh.core.models import ConceptMapping

from metakb.harvesters.base import Harvester
from metakb.normalizers import ViccNormalizers
from metakb.query import QueryHandler
from metakb.transformers.base import NORMALIZER_PRIORITY_EXT_NAME

TEST_DATA_DIR = Path(__file__).resolve().parents[0] / "data"
TEST_HARVESTERS_DIR = TEST_DATA_DIR / "harvesters"
Expand Down Expand Up @@ -64,6 +66,24 @@ def get_vicc_normalizer_ext(is_priority: bool):
return [{"name": "vicc_normalizer_priority", "value": is_priority}]


def get_mappings_normalizer_id(mappings: list[dict | ConceptMapping]) -> str | None:
"""Get normalizer ID from list of concept mappings
:param mappings: List of concept mappings
:return: Normalizer ID
"""
normalizer_id = None
for mapping in mappings:
if isinstance(mapping, ConceptMapping):
mapping = mapping.model_dump()
extensions = mapping.get("extensions") or []
for ext in extensions:
if ext["name"] == NORMALIZER_PRIORITY_EXT_NAME and ext["value"]:
normalizer_id = mapping["coding"]["code"]
break
return normalizer_id


@pytest.fixture(scope="session")
def braf_normalizer_mappings():
"""Create test fixture for braf normalizer mappings"""
Expand Down
9 changes: 3 additions & 6 deletions tests/unit/database/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import pytest
from neo4j import Driver
from neo4j.graph import Node
from tests.conftest import get_mappings_normalizer_id

from metakb.database import get_driver
from metakb.schemas.app import SourceName
from metakb.transformers.base import NORMALIZER_PRIORITY_EXT_NAME


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -184,11 +184,8 @@ def _check_function(
if k == "mappings" or (k == "subtype" and isinstance(fixture[k], dict)):
assert json.loads(node[k]) == fixture[k]
elif k == "normalizer_id":
for mapping in fixture["mappings"]:
extensions = mapping.get("extensions") or []
for ext in extensions:
if ext["name"] == NORMALIZER_PRIORITY_EXT_NAME and ext["value"]:
assert node[k] == mapping["coding"]["code"]
normalizer_id = get_mappings_normalizer_id(fixture["mappings"])
assert node[k] == normalizer_id
elif isinstance(fixture[k], list):
assert set(node[k]) == set(fixture[k])
else:
Expand Down
31 changes: 12 additions & 19 deletions tests/unit/search/test_search_statements.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
"""Test search statement methods"""

import pytest
from ga4gh.core.models import MappableConcept
from tests.conftest import get_mappings_normalizer_id

from metakb.query import QueryHandler

from .utils import assert_no_match, find_and_check_stmt


def _get_normalizer_id(mappings: list[MappableConcept]) -> str | None:
"""Get normalized ID from list of extensions
:param mappings: List of mappable concepts
:return: Normalized concept ID if found in extensions
"""
normalizer_id = None
for mapping in mappings:
if mapping.extensions == "from_vicc_normalizer":
normalizer_id = mapping.code.root
break
return normalizer_id


def assert_general_search_stmts(response):
"""Check that general search_statements queries return a valid response"""
len_stmt_id_matches = len(response.statement_ids)
Expand Down Expand Up @@ -216,11 +202,14 @@ async def test_general_search_statements(query_handler):
tp = statement.proposition.objectTherapeutic.root

if hasattr(tp, "conceptType"):
assert _get_normalizer_id(tp.mappings) == expected_therapy_id
assert get_mappings_normalizer_id(tp.mappings) == expected_therapy_id
else:
found_expected = False
for therapeutic in tp.therapies:
if _get_normalizer_id(therapeutic.mappings) == expected_therapy_id:
if (
get_mappings_normalizer_id(therapeutic.mappings)
== expected_therapy_id
):
found_expected = True
break
assert found_expected
Expand All @@ -245,11 +234,15 @@ async def test_general_search_statements(query_handler):
== expected_variation_id
)
assert (
_get_normalizer_id(statement.proposition.objectTherapeutic.root.extensions)
get_mappings_normalizer_id(
statement.proposition.objectTherapeutic.root.mappings
)
== expected_therapy_id
)
assert (
_get_normalizer_id(statement.proposition.conditionQualifier.root.extensions)
get_mappings_normalizer_id(
statement.proposition.conditionQualifier.root.mappings
)
== expected_disease_id
)

Expand Down

0 comments on commit 704602a

Please sign in to comment.