107 lines
5.0 KiB
Python
107 lines
5.0 KiB
Python
"""Rows that reference a parent by the source's own id (a biopsy's `mast_id`,
|
|
a hospital table's `pac_id`) must land on the right parent, whatever order or
|
|
numbering the parents arrive in."""
|
|
import base64
|
|
import json
|
|
|
|
import pytest
|
|
|
|
|
|
def _json(records, target, mapping=None):
|
|
mapping = mapping or {k: k for k in records[0]}
|
|
return {"source_type": "json", "target": target, "column_mapping": mapping,
|
|
"payload": {"file_base64": base64.b64encode(json.dumps(records).encode()).decode()}}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def client(wire_domain):
|
|
wire_domain("cama")
|
|
from webapi import create_app
|
|
|
|
app = create_app()
|
|
app.config.update(TESTING=True)
|
|
with app.test_client() as test_client:
|
|
yield test_client
|
|
|
|
|
|
def test_biopsy_keeps_pointing_at_its_mammogram(client):
|
|
pacientes = [{"pac_id": 70, "pac_id_hospital": "SRC-A", "pac_fecha_nacimiento": "1970-01-01"},
|
|
{"pac_id": 50, "pac_id_hospital": "SRC-B", "pac_fecha_nacimiento": "1971-01-01"}]
|
|
# Hospital ids out of insertion order: an auto-numbered table would swap them.
|
|
mastografias = [{"mast_id": 902, "pac_id": 50, "pac_id_hospital": "SRC-B", "mast_birads": "B4"},
|
|
{"mast_id": 901, "pac_id": 70, "pac_id_hospital": "SRC-A", "mast_birads": "B1"}]
|
|
biopsias = [{"bx_id": 1, "mast_id": 902, "pac_id": 50, "pac_id_hospital": "SRC-B"}]
|
|
sources = [_json(pacientes, "cama.Paciente"), _json(mastografias, "cama.Mastografia"),
|
|
_json(biopsias, "cama.BiopsiaMama", {"mast_id": "mast_id", "pac_id": "pac_id", "pac_id_hospital": "pac_id_hospital"})]
|
|
body = client.post("/imports/commit", json={"sources": sources}).get_json()
|
|
assert all(r["rejected"] == 0 for r in body["results"]), body
|
|
|
|
from persistance.cama import biopsia_mama_repository, mastografia_repository, paciente_repository
|
|
|
|
pacientes = {p.pac_id_hospital: p.pac_id for p in paciente_repository.list()}
|
|
assert pacientes == {"SRC-A": 70, "SRC-B": 50}
|
|
biopsia = biopsia_mama_repository.list()[0]
|
|
mamografia = mastografia_repository.get(biopsia.mast_id)
|
|
assert mamografia.pac_id == biopsia.pac_id == 50
|
|
|
|
|
|
def test_natural_key_wins_over_a_foreign_internal_id(client):
|
|
"""The patient came from a CSV without ids; the hospital table sends its own pac_id."""
|
|
pacientes = [{"pac_id_hospital": "CSV-1", "pac_fecha_nacimiento": "1980-01-01"}]
|
|
client.post("/imports/commit", json={"sources": [_json(pacientes, "cama.Paciente")]})
|
|
notas = [{"pac_id": 999, "pac_id_hospital": "CSV-1", "nm_id_hospital": "N-1"}]
|
|
result = client.post("/imports/commit", json={"sources": [_json(notas, "cama.NotaMedica")]}).get_json()["results"][0]
|
|
assert result["created"] == 1, result
|
|
assert result["linked"] == {"pac_id": 1}
|
|
|
|
from persistance.cama import nota_medica_repository, paciente_repository
|
|
|
|
paciente = next(p for p in paciente_repository.list() if p.pac_id_hospital == "CSV-1")
|
|
nota = next(n for n in nota_medica_repository.list() if n.nm_id_hospital == "N-1")
|
|
assert nota.pac_id == paciente.pac_id != 999
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def tea_client(wire_domain):
|
|
wire_domain("tea")
|
|
from webapi import create_app
|
|
|
|
app = create_app()
|
|
app.config.update(TESTING=True)
|
|
with app.test_client() as test_client:
|
|
yield test_client
|
|
|
|
|
|
def test_tea_children_keep_pointing_at_their_parents_source_ids(tea_client):
|
|
sources = [
|
|
_json([{"pac_id": 1, "pac_id_hospital": "T-1"}], "tea.Pacientes"),
|
|
_json([{"ord_id": "ord-1", "ord_pac_id": 1, "ord_id_hospital": "O-1"}], "tea.OrdenesEstudio"),
|
|
_json([{"psi_id": "psi-1", "psi_ord_id": "ord-1", "psi_pac_id": 1}], "tea.EvaluacionPsicologica"),
|
|
_json([{"ins_psi_id": "psi-1", "ins_instrumento": "ADOS-2"}], "tea.InstrumentoAplicado"),
|
|
_json([{"eeg_id": "eeg-1", "eeg_ord_id": "ord-1", "eeg_id_registro_laboratorio": "L-1"}], "tea.EegEstudio"),
|
|
_json([{"seg_eeg_id": "eeg-1", "seg_duracion_s": 30}], "tea.EegSegmento"),
|
|
]
|
|
body = tea_client.post("/imports/commit", json={"sources": sources}).get_json()
|
|
assert all(r["rejected"] == 0 for r in body["results"]), body
|
|
|
|
from persistance.tea import (
|
|
eeg_estudio_repository,
|
|
eeg_segmento_repository,
|
|
evaluacion_psicologica_repository,
|
|
instrumento_aplicado_repository,
|
|
)
|
|
|
|
assert eeg_estudio_repository.get(eeg_segmento_repository.list()[0].seg_eeg_id).eeg_id_registro_laboratorio == "L-1"
|
|
evaluacion = evaluacion_psicologica_repository.get(instrumento_aplicado_repository.list()[0].ins_psi_id)
|
|
assert evaluacion.psi_ord_id == "ord-1"
|
|
|
|
|
|
def test_tea_rows_without_a_source_id_get_one(tea_client):
|
|
eeg = _json([{"eeg_id_registro_laboratorio": "L-SIN-ID"}], "tea.EegEstudio")
|
|
assert tea_client.post("/imports/commit", json={"sources": [eeg]}).get_json()["results"][0]["created"] == 1
|
|
|
|
from persistance.tea import eeg_estudio_repository
|
|
|
|
[fila] = [e for e in eeg_estudio_repository.list() if e.eeg_id_registro_laboratorio == "L-SIN-ID"]
|
|
assert len(fila.eeg_id) == 36
|