-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_support.py
269 lines (240 loc) · 8.05 KB
/
test_support.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import json
import os
from typing import Dict, List
import pytest
from linuxforhealth.csvtofhir.support import (find_fhir_resources, is_valid_year, read_csv,
validate_paths, parse_uri_scheme)
@pytest.fixture
def practitioner() -> str:
data = {
"id": "ABCDEF",
"identifier": [
{
"id": "RI.ABCDEF",
"system": "urn:id:hospb",
"type": {
"coding": [
{
"code": "RI",
"system": "http://terminology.hl7.org/CodeSystem/v2-0203"
}
],
"text": "Resource identifier"
},
"value": "ABCDEF"
}
],
"meta": {
"extension": [
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/tenant-id",
"valueString": "sample-tenant"
},
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/source-file-id",
"valueString": "testresults.csv"
},
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/source-event-trigger",
"valueCodeableConcept": {
"text": "Observation"
}
},
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/process-timestamp",
"valueDateTime": "2022-03-28T13:35:48.720714+00:00"
},
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/source-record-type",
"valueCodeableConcept": {
"text": "csv"
}
}
]
},
"resourceType": "Practitioner"
}
return json.dumps(data)
@pytest.fixture
def observation() -> str:
"""
:return: an Observation JSON FHIR resource
"""
data = {
"category": [
{
"coding": [
{
"code": "laboratory",
"display": "Laboratory",
"system": "http://terminology.hl7.org/CodeSystem/observation-category"
}
],
"text": "Laboratory"
}
],
"code": {
"coding": [
{
"code": "26499-4",
"system": "http://loinc.org"
}
],
"text": "#Neutrophils"
},
"effectiveDateTime": "2021-10-11T20:53:00+00:00",
"encounter": {
"reference": "Encounter/hospb-EZ100"
},
"id": "1648474548730029000.2fd8a5cb5f83411b81b29038d6ddcc0a",
"identifier": [
{
"id": "AN.hospb-EZ100",
"system": "urn:id:hospb",
"type": {
"coding": [
{
"code": "AN",
"system": "http://terminology.hl7.org/CodeSystem/v2-0203"
}
],
"text": "Account number"
},
"value": "hospb_EZ100"
}
],
"meta": {
"extension": [
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/tenant-id",
"valueString": "sample-tenant"
},
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/source-file-id",
"valueString": "testresults.csv"
},
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/source-event-trigger",
"valueCodeableConcept": {
"text": "Observation"
}
},
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/process-timestamp",
"valueDateTime": "2022-03-28T13:35:48.720714+00:00"
},
{
"url": "http://ibm.com/fhir/cdm/StructureDefinition/source-record-type",
"valueCodeableConcept": {
"text": "csv"
}
}
]
},
"performer": [
{
"reference": "Practitioner/ABCDEF"
}
],
"referenceRange": [
{
"high": {
"unit": "thou/uL",
"value": 6.5
},
"low": {
"unit": "thou/uL",
"value": 1.4
},
"text": "1.40-6.50"
}
],
"resourceType": "Observation",
"status": "final",
"subject": {
"reference": "Patient/hospb-EZ100"
},
"valueQuantity": {
"unit": "thou/uL",
"value": 7.9
}
}
return json.dumps(data)
@pytest.fixture
def converter_results(practitioner, observation) -> List[str]:
"""
csvtofhir.converter.convert result fixture containing a single patient result
:return: List of results
"""
return [practitioner, observation]
@pytest.mark.parametrize(
"resource_type,is_match,resource_count",
[("Practitioner", True, 1),
("practitioner", True, 1),
("Encounter", True, 0),
("Observation", False, 1),
("observation", False, 1)]
)
def test_find_fhir_resources(
converter_results, resource_type: str, is_match: bool, resource_count: int
):
"""
Validates find_fhir_resources to ensure the search is not case sensitive
"""
actual_result: List[Dict] = find_fhir_resources(converter_results, resource_type)
assert len(actual_result) == resource_count
dictionary_results: List = list(
filter(lambda x: isinstance(x, dict), actual_result)
)
assert len(actual_result) == len(dictionary_results)
resource_types: List[str] = [
d
for d in dictionary_results
if d.get("resourceType", "").lower() == resource_type.lower()
]
assert len(resource_types) == len(actual_result)
def test_read_csv(data_contract_directory):
"""
Tests the read_csv support function.
:param data_contract_directory: The data contract directory fixture
"""
filepath = os.path.join(data_contract_directory, "sex.csv")
csv_dict = read_csv(filepath)
assert csv_dict == {"default": "unknown", "M": "male", "F": "female", "O": "other"}
def test_is_valid_year():
"""
Validates an input string is a valid year and within range of a patient event (e.g. birth, death)s
"""
assert is_valid_year("1999")
assert not is_valid_year("1880")
assert not is_valid_year("2300")
assert not is_valid_year("19991")
assert not is_valid_year("1999abc")
assert not is_valid_year("99")
assert not is_valid_year("apple")
def test_validate_paths(data_contract_directory: str):
"""
Validates positive and negative outcomes with validate_paths
:param data_contract_directory: A path directory fixture used for a "valid" path
"""
paths = [data_contract_directory]
result: List[str] = validate_paths(paths, raise_exception=True)
assert len(result) == 0
result = validate_paths(paths, raise_exception=False)
assert len(result) == 0
paths.append("/tmp/bar/not-a-valid-path")
result = validate_paths(paths, raise_exception=False)
assert len(result) == 1
with pytest.raises(FileNotFoundError):
validate_paths(paths, raise_exception=True)
@pytest.mark.parametrize(
"input_uri, expected_uri",
[
("/opt/data/data.csv", "file"),
("C:/data/data.csv", "file"),
("http://someserver/file.dat", "http")
]
)
def test_parse_uri_scheme(input_uri, expected_uri):
actual_scheme = parse_uri_scheme(input_uri)
assert expected_uri == actual_scheme