|
| 1 | +from unittest.mock import MagicMock |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pytest_mock import MockerFixture |
| 5 | + |
| 6 | +from murfey.workflows.register_data_collection_group import run |
| 7 | +from tests.conftest import ExampleVisit |
| 8 | + |
| 9 | +register_data_collection_group_params_matrix = ( |
| 10 | + # ISPyB session ID | # DCG search result | # DCG insert result | # Atlas insert result |
| 11 | + (0, 0, 0, 0), |
| 12 | + (0, 0, 0, None), |
| 13 | + (0, 0, None, 0), |
| 14 | + (0, 0, None, None), |
| 15 | + (0, None, 0, 0), |
| 16 | + (0, None, 0, None), |
| 17 | + (0, None, None, 0), |
| 18 | + (0, None, None, None), |
| 19 | + (None, 0, 0, 0), |
| 20 | + (None, 0, 0, None), |
| 21 | + (None, 0, None, 0), |
| 22 | + (None, 0, None, None), |
| 23 | + (None, None, 0, 0), |
| 24 | + (None, None, 0, None), |
| 25 | + (None, None, None, 0), |
| 26 | + (None, None, None, None), |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize("test_params", register_data_collection_group_params_matrix) |
| 31 | +def test_run( |
| 32 | + mocker: MockerFixture, |
| 33 | + test_params: tuple[int | None, int | None, int | None, int | None], |
| 34 | +): |
| 35 | + # Unpack test params |
| 36 | + (ispyb_session_id, dcg_result, insert_dcg, insert_atlas) = test_params |
| 37 | + |
| 38 | + # Mock the transport object functions |
| 39 | + mock_transport_object = mocker.patch( |
| 40 | + "murfey.workflows.register_data_collection_group._transport_object" |
| 41 | + ) |
| 42 | + mock_transport_object.do_insert_data_collection_group.return_value = { |
| 43 | + "return_value": insert_dcg, |
| 44 | + } |
| 45 | + mock_transport_object.do_insert_atlas.return_value = {"return_value": insert_atlas} |
| 46 | + |
| 47 | + # Mock the 'get_session_id' return value |
| 48 | + mock_get_session_id = mocker.patch( |
| 49 | + "murfey.workflows.register_data_collection_group.get_session_id" |
| 50 | + ) |
| 51 | + mock_get_session_id.return_value = ispyb_session_id |
| 52 | + |
| 53 | + # Mock the Murfey database |
| 54 | + mock_murfey_db = MagicMock() |
| 55 | + mock_dcg = MagicMock() |
| 56 | + mock_dcg.id = dcg_result |
| 57 | + mock_murfey_db.exec.return_value.all.return_value = ( |
| 58 | + [mock_dcg] if dcg_result is not None else [] |
| 59 | + ) |
| 60 | + |
| 61 | + # Run the function and check the results and calls |
| 62 | + message = { |
| 63 | + "microscope": "test", |
| 64 | + "proposal_code": ExampleVisit.proposal_code, |
| 65 | + "proposal_number": ExampleVisit.proposal_number, |
| 66 | + "visit_number": ExampleVisit.visit_number, |
| 67 | + "session_id": ExampleVisit.murfey_session_id, |
| 68 | + "tag": "some_text", |
| 69 | + "experiment_type": "single particle", |
| 70 | + "experiment_type_id": 0, |
| 71 | + "atlas": "some_file", |
| 72 | + "atlas_pixel_size": 1e-9, |
| 73 | + "sample": 0, |
| 74 | + } |
| 75 | + result = run(message=message, murfey_db=mock_murfey_db) |
| 76 | + if dcg_result is not None: |
| 77 | + assert result == {"success": True} |
| 78 | + else: |
| 79 | + if ispyb_session_id is not None: |
| 80 | + mock_transport_object.do_insert_data_collection_group.assert_called_once() |
| 81 | + mock_transport_object.do_insert_atlas.assert_called_once() |
| 82 | + if insert_dcg is not None: |
| 83 | + assert result == {"success": True} |
| 84 | + else: |
| 85 | + assert result == {"success": False, "requeue": True} |
| 86 | + else: |
| 87 | + assert result == {"success": False, "requeue": True} |
0 commit comments