Conectar API: sources, uploads, pipelines, runs, node rows, review; tested end to end
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
"""An operator connects a messy site through the API, the way the /conectar screens will:
|
||||
look into its sources, save the drawn pipeline, run it, read what came out, and settle
|
||||
what review shows. Real API and Dagster processes; nothing faked."""
|
||||
|
||||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from synthetic.site import build
|
||||
|
||||
ROOT = Path(__file__).parents[2]
|
||||
HOSPITAL = ROOT / "tests" / "fixtures" / "hospital" / "hospital_cacu.db"
|
||||
PIPELINE = ROOT / "tests" / "fixtures" / "pipelines" / "messy_site.json"
|
||||
SQL = "dlt.sources.sql_database:sql_database"
|
||||
CYTOLOGY = {"entity": "cervical_cytology"}
|
||||
|
||||
|
||||
def ok(response, status=200):
|
||||
assert response.status_code == status, response.text
|
||||
return response.json()
|
||||
|
||||
|
||||
def wait_for(api, run_id: str, seconds: int = 900) -> None:
|
||||
deadline = time.monotonic() + seconds
|
||||
while (status := ok(api.get(f"/runs/{run_id}"))["status"]) not in ("SUCCESS", "FAILURE"):
|
||||
assert time.monotonic() < deadline, f"run {run_id} still {status}"
|
||||
time.sleep(2)
|
||||
assert status == "SUCCESS"
|
||||
|
||||
|
||||
def run(api, pipeline: str) -> None:
|
||||
wait_for(api, ok(api.post(f"/ingest/pipelines/{pipeline}/runs", json={}))["run_id"])
|
||||
|
||||
|
||||
def test_an_operator_connects_a_site_and_settles_its_review(api, platform, tmp_path):
|
||||
site = tmp_path / "site"
|
||||
truth = build(HOSPITAL, site)["records"]
|
||||
database = {"credentials": f"sqlite:///{site}/site.db"}
|
||||
|
||||
# What the site has, and what one of its tables holds (documents shown by size).
|
||||
found = ok(api.post("/ingest/sources/resources", json={"source": SQL, "args": database}))
|
||||
assert found == ["ArchivosUni", "pacientes_his"]
|
||||
missing = {"credentials": f"sqlite:///{tmp_path}/nowhere/site.db"}
|
||||
unreachable = api.post("/ingest/sources/resources", json={"source": SQL, "args": missing})
|
||||
assert unreachable.status_code == 422 and "unable to open" in unreachable.text
|
||||
reports = {"id": "reports", "kind": "source", "source": SQL, "args": database}
|
||||
sample = ok(
|
||||
api.post("/ingest/sources/preview?limit=3", json=reports | {"resource": "ArchivosUni"})
|
||||
)
|
||||
assert len(sample) == 3 and all(r["Archivo"].endswith(" bytes>") for r in sample)
|
||||
|
||||
# A file someone uploads becomes a source like any other.
|
||||
csv = (site / "files" / "consultas.csv").read_bytes()
|
||||
uploaded = ok(api.post("/ingest/uploads", files={"file": ("consultas.csv", csv)}))
|
||||
notes = {"id": "notes", "kind": "source", "source": "dlt.sources.filesystem:readers",
|
||||
"resource": "read_csv", "args": {"bucket_url": uploaded["bucket_url"]}} # fmt: skip
|
||||
assert len(ok(api.post("/ingest/sources/preview?limit=5", json=notes))) == 5
|
||||
|
||||
# Saving a drawing: broken ones are refused with the reason; good ones get versions.
|
||||
drawn = json.loads(PIPELINE.read_text().replace("{site}", str(site)))
|
||||
broken = {"nodes": [*drawn["nodes"], {"id": "loose", "kind": "read", "inputs": ["gone"],
|
||||
"column": "x"}]} # fmt: skip
|
||||
assert "no node" in api.put("/ingest/pipelines/site", json=broken).text
|
||||
assert ok(api.put("/ingest/pipelines/site", json=drawn)) == {"version": 1}
|
||||
assert ok(api.get("/ingest/pipelines/site"))["version"] == 1
|
||||
|
||||
# A password is never shown, and survives the UI sending the masked drawing back.
|
||||
secret = "postgresql://isss:S3cret@10.0.10.9/his"
|
||||
hidden = {"nodes": [{"id": "his", "kind": "source", "source": SQL,
|
||||
"args": {"credentials": secret}}]} # fmt: skip
|
||||
ok(api.put("/ingest/pipelines/his", json=hidden))
|
||||
shown = ok(api.get("/ingest/pipelines/his"))["pipeline"]
|
||||
assert "S3cret" not in json.dumps(shown)
|
||||
assert ok(api.put("/ingest/pipelines/his", json=shown)) == {"version": 2}
|
||||
stored = platform.query(
|
||||
"select doc from config.pipelines where pipeline_id = 'his' and version = 2"
|
||||
)
|
||||
assert secret in stored[0][0]
|
||||
|
||||
# Run it, and look at what any node produced.
|
||||
run(api, "site")
|
||||
reports_read = ok(api.get("/ingest/pipelines/site/nodes/archivos/rows?limit=500"))
|
||||
record_of = {f"ArchivosUni:{r['id']}": r["_record"] for r in reports_read}
|
||||
assert len(ok(api.get("/ingest/pipelines/site/nodes/cytology/rows?limit=500"))) == 53
|
||||
|
||||
# Review shows exactly the reports that contradict each other; nothing else is held.
|
||||
conflicting = {record_of[r["where"]] for r in truth if r.get("conflict")}
|
||||
assert {c["_record"] for c in ok(api.get("/review/conflicts", params=CYTOLOGY))} == conflicting
|
||||
assert ok(api.get("/review/held", params=CYTOLOGY)) == []
|
||||
assert isinstance(
|
||||
ok(api.get("/review/links", params={"pipeline": "site", "node": "persons"})), list
|
||||
)
|
||||
|
||||
# The reviewer rejects the wrong copies; the next run settles the conflicts.
|
||||
for r in truth:
|
||||
if r.get("flipped"):
|
||||
decision = CYTOLOGY | {"record": record_of[r["where"]], "reason": "flipped HPV"}
|
||||
ok(api.post("/review/rejections", json=decision), status=201)
|
||||
run(api, "site")
|
||||
assert ok(api.get("/review/conflicts", params=CYTOLOGY)) == []
|
||||
placed = platform.count("silver.cervical_cytology")
|
||||
assert placed == sum(
|
||||
1 for r in truth if r["entity"] == "cervical_cytology" and not r.get("flipped")
|
||||
)
|
||||
Reference in New Issue
Block a user