-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path3_assert.py
More file actions
432 lines (348 loc) · 14.2 KB
/
3_assert.py
File metadata and controls
432 lines (348 loc) · 14.2 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import json
from behave import * # noqa
from behave.runner import Context
from nrlf.producer.fhir.r4.model import Bundle, DocumentReference, OperationOutcome
def format_error(
message: str, expected: str | None, actual: str | None, response: str | None
):
message = f"{message}\n\nExpected: {expected}\nActual: {actual}"
if response:
message += f"\n\nResponse body:\n{response}"
return message
@then("the response status code is {status_code}")
def assert_response_status_code_step(context: Context, status_code: str):
assert context.response.status_code == int(status_code), format_error(
"Status code does not match",
status_code,
str(context.response.status_code),
context.response.text,
)
@then("the response does not contain the key '{key}'")
def assert_response_no_key_in_response_step(context: Context, key: str):
assert key not in context.response.json(), f"Key {key} found in response"
@then("the response is a {bundle_type} Bundle")
def assert_bundle_step(context: Context, bundle_type: str):
body = context.response.json()
assert body["resourceType"] == "Bundle", format_error(
"Unexpected resourceType",
"Bundle",
body["resourceType"],
context.response.text,
)
assert body["type"] == bundle_type, format_error(
"Unexpected type",
bundle_type,
body["type"],
context.response.text,
)
context.bundle = Bundle.model_validate(body)
@then("the response has an empty body")
def assert_empty_body_step(context: Context):
"""
Asserts that the response body is empty.
"""
assert context.response.text == "", format_error(
"Response body is not empty",
"empty",
context.response.text,
context.response.text,
)
context.bundle = None
@then("the Bundle has a total of {total}")
def assert_bundle_total_step(context: Context, total: str):
assert (
context.bundle is not None
), "The Bundle has not been yet parsed from the response"
assert context.bundle.total == int(total), format_error(
"Unexpected Bundle total",
total,
str(context.bundle.total),
context.response.text,
)
@then("the Bundle has a self link matching '{rel_url}'")
def assert_bundle_self(context: Context, rel_url: str):
assert (
context.bundle is not None
), "The Bundle has not yet been parsed from the response"
assert context.bundle.link is not None, format_error(
"No links present in the Bundle",
f"{context.base_url}{rel_url}",
"None",
context.response.text,
)
assert len(context.bundle.link) == 1, format_error(
"The Bundle's link array should contain a single item if no pagination is used",
"1 entry",
f"{len(context.bundle.link)} entries",
context.response.text,
)
link_entry = context.bundle.link[0].model_dump(exclude_none=True)
assert link_entry.get("relation") == "self", format_error(
"Link should specify a 'self' type relation",
"self",
link_entry.get("relation"),
context.response.text,
)
actual_url_params = link_entry.get("url").split("consumer/FHIR/R4/")[-1]
assert actual_url_params == rel_url, format_error(
"Link url does not specify the search parameters expected",
rel_url,
actual_url_params,
context.response.text,
)
@then("the Bundle has {num_entries} entry")
@then("the Bundle has {num_entries} entries")
def assert_bundle_entries_step(context: Context, num_entries: str):
assert (
context.bundle is not None
), "The Bundle has not been yet parsed from the response"
assert len(context.bundle.entry) == int(num_entries), format_error(
"Unexpected number of Bundle entries",
num_entries,
str(len(context.bundle.entry)),
context.response.text,
)
def assert_document_reference_matches_value(
context: Context, doc_ref: dict | DocumentReference, items: dict
):
"""
Asserts that the given document reference matches the expected values.
Args:
context (Context): The context object.
doc_ref (dict): The actual document reference.
items (dict): The expected values for the document reference.
Raises:
AssertionError: If any of the document reference values do not match the expected values.
"""
if isinstance(doc_ref, dict):
doc_ref = DocumentReference.model_validate(doc_ref)
assert doc_ref.id == items["id"], format_error(
"DocumentReference ID does not match",
items["id"],
doc_ref.id,
context.response.json(),
)
if status := items.get("status"):
assert doc_ref.status == status, format_error(
"DocumentReference status does not match",
status,
doc_ref.status,
context.response.json(),
)
if type_code := items.get("type"):
assert doc_ref.type.coding[0].code == type_code, format_error(
"DocumentReference type does not match",
type_code,
doc_ref.type.coding[0].code,
context.response.json(),
)
if type_system := items.get("type_system"):
assert doc_ref.type.coding[0].system == type_system, format_error(
"DocumentReference type does not match",
type_system,
doc_ref.type.coding[0].system,
context.response.json(),
)
if type_display := items.get("type_display"):
assert doc_ref.type.coding[0].display == type_display, format_error(
"DocumentReference type does not match",
type_display,
doc_ref.type.coding[0].display,
context.response.json(),
)
if category := items.get("category"):
assert doc_ref.category[0].coding[0].code == category, format_error(
"DocumentReference custodian does not match",
category,
doc_ref.category[0].coding[0].code,
context.response.json(),
)
if subject := items.get("subject"):
assert doc_ref.subject.identifier.value == subject, format_error(
"DocumentReference subject does not match",
subject,
doc_ref.subject.identifier.value,
context.response.json(),
)
if custodian := items.get("custodian"):
assert doc_ref.custodian.identifier.value == custodian, format_error(
"DocumentReference custodian does not match",
custodian,
doc_ref.custodian.identifier.value,
context.response.json(),
)
if author := items.get("author"):
assert doc_ref.author[0].identifier.value == author, format_error(
"DocumentReference author does not match",
author,
doc_ref.author[0].identifier.value,
context.response.json(),
)
if content_type := items.get("contentType"):
assert doc_ref.content[0].attachment.contentType == content_type, format_error(
"DocumentReference content type does not match",
content_type,
doc_ref.content[0].attachment.contentType,
context.response.json(),
)
if url := items.get("url"):
assert doc_ref.content[0].attachment.url == url, format_error(
"DocumentReference URL does not match",
url,
doc_ref.content[0].attachment.url,
context.response.json(),
)
if identifier := items.get("identifier"):
assert doc_ref.identifier[0].value == identifier, format_error(
"DocumentReference Identifier does not match",
identifier,
doc_ref.identifier[0].value,
context.response.json(),
)
@then("the Bundle contains an DocumentReference with values")
def assert_bundle_contains_documentreference_values_step(context: Context):
if not context.table:
raise ValueError("No DocumentReference table provided")
items = {row["property"]: row["value"] for row in context.table}
if not items.get("id"):
raise ValueError("No id provided in the table")
for entry in context.bundle.entry:
if entry.resource.id != items["id"]:
continue
return assert_document_reference_matches_value(context, entry.resource, items)
raise AssertionError(
f"DocumentReference with id {items['id']} not found in the response\n\nFull response:\n{json.dumps(context.response.json(), indent=2)}"
)
@then("the Bundle does not contain a DocumentReference with ID '{doc_ref_id}'")
def assert_bundle_does_not_contain_doc_ref_step(context: Context, doc_ref_id: str):
for entry in context.bundle.entry:
assert (
entry.resource.id != doc_ref_id
), f"DocumentReference with ID {doc_ref_id} found in the response"
@then("the response is an OperationOutcome with {num_issues} issue")
@then("the response is an OperationOutcome with {num_issues} issues")
def assert_response_operation_outcome_step(context: Context, num_issues: str):
body = context.response.json()
assert body["resourceType"] == "OperationOutcome"
assert len(body["issue"]) == int(num_issues)
context.operation_outcome = OperationOutcome.model_validate(body)
@then("the OperationOutcome contains the issue")
def assert_response_operation_outcome_issue(context: Context):
if not context.text:
raise ValueError("No issue JSON provided")
try:
content = json.loads(context.text)
except:
raise ValueError("Invalid JSON provided")
for issue in context.operation_outcome.issue:
if issue.model_dump(exclude_none=True) == content:
return
raise ValueError(
f"Could not find issue in response:\n\nFull Response:\n{json.dumps(context.response.json(), indent=2)}\n\nExpected Issue:\n{json.dumps(content, indent=2)}"
)
@then("the response is a DocumentReference with JSON value")
def assert_response_document_reference_step(context: Context):
if not context.text:
raise ValueError("No DocumentReference JSON provided")
try:
content = json.loads(context.text)
except:
raise ValueError("Invalid JSON provided")
assert context.response.json()["resourceType"] == "DocumentReference", format_error(
"Response is not a DocumentReference",
"DocumentReference",
context.response.json()["resourceType"],
context.response.text,
)
assert context.response.json() == content, format_error(
"DocumentReference does not match",
json.dumps(content, indent=2),
json.dumps(context.response.json(), indent=2),
context.response.text,
)
@then("the {header_name} header is '{header_value}'")
def assert_header(context: Context, header_name: str, header_value: str):
assert context.response.headers.get(header_name) == header_value, format_error(
f"Header {header_name} does not match",
header_value,
context.response.headers.get(header_name),
context.response.text,
)
@then("the response has a {header_name} header")
def assert_location_header(context: Context, header_name: str):
generated_id = context.response.headers.get(header_name)
# check there is a location header
assert generated_id is not None, format_error(
f"Missing {header_name} header",
None,
None,
context.response.text,
)
context.pointer_id = generated_id
@then("the {header_name} header is not present")
def assert_header_not_present(context: Context, header_name: str):
header_value = context.response.headers.get(header_name)
assert header_value is None, format_error(
f"Header {header_name} should not be present",
"not present",
header_value,
context.response.text,
)
@then("the {header_name} header starts with '{starts_with}'")
def assert_header_starts_with(context: Context, header_name: str, starts_with: str):
header_value = context.response.headers.get(header_name)
assert header_value.startswith(starts_with), format_error(
f"Header {header_name} does not start with expected value",
starts_with,
header_value,
context.response.text,
)
@then("the resource in the Location header exists with values")
def assert_resource_in_location_header_exists_with_values(context: Context):
location = context.response.headers.get("Location")
assert location.startswith("/DocumentReference/"), format_error(
"Unexpected Location header",
"/DocumentReference/",
location,
context.response.text,
)
resource_id = location.split("/")[-1]
resource_id.replace("|", ".") # NRL-766 define and verify custodian suffix formats
resource = context.repository.get_by_id(resource_id)
assert resource is not None, format_error(
"Resource does not exist",
resource_id,
None,
context.response.text,
)
if not context.table:
raise ValueError("No DocumentReference table provided")
items = {row["property"]: row["value"] for row in context.table}
items["id"] = resource_id
assert_document_reference_matches_value(
context, DocumentReference.model_validate_json(resource.document), items
)
@then("the Document Reference '{doc_ref_id}' exists with values")
def assert_resource_exists_with_values(context: Context, doc_ref_id: str):
resource = context.repository.get_by_id(doc_ref_id)
assert resource is not None, format_error(
"Resource does not exist",
doc_ref_id,
None,
context.response.text,
)
if not context.table:
raise ValueError("No DocumentReference table provided")
items = {row["property"]: row["value"] for row in context.table}
assert_document_reference_matches_value(
context, DocumentReference.model_validate_json(resource.document), items
)
@then("the resource with id '{doc_ref_id}' does not exist")
def assert_resource_absent(context: Context, doc_ref_id: str):
resource = context.repository.get_by_id(doc_ref_id)
assert resource is None, format_error(
"Resource that should be absent is found in database by id",
None,
doc_ref_id,
DocumentReference.model_validate_json(resource.document),
)