integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
"""Exercises `SqlConnector` against a real MySQL server, covering
|
||||
MySQL-specific reflection (native `ENUM` columns, `pymysql` quirks, DATE
|
||||
types) that the SQLite-based tests don't reach. Skipped unless
|
||||
`MYSQL_TEST_URL` is set.
|
||||
|
||||
To stand one up locally:
|
||||
docker run -d --name isssteleon-mysql-test \
|
||||
-e MYSQL_ROOT_PASSWORD=<root_pw> -e MYSQL_DATABASE=hospital_legacy \
|
||||
-e MYSQL_USER=<user> -e MYSQL_PASSWORD=<pw> -p 3307:3306 mysql:8.0
|
||||
export MYSQL_TEST_URL="mysql+pymysql://<user>:<pw>@127.0.0.1:3307/hospital_legacy"
|
||||
"""
|
||||
import os
|
||||
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
|
||||
from webapi.imports.connectors.errors import ConnectorError
|
||||
from webapi.imports.connectors.sql import SqlConnector
|
||||
|
||||
MYSQL_TEST_URL = os.environ.get("MYSQL_TEST_URL")
|
||||
|
||||
pytestmark = pytest.mark.skipif(not MYSQL_TEST_URL, reason="MYSQL_TEST_URL not set — no real MySQL server to test against")
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def legacy_table():
|
||||
"""A legacy table whose columns are unrelated to Core's naming, as in a
|
||||
third-party database we don't control."""
|
||||
engine = sa.create_engine(MYSQL_TEST_URL)
|
||||
table_name = "test_connectors_mysql_fixture"
|
||||
with engine.begin() as conn:
|
||||
conn.execute(sa.text(f"DROP TABLE IF EXISTS {table_name}"))
|
||||
conn.execute(
|
||||
sa.text(
|
||||
f"CREATE TABLE {table_name} ("
|
||||
" legacy_id VARCHAR(40) NOT NULL PRIMARY KEY,"
|
||||
" legacy_sexo ENUM('F','M'),"
|
||||
" legacy_nacimiento DATE"
|
||||
") ENGINE=InnoDB"
|
||||
)
|
||||
)
|
||||
conn.execute(
|
||||
sa.text(
|
||||
f"INSERT INTO {table_name} (legacy_id, legacy_sexo, legacy_nacimiento) VALUES "
|
||||
"('MYFIX-1', 'F', '1990-01-15'), ('MYFIX-2', 'M', '1985-06-20')"
|
||||
)
|
||||
)
|
||||
yield table_name
|
||||
with engine.begin() as conn:
|
||||
conn.execute(sa.text(f"DROP TABLE IF EXISTS {table_name}"))
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_connects_and_reports_mysql_dialect():
|
||||
connector = SqlConnector(MYSQL_TEST_URL)
|
||||
connector.test_connection()
|
||||
assert connector.get_dialect() == "mysql"
|
||||
|
||||
|
||||
def test_introspects_real_mysql_schema(legacy_table):
|
||||
connector = SqlConnector(MYSQL_TEST_URL)
|
||||
assert legacy_table in connector.list_tables()
|
||||
columns = {c.name: c.inferred_type for c in connector.list_columns(legacy_table)}
|
||||
assert columns["legacy_id"] == "string"
|
||||
assert columns["legacy_nacimiento"] == "date"
|
||||
# A native MySQL ENUM column is reflected as a string-like type.
|
||||
assert columns["legacy_sexo"] == "string"
|
||||
|
||||
|
||||
def test_reads_rows_with_column_mapping_against_real_mysql(legacy_table):
|
||||
connector = SqlConnector(MYSQL_TEST_URL)
|
||||
connector.select_table(legacy_table)
|
||||
preview = connector.preview()
|
||||
assert preview.total_rows == 2
|
||||
|
||||
rows = list(
|
||||
connector.read_rows({"legacy_id": "pac_id_hospital", "legacy_sexo": "pac_sexo", "legacy_nacimiento": "pac_fecha_nacimiento"})
|
||||
)
|
||||
by_id = {r["pac_id_hospital"]: r for r in rows}
|
||||
assert by_id["MYFIX-1"]["pac_sexo"] == "F"
|
||||
# Rows come straight from the driver, so a MySQL DATE is a `date`.
|
||||
import datetime as _dt
|
||||
|
||||
assert by_id["MYFIX-1"]["pac_fecha_nacimiento"] == _dt.date(1990, 1, 15)
|
||||
|
||||
|
||||
def test_bad_mysql_credentials_raise_connector_error():
|
||||
bad_url = MYSQL_TEST_URL.rsplit("@", 1)[-1]
|
||||
connector = SqlConnector(f"mysql+pymysql://nobody:wrongpassword@{bad_url}")
|
||||
with pytest.raises(ConnectorError):
|
||||
connector.test_connection()
|
||||
Reference in New Issue
Block a user