Run every node kind: route, map, join, extract (LLM or patterns), link (Splink), target
This commit is contained in:
+7
-60
@@ -2,16 +2,11 @@
|
||||
the rules point at is read into text (scanned ones through OCR)."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sqlite3
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine, make_url, text
|
||||
|
||||
from synthetic.site import build
|
||||
|
||||
FIXTURES = Path(__file__).parents[2] / "tests" / "fixtures" / "hospital"
|
||||
@@ -53,56 +48,8 @@ FILES = "dlt.sources.filesystem:filesystem"
|
||||
READERS = "dlt.sources.filesystem:readers"
|
||||
|
||||
|
||||
@pytest.fixture(params=["sqlite", "postgres"])
|
||||
def platform(request, tmp_path):
|
||||
"""Env for a fresh platform on each backend, and a SQL connection to read it back."""
|
||||
(tmp_path / "dagster").mkdir()
|
||||
env = {
|
||||
**os.environ,
|
||||
"ISSSTELEON_DATA": str(tmp_path),
|
||||
"DLT_DATA_DIR": str(tmp_path / "dlt"),
|
||||
"DAGSTER_HOME": str(tmp_path / "dagster"),
|
||||
}
|
||||
if request.param == "sqlite":
|
||||
url = env["PLATFORM_DATABASE_URL"] = f"sqlite:///{tmp_path / 'platform.db'}"
|
||||
attach = [f"ATTACH '{tmp_path}/platform__bronze.db' AS bronze"]
|
||||
else:
|
||||
pg = os.environ["POSTGRES_TEST_URL"] # required: run with `docker compose run --rm test`
|
||||
env.update(PLATFORM_DATABASE_URL=pg, DBT_TARGET="postgres")
|
||||
url = make_url(pg).set(drivername="postgresql+psycopg2")
|
||||
attach = []
|
||||
with create_engine(url).begin() as con:
|
||||
for schema in ("bronze", "config"):
|
||||
con.exec_driver_sql(f"drop schema if exists {schema} cascade")
|
||||
|
||||
def count(table: str) -> int:
|
||||
with create_engine(url).connect() as con:
|
||||
for statement in attach:
|
||||
con.exec_driver_sql(statement)
|
||||
return con.execute(text(f"select count(*) from bronze.{table}")).scalar()
|
||||
|
||||
def query(sql: str):
|
||||
with create_engine(url).connect() as con:
|
||||
for statement in attach:
|
||||
con.exec_driver_sql(statement)
|
||||
return con.execute(text(sql)).fetchall()
|
||||
|
||||
return env, count, query
|
||||
|
||||
|
||||
def run(env, *args):
|
||||
subprocess.run(args, env=env, check=True)
|
||||
|
||||
|
||||
def ingest(env):
|
||||
config = Path(env["ISSSTELEON_DATA"]) / "run.json"
|
||||
config.write_text(json.dumps({"ops": {"run_pipeline": {"config": {"pipeline": "site"}}}}))
|
||||
run(env, "dagster", "job", "execute", "-m", "models.definitions", "-j", "ingest",
|
||||
"--config", str(config)) # fmt: skip
|
||||
|
||||
|
||||
def test_a_site_reaches_bronze_once_and_its_documents_are_read(platform, tmp_path):
|
||||
env, count, query = platform
|
||||
count, query = platform.count, platform.query
|
||||
tables = {}
|
||||
for name in ("cacu", "cama"):
|
||||
shutil.copy(FIXTURES / f"hospital_{name}.db", tmp_path / f"hospital_{name}.db")
|
||||
@@ -111,7 +58,7 @@ def test_a_site_reaches_bronze_once_and_its_documents_are_read(platform, tmp_pat
|
||||
tables[name] = [t for (t,) in found]
|
||||
truth = build(FIXTURES / "hospital_cacu.db", tmp_path / "site")["records"]
|
||||
(tmp_path / "pipeline.json").write_text(json.dumps(graph(tmp_path, tables)))
|
||||
run(env, "python", "-m", "persistance.config_log", "site", str(tmp_path / "pipeline.json"))
|
||||
platform.run("python", "-m", "persistance.config_log", "site", str(tmp_path / "pipeline.json"))
|
||||
|
||||
by_place = {}
|
||||
for r in truth:
|
||||
@@ -131,17 +78,17 @@ def test_a_site_reaches_bronze_once_and_its_documents_are_read(platform, tmp_pat
|
||||
expected[f"{name}_{t}"] = con.execute(f"select count(*) from {t}").fetchone()[0]
|
||||
|
||||
def counts():
|
||||
return {node: count(f"n__site__{node}") for node in expected}
|
||||
return {node: count(f"bronze.n__site__{node}") for node in expected}
|
||||
|
||||
for _ in range(2): # loading the same site again changes nothing
|
||||
ingest(env)
|
||||
platform.ingest()
|
||||
assert counts() == expected
|
||||
read_once = query("select sha256, _dlt_load_id from bronze.read_cache order by sha256")
|
||||
read_once = query("select key, _dlt_load_id from bronze.read_cache order by key")
|
||||
assert len(read_once) == expected["archivos"] + expected["pdfs"] # each file read once
|
||||
|
||||
with sqlite3.connect(tmp_path / "hospital_cacu.db") as con: # a corrected record updates
|
||||
con.execute("update paciente set pac_codigo_postal = '64000' where pac_id = 1")
|
||||
ingest(env)
|
||||
platform.ingest()
|
||||
assert counts() == expected
|
||||
corrected = "select pac_codigo_postal from bronze.n__site__cacu_paciente where pac_id = 1"
|
||||
assert query(corrected) == [("64000",)]
|
||||
@@ -155,6 +102,6 @@ def test_a_site_reaches_bronze_once_and_its_documents_are_read(platform, tmp_pat
|
||||
assert bool(ocr) == r["scanned"] # OCR runs on scans, and only on scans
|
||||
if "record_number" in r["identity"]: # even scanned, the identifier reads back exactly
|
||||
assert r["identity"]["record_number"] in txt, (r["where"], txt)
|
||||
assert query("select sha256, _dlt_load_id from bronze.read_cache order by sha256") == (
|
||||
assert query("select key, _dlt_load_id from bronze.read_cache order by key") == (
|
||||
read_once
|
||||
) # reruns never read a file again
|
||||
|
||||
Reference in New Issue
Block a user