import json from datetime import date from hashlib import sha256 from edf_files import SEX_MAP, hospital, pipeline, small_edf def test_edf_pipeline_keeps_and_links_the_files(platform, tmp_path): hospital_url = hospital(tmp_path / "hospital.db") uploads = tmp_path / "uploads" uploads.mkdir() a = small_edf(uploads / "a.edf") b = small_edf(uploads / "b.edf", patient_code="N1002", birthdate=date(2025, 2, 5)) bad = b"junk" (uploads / "bad.edf").write_bytes(bad) clean = {sha256(a).hexdigest(): a, sha256(b).hexdigest(): b} bad_hash = sha256(bad).hexdigest() drawing = tmp_path / "edf.json" drawing.write_text(json.dumps(pipeline(hospital_url, str(uploads)))) concepts = tmp_path / "sex.json" concepts.write_text(json.dumps(SEX_MAP)) platform.save("concept_maps", "edf_sex", concepts) platform.save("pipelines", "edf", drawing) for _ in range(2): platform.ingest("edf") people = dict( platform.query( "select i.value, p.person_id from silver.person p " "join silver.person_identifier i on i.person_id = p.person_id" ) ) assert set(people) == {"N1001", "N1002"} studies = platform.query("select person_id, study_date, lab_record_id, edf from silver.eeg") assert {(p, str(d), lab, key) for p, d, lab, key in studies} == { (people["N1001"], "2026-03-02", "LAB-1", sha256(a).hexdigest()), (people["N1002"], "2026-03-02", "LAB-1", sha256(b).hexdigest()), } assert len(studies) == 2 assert { key: bytes(value) for key, value in platform.query("select key, content from bronze.files") } == {**clean, bad_hash: bad} assert platform.count("bronze.n__edf__header") == 3 ((error, *fields),) = platform.query( "select edf_error, edf_patient_code, edf_sex, edf_birth_date, edf_study_date, " "edf_record_code, edf_duration_s, edf_channels from bronze.n__edf__header " f"where content = '{bad_hash}'" ) assert error and fields == [None] * 7 ((study, reasons),) = platform.query("select study, _held from bronze.staged__held") assert study == "eeg" assert any(reason.startswith("study_date:") for reason in json.loads(reasons))