-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_sdk.py
More file actions
198 lines (163 loc) · 6.17 KB
/
test_sdk.py
File metadata and controls
198 lines (163 loc) · 6.17 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
import asyncio
import logging
from unittest import mock
import pytest
from fhirpathpy import evaluate
from fhirpy.base.exceptions import OperationOutcome
import main
from aidbox_python_sdk.db import DBProxy
@pytest.mark.skip("Skipped because of regression in Aidbox 2510")
@pytest.mark.asyncio
@pytest.mark.parametrize(
("expression", "expected"),
[
(
"CapabilityStatement.rest.operation.where(definition='http://test.com').count()",
1,
),
(
"CapabilityStatement.rest.operation.where(definition='http://test.com').first().name",
"observation-custom-op",
),
(
"CapabilityStatement.rest.resource.where(type='Observation').operation.where(definition='http://test.com').count()",
1,
),
(
"CapabilityStatement.rest.resource.where(type='Observation').operation.where(definition='http://test.com').first().name",
"observation-custom-op",
),
],
)
async def test_operation_with_compliance_params(aidbox_client, expression, expected):
response = await aidbox_client.execute("fhir/metadata", method="GET")
assert evaluate(response, expression, {})[0] == expected
@pytest.mark.asyncio
async def test_health_check(client):
resp = await client.get("/health")
assert resp.status == 200
json = await resp.json()
assert json == {"status": "OK"}
@pytest.mark.asyncio
async def test_live_health_check(client):
resp = await client.get("/live")
assert resp.status == 200
json = await resp.json()
assert json == {"status": "OK"}
@pytest.mark.asyncio
async def test_signup_reg_op(aidbox_client):
json = await aidbox_client.execute("signup/register/21.02.19/testvalue")
assert json == {
"success": "Ok",
"request": {"date": "21.02.19", "test": "testvalue"},
}
@pytest.mark.asyncio
async def test_appointment_sub(sdk, aidbox_client, safe_db):
with mock.patch.object(main, "_appointment_sub") as appointment_sub:
f = asyncio.Future()
f.set_result("")
appointment_sub.return_value = f
was_appointment_sub_triggered = sdk.was_subscription_triggered("Appointment")
resource = aidbox_client.resource(
"Appointment",
**{
"status": "proposed",
"participant": [{"status": "accepted"}],
},
)
await resource.save()
await was_appointment_sub_triggered
event = appointment_sub.call_args_list[0][0][0]
logging.debug("event: %s", event)
expected = {
"resource": {
"status": "proposed",
"participant": [{"status": "accepted"}],
"resourceType": "Appointment",
},
"action": "create",
}
assert expected["resource"].items() <= event["resource"].items()
@pytest.mark.asyncio
async def test_database_isolation__1(aidbox_client, safe_db):
patients = await aidbox_client.resources("Patient").fetch_all()
assert len(patients) == 2
patient = aidbox_client.resource("Patient")
await patient.save()
patients = await aidbox_client.resources("Patient").fetch_all()
assert len(patients) == 3
@pytest.mark.asyncio
async def test_database_isolation__2(aidbox_client, safe_db):
patients = await aidbox_client.resources("Patient").fetch_all()
assert len(patients) == 2
patient = aidbox_client.resource("Patient")
await patient.save()
patient = aidbox_client.resource("Patient")
await patient.save()
patients = await aidbox_client.resources("Patient").fetch_all()
assert len(patients) == 4
@pytest.mark.asyncio
async def test_database_isolation_with_history_in_name__1(aidbox_client, safe_db):
resources = await aidbox_client.resources("FamilyMemberHistory").fetch_all()
assert len(resources) == 0
resource = aidbox_client.resource(
"FamilyMemberHistory",
status="completed",
patient={
"identifier": {"system": "http://example.org/test-patients", "value": "test-patient-1"}
},
relationship={
"coding": [
{"system": "http://terminology.hl7.org/CodeSystem/v3-RoleCode", "code": "FTH"}
]
},
)
await resource.save()
resources = await aidbox_client.resources("FamilyMemberHistory").fetch_all()
assert len(resources) == 1
@pytest.mark.asyncio
async def test_database_isolation_with_history_in_name__2(aidbox_client, safe_db):
resources = await aidbox_client.resources("FamilyMemberHistory").fetch_all()
assert len(resources) == 0
resource1 = aidbox_client.resource(
"FamilyMemberHistory",
status="completed",
patient={
"identifier": {"system": "http://example.org/test-patients", "value": "test-patient-1"}
},
relationship={
"coding": [
{"system": "http://terminology.hl7.org/CodeSystem/v3-RoleCode", "code": "FTH"}
]
},
)
await resource1.save()
resource2 = aidbox_client.resource(
"FamilyMemberHistory",
status="completed",
patient={
"identifier": {"system": "http://example.org/test-patients", "value": "test-patient-2"}
},
relationship={
"coding": [
{"system": "http://terminology.hl7.org/CodeSystem/v3-RoleCode", "code": "MTH"}
]
},
)
await resource2.save()
resources = await aidbox_client.resources("FamilyMemberHistory").fetch_all()
assert len(resources) == 2
async def test_aidbox_db_fixture(client, aidbox_db: DBProxy, safe_db):
"""
Test that aidbox_db fixture works with isolated DB Proxy from app's instance
"""
response = await client.get("/db-tests")
assert response.status == 200
json = await response.json()
assert json == [{"id": "app-test"}]
app_ids = await main.get_app_ids(aidbox_db)
assert app_ids == [{"id": "app-test"}]
async def test_operation_outcome_test_op(aidbox_client):
with pytest.raises(OperationOutcome) as exc:
await aidbox_client.execute("/$operation-outcome-test")
assert exc.value.resource.get("issue")[0].get("diagnostics") == "test reason"