def test_qr_query_exclude_default_params(): """If a parameter has not been explicitly given to a DICOMQuery, it should not pollute any DICOM query so should be ignored """ query = DICOMQuery(StudyInstanceUID="123") assert ( query.PatientName == "" ) # not passed in init, so default empty value assert ( "StudyInstanceUID" in query.as_dataset() ) # SID was passed so should exist assert "PatientName" not in query.as_dataset() # not passed, so ignored
def test_qr_query(): query = DICOMQuery( StudyInstanceUID="123", QueryRetrieveLevel=QueryLevels.SERIES, minStudyDate=datetime(year=2020, month=3, day=1), ) ds = query.as_dataset() assert ds.StudyInstanceUID == "123" assert ds.StudyDate == "20200301-" assert ds.QueryRetrieveLevel == QueryLevels.SERIES assert ( ds.SeriesInstanceUID == "" ) # should be added as default for QueryLevel
def test_qr_query_do_not_overwrite_parameters(): """If a parameter is passed in init and also in IncludeFields, don't overwrite the init value """ query = DICOMQuery( StudyInstanceUID="123", ProtocolName="foo", includeFields=["StudyInstanceUID", "ProtocolName"], ) assert query.StudyInstanceUID == "123" assert query.ProtocolName == "foo" ds = query.as_dataset() assert ds.StudyInstanceUID == "123" assert ds.ProtocolName == "foo"
def test_send_cfind(a_mock_ae_associate, monkeypatch): monkeypatch.setattr( "dicomtrolley.dicom_qr.AE.associate", Mock(return_value=a_mock_ae_associate), ) qr = DICOMQR(host="host", port=123) results = qr.send_c_find(query=DICOMQuery()) assert len(results) == 2 assert results[0].PatientName == "patient"
def test_send_cfind_no_connection(a_mock_ae_associate, monkeypatch): a_mock_ae_associate.is_established = False monkeypatch.setattr( "dicomtrolley.dicom_qr.AE.associate", Mock(return_value=a_mock_ae_associate), ) qr = DICOMQR(host="host", port=123) with pytest.raises(DICOMTrolleyError): qr.send_c_find(query=DICOMQuery())
def test_qr_query_exceptions(parameters): with pytest.raises(ValueError): DICOMQuery(**parameters)
def test_qr_query_allowed_parameters(parameters): """These should work without problems""" assert DICOMQuery(**parameters).as_dataset()
from dicomtrolley.dicom_qr import DICOMQR, DICOMQuery, QueryRetrieveLevels print("Setting up DICOM query-retrieve") dicom_qr = DICOMQR( host=environ["HOST"], port=int(environ["PORT"]), aet=environ["AET"], aec=environ["AEC"], ) print("Perform a search") studies = dicom_qr.find_studies( DICOMQuery( PatientName="BAL*", ProtocolName="Thorax", minStudyDate=datetime(year=2015, month=3, day=1), maxStudyDate=datetime(year=2015, month=4, day=1), includeFields=[ "PatientBirthDate", "SOPClassesInStudy", "Modality", "StudyDescription", "SeriesDescription", "ProtocolName", ], QueryRetrieveLevel=QueryRetrieveLevels.SERIES, )) print(f"Found {len(studies)} studies")