File size: 3,971 Bytes
ba54ea9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | import json
import shutil
from recap.cases import load_case
def test_load_case_with_only_fhir(tmp_path):
case = tmp_path / "tiny"
case.mkdir()
(case / "manifest.json").write_text(json.dumps({
"id": "tiny",
"display_name": "Tiny Test",
"fhir_bundle": "fhir.json",
"docs": [],
"images": [],
"demo_questions": [],
}))
shutil.copy("tests/fixtures/tiny_fhir.json", case / "fhir.json")
p = load_case(str(tmp_path), "tiny")
assert p.id == "tiny"
assert p.display_name == "Tiny Test" # manifest override wins
assert len(p.events) > 0
def test_load_case_pulls_display_name_from_fhir_when_manifest_omits_it(tmp_path):
"""Minimal manifest — name, age, gender all come from the FHIR Patient resource."""
case = tmp_path / "auto"
case.mkdir()
(case / "manifest.json").write_text(json.dumps({
"id": "auto",
"fhir_bundle": "fhir.json",
"demo_questions": [],
}))
shutil.copy("tests/fixtures/tiny_fhir.json", case / "fhir.json")
p = load_case(str(tmp_path), "auto")
assert p.display_name.startswith("Jane Doe") # from FHIR Patient.name
assert p.age is not None and p.age >= 60
assert p.gender == "female"
def test_load_case_with_pdf_docs(tmp_path):
case = tmp_path / "tiny"
case.mkdir()
(case / "docs").mkdir()
shutil.copy("tests/fixtures/tiny_lab.pdf", case / "docs" / "lab.pdf")
(case / "manifest.json").write_text(json.dumps({
"id": "tiny",
"display_name": "Tiny",
"fhir_bundle": None,
"docs": [{
"file": "docs/lab.pdf",
"date": "2022-03-14",
"category": "lab",
"title": "Renal panel",
}],
"images": [],
"demo_questions": [],
}))
p = load_case(str(tmp_path), "tiny")
pdf_events = [e for e in p.events if e.source == "lab.pdf"]
assert len(pdf_events) == 1
assert pdf_events[0].category == "lab"
assert "Creatinine" in pdf_events[0].body
def test_load_case_with_images(tmp_path):
case = tmp_path / "tiny"
case.mkdir()
(case / "images").mkdir()
from PIL import Image
Image.new("RGB", (10, 10), "white").save(case / "images" / "fundus.png")
(case / "manifest.json").write_text(json.dumps({
"id": "tiny",
"display_name": "Tiny",
"fhir_bundle": None,
"docs": [],
"images": [{
"file": "images/fundus.png",
"date": "2023-04-01",
"category": "scan",
"title": "Fundus photo",
}],
"demo_questions": [],
}))
p = load_case(str(tmp_path), "tiny")
assert len(p.events) == 1
assert p.events[0].category == "scan"
assert p.events[0].source == "fundus.png"
def test_load_case_events_chronologically_orderable(tmp_path):
"""Multi-source case (FHIR + PDF + image) yields a sortable timeline."""
case = tmp_path / "tiny"
case.mkdir()
(case / "docs").mkdir()
(case / "images").mkdir()
shutil.copy("tests/fixtures/tiny_fhir.json", case / "fhir.json")
shutil.copy("tests/fixtures/tiny_lab.pdf", case / "docs" / "lab.pdf")
from PIL import Image
Image.new("RGB", (10, 10), "white").save(case / "images" / "fundus.png")
(case / "manifest.json").write_text(json.dumps({
"id": "tiny",
"display_name": "Tiny",
"fhir_bundle": "fhir.json",
"docs": [{
"file": "docs/lab.pdf",
"date": "2022-03-14",
"category": "lab",
"title": "Lab",
}],
"images": [{
"file": "images/fundus.png",
"date": "2023-04-01",
"category": "scan",
"title": "Fundus",
}],
"demo_questions": [],
}))
p = load_case(str(tmp_path), "tiny")
sorted_dates = sorted(e.date for e in p.events)
assert sorted_dates == [e.date for e in sorted(p.events, key=lambda e: e.date)]
|