-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_sdk.py
More file actions
185 lines (154 loc) · 5.31 KB
/
test_sdk.py
File metadata and controls
185 lines (154 loc) · 5.31 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import asyncio
import base64
from typing import AsyncIterator
import pytest
import pytest_asyncio
from fhirpy import AsyncFHIRClient
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
from fhir_types.hl7_fhir_r4_core.organization import Organization
from pydantic import ValidationError
FHIR_SERVER_URL = "http://localhost:8080/fhir"
USERNAME = "root"
PASSWORD = (
"<SECRET>"
)
TOKEN = base64.b64encode(f"{USERNAME}:{PASSWORD}".encode()).decode()
@pytest.fixture(scope="module")
def client() -> AsyncFHIRClient:
return AsyncFHIRClient(
FHIR_SERVER_URL,
authorization=f"Basic {TOKEN}",
dump_resource=lambda x: x.model_dump(exclude_none=True),
)
@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"
await client.delete(f"Patient/{created.id}")
@pytest.mark.asyncio
async def test_search_patients(client: AsyncFHIRClient, created_patient: Patient) -> None:
"""Test client.resources(Patient).fetch() — requires resourceType class-level 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:
"""Test client.resources(Patient).search(family='FhirpyPatient').fetch()"""
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 that fetched resources are deserialized into our 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_wrong_resource_type() -> None:
json = """
{
"resourceType" : "Bundle",
"id" : "bundle-example",
"type" : "searchset",
"total" : 3,
"link" : [{
"relation" : "self",
"url" : "https://example.com/base/MedicationRequest?patient=347"
}],
"entry" : [{
"fullUrl" : "https://example.com/base/Patient/3123",
"resource" : {
"resourceType" : "Weird_Patient",
"id" : "3123"
},
"search" : {
"mode" : "match",
"score" : 1
}
}]
}
"""
with pytest.raises(ValidationError):
Bundle.from_json(json)
def test_wrong_fields() -> None:
json = """
{
"resourceType" : "Bundle",
"id" : "bundle-example",
"type" : "searchset",
"total" : 3,
"link" : [{
"relation" : "self",
"url" : "https://example.com/base/MedicationRequest?patient=347"
}],
"entry" : [{
"fullUrl" : "https://example.com/base/Patient/3123",
"resource" : {
"resourceType" : "Patient",
"id" : "3123",
"very_wrong_field" : "WRONG"
},
"search" : {
"mode" : "match",
"score" : 1
}
}]
}
"""
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
def test_resource_type_class_access() -> None:
"""Verify that resourceType is accessible at class level (needed for fhirpy search)"""
assert Patient.resourceType == "Patient"
assert Observation.resourceType == "Observation"
assert Bundle.resourceType == "Bundle"