-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_raw_extension.py
More file actions
164 lines (136 loc) · 5.34 KB
/
test_raw_extension.py
File metadata and controls
164 lines (136 loc) · 5.34 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
"""
FHIR R4 Extension Demo Test
Mirrors examples/typescript-r4/raw-extension.test.ts for the Python generator.
"""
import json
from pathlib import Path
from fhir_types.hl7_fhir_r4_core import (
Address,
ContactPoint,
Element,
Extension,
HumanName,
)
from fhir_types.hl7_fhir_r4_core.patient import Patient, PatientContact
def create_patient_with_extensions() -> Patient:
name = HumanName(
extension=[
Extension(
url="http://example.org/fhir/StructureDefinition/name-verified",
value_boolean=True,
)
],
family="van Beethoven",
family_extension=Element(
extension=[
Extension(
url="http://hl7.org/fhir/StructureDefinition/humanname-own-prefix",
value_string="van",
),
],
),
given=["Ludwig", "Maria", "Johann"],
given_extension=[
Element(
extension=[
Extension(
url="http://example.org/fhir/StructureDefinition/name-source",
value_code="birth-certificate",
),
],
),
None,
Element(
extension=[
Extension(
url="http://example.org/fhir/StructureDefinition/name-source",
value_code="baptism-record",
),
],
),
],
)
contact = PatientContact(
extension=[
Extension(
url="http://example.org/fhir/StructureDefinition/contact-priority",
value_integer=1,
)
],
name=HumanName(family="Watson", given=["John"]),
telecom=[ContactPoint(system="phone", value="+44-20-7946-1234")],
)
return Patient(
id="ext-demo",
extension=[
Extension(
url="http://hl7.org/fhir/StructureDefinition/patient-birthPlace",
value_address=Address(city="Springfield", country="US"),
),
],
modifier_extension=[
Extension(
url="http://example.org/fhir/StructureDefinition/do-not-contact",
value_boolean=False,
),
],
birth_date="1990-03-15",
birth_date_extension=Element(
extension=[
Extension(
url="http://hl7.org/fhir/StructureDefinition/patient-birthTime",
value_date_time="1990-03-15T08:22:00-05:00",
),
],
),
name=[name],
contact=[contact],
)
SNAPSHOT_DIR = Path(__file__).parent / "__snapshots__"
def test_patient_with_extensions() -> None:
patient = create_patient_with_extensions()
actual = json.loads(patient.to_json(indent=2))
expected = json.loads((SNAPSHOT_DIR / "patient_with_extensions.json").read_text())
assert actual == expected
def test_read_resource_level_extension() -> None:
patient = create_patient_with_extensions()
assert patient.extension is not None
assert patient.extension[0].url == "http://hl7.org/fhir/StructureDefinition/patient-birthPlace"
assert patient.extension[0].value_address is not None
assert patient.extension[0].value_address.city == "Springfield"
assert patient.modifier_extension is not None
assert patient.modifier_extension[0].value_boolean is False
def test_read_element_level_extension() -> None:
patient = create_patient_with_extensions()
assert patient.name is not None
name = patient.name[0]
assert name.extension is not None
assert name.extension[0].url == "http://example.org/fhir/StructureDefinition/name-verified"
assert name.extension[0].value_boolean is True
assert patient.contact is not None
contact = patient.contact[0]
assert contact.extension is not None
assert contact.extension[0].value_integer == 1
def test_read_primitive_extension() -> None:
patient = create_patient_with_extensions()
name = patient.name[0]
assert isinstance(name.family_extension, Element)
assert name.family_extension.extension[0].value_string == "van"
assert isinstance(name.given_extension, list)
assert name.given_extension[0].extension[0].value_code == "birth-certificate"
assert name.given_extension[1] is None
assert name.given_extension[2].extension[0].value_code == "baptism-record"
assert patient.birth_date_extension is not None
assert isinstance(patient.birth_date_extension, Element)
assert patient.birth_date_extension.extension[0].value_date_time == "1990-03-15T08:22:00-05:00"
def test_primitive_extension_survives_round_trip() -> None:
"""After serialize → deserialize, typed _extension fields come back as Element instances."""
patient = create_patient_with_extensions()
restored = Patient.from_json(patient.to_json())
assert restored.birth_date == "1990-03-15"
assert restored.extension is not None
assert restored.extension[0].value_address is not None
assert restored.extension[0].value_address.city == "Springfield"
assert restored.birth_date_extension is not None
assert isinstance(restored.birth_date_extension, Element)
assert restored.birth_date_extension.extension[0].value_date_time == "1990-03-15T08:22:00-05:00"