-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_bundle.py
More file actions
78 lines (65 loc) · 2.29 KB
/
Copy pathtest_bundle.py
File metadata and controls
78 lines (65 loc) · 2.29 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
import pytest
from pydantic import ValidationError
from fhir_types.hl7_fhir_r4_core.base import CodeableConcept
from fhir_types.hl7_fhir_r4_core.bundle import Bundle, BundleEntry
from fhir_types.hl7_fhir_r4_core.observation import Observation
from fhir_types.hl7_fhir_r4_core.patient import Patient
def test_bundle_generic_narrows_entry_resources() -> None:
patient = Patient(id="p-1")
observation = Observation(id="obs-1", status="final", code=CodeableConcept())
bundle: Bundle[Patient | Observation] = Bundle(
type="transaction",
entry=[
BundleEntry(resource=patient),
BundleEntry(resource=observation),
],
)
observations = [
e.resource
for e in (bundle.entry or [])
if e.resource and e.resource.resourceType == "Observation"
]
assert len(observations) == 1
assert observations[0].id == "obs-1"
def test_bundle_entry_generic_narrows_resource() -> None:
patient = Patient(id="p-1")
entry: BundleEntry[Patient] = BundleEntry(resource=patient)
resource = entry.resource
assert resource is not None
assert resource.resourceType == "Patient"
def test_bundle_without_type_param_is_backwards_compatible() -> None:
patient = Patient(id="p-1")
bundle: Bundle = Bundle(
type="collection",
entry=[BundleEntry(resource=patient)],
)
entry = bundle.entry
assert entry is not None
assert len(entry) == 1
def test_bundle_from_json_raises_on_invalid_resource() -> None:
# Observation requires `status` and `code` — omitting them causes a runtime ValidationError
bundle_json = """{
"resourceType": "Bundle",
"type": "searchset",
"entry": [{
"resource": {
"resourceType": "Observation",
"id": "obs-1"
}
}]
}"""
with pytest.raises(ValidationError):
Bundle.from_json(bundle_json)
def test_bundle_from_json_raises_on_wrong_typed_resource() -> None:
bundle_json = """{
"resourceType": "Bundle",
"type": "searchset",
"entry": [{
"resource": {
"resourceType": "Patient",
"id": "pt-1"
}
}]
}"""
with pytest.raises(ValidationError):
Bundle[Observation].from_json(bundle_json)