|
1 | 1 | from unittest.mock import patch |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
3 | 5 | from services.mwl.create_worklist_item import CreateWorklistItem |
4 | | -from services.storage import DuplicateWorklistItemError, WorklistItem |
| 6 | +from services.storage import MWLStorage |
5 | 7 |
|
6 | 8 |
|
7 | | -@patch(f"{CreateWorklistItem.__module__}.MWLStorage") |
8 | 9 | class TestCreateWorklistItem: |
9 | | - def test_call_success(self, mock_mwl_storage, listener_payload): |
10 | | - mock_storage_instance = mock_mwl_storage.return_value |
11 | | - subject = CreateWorklistItem(mock_storage_instance) |
| 10 | + @pytest.fixture |
| 11 | + def db_file(tmp_path): |
| 12 | + return f"{tmp_path}/test.db" |
| 13 | + |
| 14 | + @pytest.fixture |
| 15 | + def mwl_storage(db_file): |
| 16 | + return MWLStorage(str(db_file)) |
| 17 | + |
| 18 | + def test_call_success(self, mwl_storage, listener_payload): |
| 19 | + subject = CreateWorklistItem(mwl_storage) |
12 | 20 |
|
13 | 21 | response = subject.call(listener_payload) |
14 | 22 | assert response == {"action_id": "action-12345", "status": "created"} |
15 | 23 |
|
16 | | - mock_storage_instance.store_worklist_item.assert_called_once_with( |
17 | | - WorklistItem( |
18 | | - accession_number="ACC999999", |
19 | | - patient_id="999123456", |
20 | | - patient_name="SMITH^JANE", |
21 | | - patient_birth_date="19900202", |
22 | | - patient_sex="F", |
23 | | - scheduled_date="20240615", |
24 | | - scheduled_time="101500", |
25 | | - modality="MG", |
26 | | - study_description="MAMMOGRAPHY", |
27 | | - source_message_id="action-12345", |
28 | | - ) |
29 | | - ) |
30 | | - |
31 | | - def test_call_missing_action_id(self, mock_mwl_storage, listener_payload): |
32 | | - mock_storage_instance = mock_mwl_storage.return_value |
33 | | - subject = CreateWorklistItem(mock_storage_instance) |
| 24 | + def test_call_missing_action_id(self, mwl_storage, listener_payload): |
| 25 | + subject = CreateWorklistItem(mwl_storage) |
34 | 26 |
|
35 | 27 | del listener_payload["action_id"] |
36 | 28 |
|
37 | 29 | response = subject.call(listener_payload) |
38 | 30 | assert response["status"] == "error" |
39 | 31 | assert "Missing action_id" in response["error"] |
40 | 32 |
|
41 | | - mock_storage_instance.store_worklist_item.assert_not_called() |
| 33 | + def test_call_existing_worklist_item(self, mwl_storage, listener_payload): |
| 34 | + CreateWorklistItem(mwl_storage).call(listener_payload) |
42 | 35 |
|
43 | | - def test_call_duplicate_worklist_item(self, mock_mwl_storage, listener_payload): |
44 | | - mock_storage_instance = mock_mwl_storage.return_value |
45 | | - mock_storage_instance.store_worklist_item.side_effect = DuplicateWorklistItemError( |
46 | | - "Worklist item already exists: ACC999999" |
47 | | - ) |
48 | | - subject = CreateWorklistItem(mock_storage_instance) |
| 36 | + subject = CreateWorklistItem(mwl_storage) |
49 | 37 |
|
50 | 38 | response = subject.call(listener_payload) |
51 | | - assert response == {"status": "duplicate", "action_id": "action-12345"} |
52 | | - |
53 | | - mock_storage_instance.store_worklist_item.assert_called_once() |
| 39 | + assert response == {"status": "exists", "action_id": "action-12345"} |
54 | 40 |
|
55 | | - def test_call_storage_exception(self, mock_mwl_storage, listener_payload): |
56 | | - mock_storage_instance = mock_mwl_storage.return_value |
57 | | - mock_storage_instance.store_worklist_item.side_effect = Exception("DB error") |
58 | | - subject = CreateWorklistItem(mock_storage_instance) |
| 41 | + @patch(f"{CreateWorklistItem.__module__}.MWLStorage.store_worklist_item", side_effect=Exception("DB error")) |
| 42 | + def test_call_storage_exception(self, _, mwl_storage, listener_payload): |
| 43 | + subject = CreateWorklistItem(mwl_storage) |
59 | 44 |
|
60 | 45 | response = subject.call(listener_payload) |
61 | 46 | assert response["status"] == "error" |
62 | 47 | assert "DB error" in response["error"] |
63 | | - |
64 | | - mock_storage_instance.store_worklist_item.assert_called_once() |
0 commit comments