|
| 1 | +import datetime |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pydicom.dataset import Dataset |
| 5 | +from pydicom.uid import generate_uid |
| 6 | +from pynetdicom import AE |
| 7 | +from pynetdicom.sop_class import ModalityPerformedProcedureStep # pyright: ignore[reportAttributeAccessIssue] |
| 8 | + |
| 9 | +from server import MWLServer |
| 10 | +from services.dicom import SUCCESS |
| 11 | +from services.storage import MWLStorage, WorklistItem |
| 12 | + |
| 13 | + |
| 14 | +class TestNCreateUpdatesWorklistStatus: |
| 15 | + @pytest.fixture(autouse=True) |
| 16 | + def with_mwl_server(self, tmp_dir): |
| 17 | + server = MWLServer("SCREENING_MWL", 4243, f"{tmp_dir}/test.db", block=False) |
| 18 | + server.start() |
| 19 | + |
| 20 | + yield |
| 21 | + |
| 22 | + server.stop() |
| 23 | + |
| 24 | + @pytest.fixture |
| 25 | + def worklist_item(self): |
| 26 | + return WorklistItem( |
| 27 | + accession_number="ACC123", |
| 28 | + patient_id="999123456", |
| 29 | + patient_name="SMITH^JANE", |
| 30 | + patient_birth_date="19800101", |
| 31 | + patient_sex="F", |
| 32 | + scheduled_date="20240101", |
| 33 | + scheduled_time="090000", |
| 34 | + modality="MG", |
| 35 | + procedure_code="12345-6", |
| 36 | + study_description="MAMMOGRAPHY SCREENING", |
| 37 | + study_instance_uid=generate_uid(), |
| 38 | + source_message_id="MSGID123456", |
| 39 | + ) |
| 40 | + |
| 41 | + def test_n_create_updates_worklist_status(self, tmp_dir, worklist_item): |
| 42 | + storage = MWLStorage(f"{tmp_dir}/test.db") |
| 43 | + study_instance_uid = generate_uid() |
| 44 | + accession_number = storage.store_worklist_item(worklist_item) |
| 45 | + |
| 46 | + ae = AE(ae_title="MODALITY_SCU") |
| 47 | + ae.add_requested_context(ModalityPerformedProcedureStep) |
| 48 | + |
| 49 | + assoc = ae.associate("localhost", 4243, ae_title="SCREENING_MWL") |
| 50 | + |
| 51 | + mpps_ds = Dataset() |
| 52 | + mpps_instance_uid = generate_uid() |
| 53 | + mpps_ds.SOPClassUID = ModalityPerformedProcedureStep |
| 54 | + mpps_ds.SOPInstanceUID = mpps_instance_uid |
| 55 | + mpps_ds.PerformedProcedureStepStatus = "IN PROGRESS" |
| 56 | + now = datetime.datetime.now() |
| 57 | + mpps_ds.PerformedProcedureStepStartDate = now.strftime("%Y%m%d") |
| 58 | + mpps_ds.PerformedProcedureStepStartTime = now.strftime("%H%M%S") |
| 59 | + mpps_ds.Modality = "MG" |
| 60 | + |
| 61 | + scheduled_step_seq = Dataset() |
| 62 | + scheduled_step_seq.StudyInstanceUID = study_instance_uid |
| 63 | + scheduled_step_seq.AccessionNumber = accession_number |
| 64 | + |
| 65 | + mpps_ds.ScheduledStepAttributesSequence = [scheduled_step_seq] |
| 66 | + mpps_ds.PerformedSeriesSequence = [] |
| 67 | + |
| 68 | + response = assoc.send_n_create(mpps_ds, ModalityPerformedProcedureStep, mpps_instance_uid) |
| 69 | + |
| 70 | + assert response[0].Status == SUCCESS |
| 71 | + |
| 72 | + updated_item = storage.get_worklist_item(accession_number) |
| 73 | + |
| 74 | + assert updated_item is not None |
| 75 | + assert updated_item.mpps_instance_uid == mpps_instance_uid |
| 76 | + assert updated_item.status == "IN PROGRESS" |
0 commit comments