From e19121bc4280acdde10153ec5f93aeaa590b2bb4 Mon Sep 17 00:00:00 2001 From: alberto Date: Fri, 13 Nov 2026 15:03:02 -0600 Subject: [PATCH] TEA: an order carries its study's report as a PDF through the import --- tests/test_new_sources.py | 38 +++++++++++++++++++ webapi/imports/schemas/tea/ordenes_estudio.py | 1 + 2 files changed, 39 insertions(+) diff --git a/tests/test_new_sources.py b/tests/test_new_sources.py index f647c74..d13c862 100644 --- a/tests/test_new_sources.py +++ b/tests/test_new_sources.py @@ -348,3 +348,41 @@ def test_embedded_image_in_the_report_maps_onto_the_target_dicom_column(client): [row] = mastografia_repository.list(pac_id_hospital="IMG-0001") assert "nĂ³dulo circunscrito" in row.mast_interpretacion assert row.mast_dicom_blob.startswith(b"\x89PNG") + + +def test_a_tea_order_keeps_the_pdf_its_sql_source_carries(client, tmp_path): + """Pins down: `ord_pdf_blob` travels with the order from a SQL source into `ordenes_estudio`, byte for + byte, and an order without a file stays without one. The source is a SQLite file in `tmp_path` holding a + two-row `ordenes_estudio`. Fails if the import schema drops the field or the commit rewrites the bytes.""" + report = text_pdf([["EVALUACION PSICOLOGICA", "ADOS-2 Modulo 2"]]) + url = f"sqlite:///{tmp_path / 'hospital.db'}" + table = sa.Table( + "ordenes_estudio", sa.MetaData(), + sa.Column("ord_id", sa.String(36), primary_key=True), + sa.Column("ord_id_hospital", sa.String(64)), + sa.Column("ord_tipo_estudio", sa.Text), + sa.Column("ord_pdf_blob", sa.LargeBinary), + ) + engine = sa.create_engine(url) + table.create(engine) + with engine.begin() as connection: + connection.execute(sa.insert(table), [ + {"ord_id": "ord-pdf-1", "ord_id_hospital": "ORD-PDF-1", "ord_tipo_estudio": "ADOS2", "ord_pdf_blob": report}, + {"ord_id": "ord-pdf-2", "ord_id_hospital": "ORD-PDF-2", "ord_tipo_estudio": "EEG", "ord_pdf_blob": None}, + ]) + engine.dispose() + source = { + "source_type": "sql", + "target": "tea.OrdenesEstudio", + "column_mapping": {column.name: column.name for column in table.columns}, + "payload": {"connection_string": url, "table": "ordenes_estudio"}, + } + + result = client.post("/imports/commit", json={"sources": [source], "on_conflict": "upsert"}).get_json()["results"][0] + + assert (result["created"], result["rejected"]) == (2, 0), result + + from persistance.tea import ordenes_estudio_repository + + assert ordenes_estudio_repository.get("ord-pdf-1").ord_pdf_blob == report + assert ordenes_estudio_repository.get("ord-pdf-2").ord_pdf_blob is None diff --git a/webapi/imports/schemas/tea/ordenes_estudio.py b/webapi/imports/schemas/tea/ordenes_estudio.py index 72c18ae..bd9d06e 100644 --- a/webapi/imports/schemas/tea/ordenes_estudio.py +++ b/webapi/imports/schemas/tea/ordenes_estudio.py @@ -15,3 +15,4 @@ class OrdenesEstudioImportSchema(ImportSchema): ord_id_hospital: OptionalStr = None ord_tipo_estudio: CatalogValue(_TIPO_ESTUDIO_CATALOG) = None ord_fecha: FlexDate = None + ord_pdf_blob: Optional[bytes] = None