62 lines
3.0 KiB
Python
62 lines
3.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
|