Skip to content

Commit

Permalink
Validate ORM relationships in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Spayralbe committed Dec 19, 2024
1 parent 6072df3 commit 5f6a02a
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 10 deletions.
18 changes: 16 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from contextlib import contextmanager
from typing import Set
from typing import Callable, Set

import pytest
from sqlalchemy import Connection, Engine, MetaData, create_engine
from sqlalchemy import Connection, Engine, MetaData, create_engine, select
from sqlalchemy.orm import Session
from sqlalchemy.sql.ddl import CreateSchema, DropSchema
from testcontainers.postgres import PostgresContainer

Expand Down Expand Up @@ -38,3 +39,16 @@ def temp_schemas(engine: Engine, schemas: Set[str]):
def create_all_tables(engine: Engine, metadata: MetaData) -> None:
with engine.begin() as conn:
metadata.create_all(bind=conn)


def validate_relationships(engine: Engine, table: Callable) -> None:
"""
Query any table to make SQLAlchemy validate table relationships.
While SQLAlchemy's Metadata.create_all method will create the tables
in the database, it can still be that ORM FK relationships are
configured incorrectly. This will only become apparent upon first
using an ORM query. Hence, this simple session interaction.
"""
with Session(engine) as session:
session.execute(select(table))
3 changes: 2 additions & 1 deletion tests/omop_cdm/dynamic/cdm531/test_cdm531.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy import Engine, inspect

from src.omop_cdm.constants import CDM_SCHEMA, VOCAB_SCHEMA
from tests.conftest import create_all_tables, temp_schemas
from tests.conftest import create_all_tables, temp_schemas, validate_relationships
from tests.omop_cdm.dynamic.cdm_definitions import cdm531
from tests.omop_cdm.table_sets import CDM531_NON_VOCAB, CUSTOM, VOCAB

Expand All @@ -21,3 +21,4 @@ def test_create_tables_cdm531(cdm531_engine: Engine):
cdm_tables = inspect(cdm531_engine).get_table_names(SCHEMA_MAP[CDM_SCHEMA])
assert set(vocab_tables) == VOCAB
assert set(cdm_tables) == CDM531_NON_VOCAB | CUSTOM
validate_relationships(cdm531_engine, cdm531.Person)
3 changes: 2 additions & 1 deletion tests/omop_cdm/dynamic/cdm54/test_cdm54.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy import Engine, inspect

from src.omop_cdm.constants import CDM_SCHEMA, VOCAB_SCHEMA
from tests.conftest import create_all_tables, temp_schemas
from tests.conftest import create_all_tables, temp_schemas, validate_relationships
from tests.omop_cdm.dynamic.cdm_definitions import cdm54
from tests.omop_cdm.table_sets import CDM54_NON_VOCAB, CUSTOM, VOCAB

Expand All @@ -21,3 +21,4 @@ def test_create_tables_cdm54(cdm54_engine: Engine):
cdm_tables = inspect(cdm54_engine).get_table_names(SCHEMA_MAP[CDM_SCHEMA])
assert set(vocab_tables) == VOCAB
assert set(cdm_tables) == CDM54_NON_VOCAB | CUSTOM
validate_relationships(cdm54_engine, cdm54.Person)
3 changes: 2 additions & 1 deletion tests/omop_cdm/dynamic/cdm54_custom/test_cdm54_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy import BIGINT, Engine, inspect

from src.omop_cdm.constants import CDM_SCHEMA, VOCAB_SCHEMA
from tests.conftest import create_all_tables, temp_schemas
from tests.conftest import create_all_tables, temp_schemas, validate_relationships
from tests.omop_cdm.dynamic.cdm_definitions import cdm54_custom
from tests.omop_cdm.table_sets import CDM54_NON_VOCAB, CUSTOM

Expand All @@ -27,6 +27,7 @@ def _create_custom_cdm_tables(cdm54_custom_engine: Engine):
def test_custom_table_is_created(cdm54_custom_engine: Engine):
cdm_tables = inspect(cdm54_custom_engine).get_table_names(SCHEMA_MAP[CDM_SCHEMA])
assert set(cdm_tables) == CDM54_NON_VOCAB | CUSTOM | {"cloudspine"}
validate_relationships(cdm54_custom_engine, cdm54_custom.Person)


@pytest.mark.usefixtures("_create_custom_cdm_tables")
Expand Down
3 changes: 2 additions & 1 deletion tests/omop_cdm/dynamic/cdm54_plus_legacy/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy import Engine, inspect

from src.omop_cdm.constants import CDM_SCHEMA, VOCAB_SCHEMA
from tests.conftest import create_all_tables, temp_schemas
from tests.conftest import create_all_tables, temp_schemas, validate_relationships
from tests.omop_cdm.dynamic.cdm_definitions import cdm54_plus_legacy
from tests.omop_cdm.table_sets import CDM54_NON_VOCAB, CUSTOM, LEGACY, VOCAB

Expand All @@ -23,3 +23,4 @@ def test_create_tables_cdm54_plus_legacy(cdm54_legacy_engine: Engine):
SCHEMA_MAP[CDM_SCHEMA]
)
assert set(cdm_tables) == VOCAB | CDM54_NON_VOCAB | LEGACY | CUSTOM
validate_relationships(cdm54_legacy_engine, cdm54_plus_legacy.Person)
3 changes: 2 additions & 1 deletion tests/omop_cdm/dynamic/cdm600/test_cdm600.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy import Engine, inspect

from src.omop_cdm.constants import CDM_SCHEMA, VOCAB_SCHEMA
from tests.conftest import create_all_tables, temp_schemas
from tests.conftest import create_all_tables, temp_schemas, validate_relationships
from tests.omop_cdm.dynamic.cdm_definitions import cdm600
from tests.omop_cdm.table_sets import CDM600_NON_VOCAB, CUSTOM, VOCAB

Expand All @@ -21,3 +21,4 @@ def test_create_tables_cdm600(cdm600_engine: Engine):
cdm_tables = inspect(cdm600_engine).get_table_names(SCHEMA_MAP[CDM_SCHEMA])
assert set(vocab_tables) == VOCAB
assert set(cdm_tables) == CDM600_NON_VOCAB | CUSTOM
validate_relationships(cdm600_engine, cdm600.Person)
3 changes: 2 additions & 1 deletion tests/omop_cdm/regular/cdm531/test_cdm531.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import src.omop_cdm.regular.cdm531 as cdm531
from src.omop_cdm.constants import CDM_SCHEMA, VOCAB_SCHEMA
from tests.conftest import create_all_tables, temp_schemas
from tests.conftest import create_all_tables, temp_schemas, validate_relationships
from tests.omop_cdm.table_sets import CDM531_NON_VOCAB, VOCAB

SCHEMA_MAP = {VOCAB_SCHEMA: "vocab531", CDM_SCHEMA: "cdm531"}
Expand All @@ -22,3 +22,4 @@ def test_create_tables_cdm531_regular(cdm531_regular_engine: Engine):
cdm_tables = inspect(engine).get_table_names(SCHEMA_MAP[CDM_SCHEMA])
assert set(vocab_tables) == VOCAB
assert set(cdm_tables) == CDM531_NON_VOCAB
validate_relationships(cdm531_regular_engine, cdm531.Person)
3 changes: 2 additions & 1 deletion tests/omop_cdm/regular/cdm54/test_cdm54.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import src.omop_cdm.regular.cdm54 as cdm54
from src.omop_cdm.constants import CDM_SCHEMA, VOCAB_SCHEMA
from tests.conftest import create_all_tables, temp_schemas
from tests.conftest import create_all_tables, temp_schemas, validate_relationships
from tests.omop_cdm.table_sets import CDM54_NON_VOCAB, VOCAB

SCHEMA_MAP = {VOCAB_SCHEMA: "vocab54", CDM_SCHEMA: "cdm54"}
Expand All @@ -22,3 +22,4 @@ def test_create_tables_cdm54_regular(cdm54_regular_engine: Engine):
cdm_tables = inspect(engine).get_table_names(SCHEMA_MAP[CDM_SCHEMA])
assert set(vocab_tables) == VOCAB
assert set(cdm_tables) == CDM54_NON_VOCAB
validate_relationships(cdm54_regular_engine, cdm54.Person)
3 changes: 2 additions & 1 deletion tests/omop_cdm/regular/cdm600/test_cdm600.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import src.omop_cdm.regular.cdm600 as cdm600
from src.omop_cdm.constants import CDM_SCHEMA, VOCAB_SCHEMA
from tests.conftest import create_all_tables, temp_schemas
from tests.conftest import create_all_tables, temp_schemas, validate_relationships
from tests.omop_cdm.table_sets import CDM600_NON_VOCAB, VOCAB

SCHEMA_MAP = {VOCAB_SCHEMA: "vocab600", CDM_SCHEMA: "cdm600"}
Expand All @@ -22,3 +22,4 @@ def test_create_tables_cdm600_regular(cdm600_regular_engine: Engine):
cdm_tables = inspect(engine).get_table_names(SCHEMA_MAP[CDM_SCHEMA])
assert set(vocab_tables) == VOCAB
assert set(cdm_tables) == CDM600_NON_VOCAB
validate_relationships(cdm600_regular_engine, cdm600.Person)

0 comments on commit 5f6a02a

Please sign in to comment.