-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_sdk.py
More file actions
153 lines (122 loc) · 4.61 KB
/
Copy pathtest_sdk.py
File metadata and controls
153 lines (122 loc) · 4.61 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import base64
from typing import AsyncIterator
import pytest
import pytest_asyncio
from fhirpy import AsyncFHIRClient
from pydantic import ValidationError
from fhir_types.hl7_fhir_r4_core import HumanName
from fhir_types.hl7_fhir_r4_core.bundle import Bundle
from fhir_types.hl7_fhir_r4_core.observation import Observation
from fhir_types.hl7_fhir_r4_core.patient import Patient
FHIR_SERVER_URL = "http://localhost:8080/fhir"
USERNAME = "root"
PASSWORD = "<SECRET>" # get actual value from docker-compose.yaml: BOX_ROOT_CLIENT_SECRET
TOKEN = base64.b64encode(f"{USERNAME}:{PASSWORD}".encode()).decode()
@pytest.fixture(scope="module")
def client() -> AsyncFHIRClient:
# Generated models are snake_case with FHIR camelCase aliases; serialize() dumps by alias.
return AsyncFHIRClient(
FHIR_SERVER_URL,
authorization=f"Basic {TOKEN}",
dump_resource=lambda x: x.serialize(),
)
@pytest_asyncio.fixture
async def created_patient(client: AsyncFHIRClient) -> AsyncIterator[Patient]:
patient = Patient(
name=[HumanName(given=["Test"], family="FhirpyPatient")],
gender="female",
birthDate="1980-01-01",
)
created = await client.create(patient)
yield created
try:
if created.id is not None:
await client.delete(f"Patient/{created.id}")
except Exception:
pass
@pytest.mark.asyncio
async def test_create_patient(client: AsyncFHIRClient) -> None:
patient = Patient(
name=[HumanName(given=["Create"], family="Test")],
gender="female",
birthDate="1980-01-01",
)
created = await client.create(patient)
assert created.id is not None
assert created.name is not None
assert created.name[0].family == "Test"
assert created.gender == "female"
assert created.birthDate == "1980-01-01"
await client.delete(f"Patient/{created.id}")
@pytest.mark.asyncio
async def test_search_patients(client: AsyncFHIRClient, created_patient: Patient) -> None:
"""client.resources(Patient).fetch() — requires class-level resourceType access"""
patients = await client.resources(Patient).fetch()
assert len(patients) > 0
found = None
for p in patients:
if p.id == created_patient.id:
found = p
break
assert found is not None, f"Patient {created_patient.id} not found in search results"
@pytest.mark.asyncio
async def test_search_with_filters(client: AsyncFHIRClient, created_patient: Patient) -> None:
patients = await client.resources(Patient).search(family="FhirpyPatient").fetch()
assert len(patients) > 0
ids = [p.id for p in patients]
assert created_patient.id in ids
@pytest.mark.asyncio
async def test_search_returns_typed_resources(client: AsyncFHIRClient, created_patient: Patient) -> None:
"""Verify fetched resources deserialize into the generated Patient class."""
patients = await client.resources(Patient).fetch()
for p in patients:
assert isinstance(p, Patient)
assert p.resourceType == "Patient"
@pytest.mark.asyncio
async def test_update_patient(client: AsyncFHIRClient, created_patient: Patient) -> None:
assert created_patient.id is not None
created_patient.name = [HumanName(given=["Updated"], family="FhirpyPatient")]
created_patient.gender = "male"
updated = await client.update(created_patient)
assert updated.id == created_patient.id
assert updated.gender == "male"
assert updated.name is not None
assert updated.name[0].given == ["Updated"]
def test_resource_type_class_access() -> None:
"""resourceType is exposed at class level (needed for fhirpy search/fetch)."""
assert Patient.resourceType == "Patient"
assert Observation.resourceType == "Observation"
assert Bundle.resourceType == "Bundle"
def test_wrong_resource_type() -> None:
json = """
{
"resourceType" : "Bundle",
"type" : "searchset",
"entry" : [{
"resource" : { "resourceType" : "Weird_Patient", "id" : "3123" }
}]
}
"""
with pytest.raises(ValidationError):
Bundle.from_json(json)
def test_wrong_fields() -> None:
json = """
{
"resourceType" : "Bundle",
"type" : "searchset",
"entry" : [{
"resource" : { "resourceType" : "Patient", "id" : "3123", "very_wrong_field" : "WRONG" }
}]
}
"""
with pytest.raises(ValidationError):
Bundle.from_json(json)
def test_to_from_json() -> None:
p = Patient(
name=[HumanName(given=["Test"], family="Patient")],
gender="female",
birthDate="1980-01-01",
)
json = p.to_json(indent=2)
p2 = Patient.from_json(json)
assert p == p2