Skip to content

Commit 4aa0fc8

Browse files
committed
Add tests
1 parent 846fd72 commit 4aa0fc8

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

aidbox_python_sdk/handlers.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import asyncio
2+
import json
23
import logging
34
from typing import Any
45

56
from aiohttp import web
6-
from fhirpy.base.exceptions import OperationOutcome, BaseFHIRError
7+
from fhirpy.base.exceptions import BaseFHIRError, OperationOutcome
78

89
from . import app_keys as ak
910

@@ -53,7 +54,7 @@ async def operation(request: web.Request, data: dict[str, Any]):
5354
payload = json.loads(str(exc))
5455
return web.json_response(payload, status=422)
5556
except (json.JSONDecodeError, TypeError):
56-
return web.Response(text=str(exc), status=422, content_type='text/plain')
57+
return web.Response(text=str(exc), status=422, content_type="text/plain")
5758

5859

5960
TYPES = {
@@ -65,10 +66,10 @@ async def operation(request: web.Request, data: dict[str, Any]):
6566
@routes.post("/aidbox")
6667
async def dispatch(request):
6768
logger.debug("Dispatch new request %s %s", request.method, request.url)
68-
json = await request.json()
69-
if "type" in json and json["type"] in TYPES:
70-
logger.debug("Dispatch to `%s` handler", json["type"])
71-
return await TYPES[json["type"]](request, json)
69+
data = await request.json()
70+
if "type" in data and data["type"] in TYPES:
71+
logger.debug("Dispatch to `%s` handler", data["type"])
72+
return await TYPES[data["type"]](request, data)
7273
req = {
7374
"method": request.method,
7475
"url": str(request.url),

main.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import asyncio
2+
import json
23
import logging
34
from datetime import datetime
45

56
import coloredlogs
67
from aiohttp import web
8+
from fhirpy.base.exceptions import BaseFHIRError, OperationOutcome
79
from sqlalchemy.sql.expression import select
810

911
from aidbox_python_sdk.db import DBProxy
@@ -119,7 +121,7 @@ async def daily_patient_report(operation, request):
119121
GET /Patient/$weekly-report
120122
GET /Patient/$daily-report
121123
"""
122-
patients = request.app["client"].resources("Patient")
124+
patients = request["app"]["client"].resources("Patient")
123125
async for p in patients:
124126
logging.debug(p.serialize())
125127
logging.debug("`daily_patient_report` operation handler")
@@ -133,7 +135,7 @@ async def get_app_ids(db: DBProxy):
133135
return await db.alchemy(select(app.c.id))
134136

135137

136-
@routes.get("/db_tests")
138+
@routes.get("/db-tests")
137139
async def db_tests(request):
138140
db = request.app["db"]
139141

@@ -151,3 +153,42 @@ async def db_tests(request):
151153
)
152154
async def observation_custom_op(operation, request):
153155
return {"message": "Observation custom operation response"}
156+
157+
158+
@sdk.operation(
159+
["POST"],
160+
["$operation-outcome-test"],
161+
)
162+
async def operation_outcome_test_op(operation, request):
163+
raise OperationOutcome(reason="test reason")
164+
165+
166+
@sdk.operation(
167+
["POST"],
168+
["$base-fhir-error-json-test"],
169+
)
170+
async def base_fhir_error_json_test_op(operation, request):
171+
raise BaseFHIRError(
172+
json.dumps(
173+
{
174+
"resourceType": "OperationOutcome",
175+
"id": "not-found",
176+
"text": {"status": "generated", "div": "Resource Patient/id not found"},
177+
"issue": [
178+
{
179+
"severity": "fatal",
180+
"code": "not-found",
181+
"diagnostics": "Resource Patient/id not found",
182+
}
183+
],
184+
}
185+
)
186+
)
187+
188+
189+
@sdk.operation(
190+
["POST"],
191+
["$base-fhir-error-text-test"],
192+
)
193+
async def base_fhir_error_text_test_op(operation, request):
194+
raise BaseFHIRError("plain")

tests/test_sdk.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from fhirpathpy import evaluate
7+
from fhirpy.base.exceptions import OperationOutcome
78

89
import main
910
from aidbox_python_sdk.db import DBProxy
@@ -182,10 +183,28 @@ async def test_aidbox_db_fixture(client, aidbox_db: DBProxy, safe_db):
182183
"""
183184
Test that aidbox_db fixture works with isolated DB Proxy from app's instance
184185
"""
185-
response = await client.get("/db_tests")
186+
response = await client.get("/db-tests")
186187
assert response.status == 200
187188
json = await response.json()
188189
assert json == [{"id": "app-test"}]
189190

190191
app_ids = await main.get_app_ids(aidbox_db)
191192
assert app_ids == [{"id": "app-test"}]
193+
194+
195+
async def test_operation_base_fhir_error_json_test_op(aidbox_client):
196+
with pytest.raises(OperationOutcome) as exc:
197+
await aidbox_client.execute("/$base-fhir-error-json-test")
198+
assert exc.value.resource.get("issue")[0].get("diagnostics") == "Resource Patient/id not found"
199+
200+
201+
async def test_operation_base_fhir_error_text_test_op(aidbox_client):
202+
with pytest.raises(OperationOutcome) as exc:
203+
await aidbox_client.execute("/$base-fhir-error-text-test")
204+
assert exc.value.resource.get("issue")[0].get("diagnostics") == "plain"
205+
206+
207+
async def test_operation_outcome_test_op(aidbox_client):
208+
with pytest.raises(OperationOutcome) as exc:
209+
await aidbox_client.execute("/$operation-outcome-test")
210+
assert exc.value.resource.get("issue")[0].get("diagnostics") == "test reason"

0 commit comments

Comments
 (0)