|
| 1 | +import pytest |
| 2 | + |
| 3 | +from models import WorklistItem |
| 4 | +from services.mwl.update_worklist_item_status import UpdateWorklistItemStatus |
| 5 | +from services.storage import MWLStorage |
| 6 | + |
| 7 | + |
| 8 | +class TestUpdateWorklistItemStatus: |
| 9 | + @pytest.fixture |
| 10 | + def worklist_item_data(self): |
| 11 | + return { |
| 12 | + "accession_number": "ACC123", |
| 13 | + "modality": "MG", |
| 14 | + "patient_birth_date": "19800101", |
| 15 | + "patient_id": "1234567890", |
| 16 | + "patient_name": "Jane^Doe", |
| 17 | + "scheduled_date": "20240101", |
| 18 | + "scheduled_time": "120000", |
| 19 | + "source_message_id": "action-12345", |
| 20 | + } |
| 21 | + |
| 22 | + @pytest.fixture |
| 23 | + def status_update_payload(self): |
| 24 | + return { |
| 25 | + "action_id": "action-12345", |
| 26 | + "action_type": "worklist.update_item_status", |
| 27 | + "parameters": {"worklist_item": {"accession_number": "ACC123", "status": "completed"}}, |
| 28 | + } |
| 29 | + |
| 30 | + @pytest.fixture |
| 31 | + def db_file(self, tmp_path) -> str: |
| 32 | + return f"{tmp_path}/test.db" |
| 33 | + |
| 34 | + @pytest.fixture |
| 35 | + def mwl_storage(self, db_file: str): |
| 36 | + return MWLStorage(db_file) |
| 37 | + |
| 38 | + def test_call_success(self, mwl_storage, worklist_item_data, status_update_payload): |
| 39 | + mwl_storage.store_worklist_item(WorklistItem(**worklist_item_data)) |
| 40 | + mwl_storage.update_status("ACC123", "IN PROGRESS") |
| 41 | + |
| 42 | + subject = UpdateWorklistItemStatus(mwl_storage) |
| 43 | + response = subject.call(status_update_payload) |
| 44 | + assert response == {"accession_number": "ACC123", "status": "COMPLETED"} |
| 45 | + |
| 46 | + def test_call_missing_keys(self, mwl_storage, status_update_payload): |
| 47 | + subject = UpdateWorklistItemStatus(mwl_storage) |
| 48 | + |
| 49 | + del status_update_payload["parameters"]["worklist_item"]["status"] |
| 50 | + |
| 51 | + response = subject.call(status_update_payload) |
| 52 | + assert response["status"] == "error" |
| 53 | + assert "Missing key" in response["message"] |
| 54 | + |
| 55 | + def test_call_nonexistent_item(self, mwl_storage, status_update_payload): |
| 56 | + subject = UpdateWorklistItemStatus(mwl_storage) |
| 57 | + |
| 58 | + response = subject.call(status_update_payload) |
| 59 | + assert response["status"] == "error" |
| 60 | + assert response["message"] == "Worklist item 'ACC123' not found" |
| 61 | + |
| 62 | + def test_call_invalid_status_transition(self, mwl_storage, worklist_item_data, status_update_payload): |
| 63 | + subject = UpdateWorklistItemStatus(mwl_storage) |
| 64 | + |
| 65 | + mwl_storage.store_worklist_item(WorklistItem(**worklist_item_data)) |
| 66 | + |
| 67 | + status_update_payload["parameters"]["worklist_item"]["status"] = "SCHEDULED" |
| 68 | + |
| 69 | + response = subject.call(status_update_payload) |
| 70 | + assert response["status"] == "error" |
| 71 | + assert response["message"] == "Cannot transition to 'SCHEDULED'" |
0 commit comments