Skip to content

Commit 0d6706a

Browse files
committed
Support updating worklist item status via relay connection
1 parent f43fc5f commit 0d6706a

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

src/relay_listener.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from environment import Environment
2323
from services.mwl.create_worklist_item import CreateWorklistItem
24+
from services.mwl.update_worklist_item_status import UpdateWorklistItemStatus
2425
from services.storage import MWLStorage
2526
from telemetry import configure_telemetry
2627

@@ -93,6 +94,8 @@ def process_action(self, payload: dict):
9394
return {"status": "echo", "payload": payload}
9495
elif action_name == "worklist.create_item":
9596
return CreateWorklistItem(self.storage).call(payload)
97+
elif action_name == "worklist.update_item_status":
98+
return UpdateWorklistItemStatus(self.storage).call(payload)
9699
else:
97100
raise ValueError(f"Unsupported action: {action_name}")
98101

tests/integration/test_relay_listener_processes_actions.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22

33
import pytest
44

5+
from models import WorklistItem
56
from relay_listener import RelayListener
67
from services.storage import MWLStorage
78

89

910
class TestRelayListenerProcessesActions:
11+
@pytest.fixture
12+
def update_payload(self):
13+
return {
14+
"action_id": "action-12345",
15+
"action_type": "worklist.update_item_status",
16+
"parameters": {"worklist_item": {"accession_number": "ACC999999", "status": "in progress"}},
17+
}
18+
1019
@pytest.mark.asyncio
1120
async def test_relay_listener_creates_worklist_items(self, listener_payload, tmp_dir, fake_relay):
1221
storage = MWLStorage(f"{tmp_dir}/test_worklist.db")
@@ -22,3 +31,34 @@ async def test_relay_listener_creates_worklist_items(self, listener_payload, tmp
2231
item = stored_items[0]
2332
assert item.accession_number == "ACC999999"
2433
assert item.patient_id == "999123456"
34+
35+
@pytest.mark.asyncio
36+
async def test_relay_listener_updates_worklist_item_status(self, update_payload, tmp_dir, fake_relay):
37+
storage = MWLStorage(f"{tmp_dir}/test_worklist.db")
38+
listener = RelayListener(storage)
39+
relay_message = json.dumps({"accept": {"address": "wss://accept-url"}})
40+
41+
storage.store_worklist_item(
42+
WorklistItem(**{
43+
"accession_number": "ACC999999",
44+
"patient_id": "999123456",
45+
"patient_name": "Test^Patient",
46+
"patient_birth_date": "19900101",
47+
"patient_sex": "F",
48+
"scheduled_date": "20240101",
49+
"scheduled_time": "090000",
50+
"modality": "MG",
51+
"study_description": "Mammogram",
52+
"source_message_id": "action-12345",
53+
})
54+
)
55+
56+
with fake_relay(relay_message, json.dumps(update_payload)) as ws_client:
57+
await listener.listen()
58+
59+
ws_client.send.assert_called_once_with(json.dumps({"accession_number": "ACC999999", "status": "IN PROGRESS"}))
60+
stored_items = storage.find_worklist_items()
61+
assert len(stored_items) == 1
62+
item = stored_items[0]
63+
assert item.accession_number == "ACC999999"
64+
assert item.patient_id == "999123456"

tests/test_relay_listener.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def test_relay_listener_listen(self, storage_instance, listener_payload, f
7272
)
7373
)
7474

75-
def test_process_action(self, storage_instance, listener_payload):
75+
def test_process_create_item_action(self, storage_instance, listener_payload):
7676
subject = RelayListener(storage_instance)
7777

7878
response = subject.process_action(listener_payload)
@@ -93,6 +93,33 @@ def test_process_action(self, storage_instance, listener_payload):
9393
)
9494
)
9595

96+
def test_process_update_item_status_action(self, storage_instance, listener_payload):
97+
subject = RelayListener(storage_instance)
98+
99+
subject.process_action(listener_payload)
100+
101+
update_payload = {
102+
"action_id": "action-12345",
103+
"action_type": "worklist.update_item_status",
104+
"parameters": {"worklist_item": {"accession_number": "ACC999999", "status": "IN PROGRESS"}},
105+
}
106+
107+
response = subject.process_action(update_payload)
108+
assert response == {"accession_number": "ACC999999", "status": "IN PROGRESS"}
109+
110+
storage_instance.update_status.assert_called_once_with("ACC999999", "IN PROGRESS")
111+
112+
def test_process_action_missing_keys(self, storage_instance, listener_payload):
113+
subject = RelayListener(storage_instance)
114+
115+
del listener_payload["parameters"]["worklist_item"]["accession_number"]
116+
117+
response = subject.process_action(listener_payload)
118+
assert response["status"] == "error"
119+
assert "Missing key" in response["message"]
120+
121+
storage_instance.store_worklist_item.assert_not_called()
122+
96123
def test_process_action_invalid_type(self, storage_instance, listener_payload):
97124
subject = RelayListener(storage_instance)
98125

0 commit comments

Comments
 (0)