|
| 1 | +from unittest.mock import MagicMock |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pytest_mock import MockerFixture |
| 5 | + |
| 6 | +from murfey.workflows.register_processing_job import run |
| 7 | + |
| 8 | +register_processing_job_params_matrix = [ |
| 9 | + # ISPyB session present | DC search result | PJ search result | APP search result | Insert ISPyB job | Update processing status |
| 10 | + (v0, v1, v2, v3, v4, v5) |
| 11 | + for v0 in (0, None) |
| 12 | + for v1 in (0, None) |
| 13 | + for v2 in (0, None) |
| 14 | + for v3 in (0, None) |
| 15 | + for v4 in (0, None) |
| 16 | + for v5 in (0, None) |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize("test_params", register_processing_job_params_matrix) |
| 21 | +def test_run( |
| 22 | + mocker: MockerFixture, |
| 23 | + test_params: tuple[ |
| 24 | + int | None, int | None, int | None, int | None, int | None, int | None |
| 25 | + ], |
| 26 | +): |
| 27 | + # Unpack test params |
| 28 | + ispyb_session, dc_result, pj_result, app_result, insert_job, update_status = ( |
| 29 | + test_params |
| 30 | + ) |
| 31 | + |
| 32 | + # Create mocks |
| 33 | + # Transport object functions |
| 34 | + mock_transport_object = mocker.patch( |
| 35 | + "murfey.workflows.register_processing_job._transport_object" |
| 36 | + ) |
| 37 | + mock_transport_object.do_create_ispyb_job.return_value = { |
| 38 | + "return_value": insert_job |
| 39 | + } |
| 40 | + mock_transport_object.do_update_processing_status.return_value = { |
| 41 | + "return_value": update_status |
| 42 | + } |
| 43 | + |
| 44 | + # ISPyB session |
| 45 | + mock_ispyb_session = mocker.patch( |
| 46 | + "murfey.workflows.register_processing_job.ISPyBSession" |
| 47 | + ) |
| 48 | + mock_ispyb_session.return_value = ispyb_session |
| 49 | + |
| 50 | + # Murfey database |
| 51 | + mock_murfey_dc = MagicMock() |
| 52 | + mock_murfey_dc.id = dc_result |
| 53 | + mock_murfey_pj = MagicMock() |
| 54 | + mock_murfey_pj.id = pj_result |
| 55 | + mock_murfey_app = MagicMock() |
| 56 | + mock_murfey_app.id = app_result |
| 57 | + |
| 58 | + # Set up side effects depending on route taken through the function |
| 59 | + db_call_order = [[[mock_murfey_dc]] if dc_result is not None else []] |
| 60 | + if dc_result is not None: |
| 61 | + db_call_order.append([mock_murfey_pj] if pj_result is not None else []) |
| 62 | + if pj_result is not None or insert_job is not None or ispyb_session is None: |
| 63 | + db_call_order.append([mock_murfey_app] if app_result is not None else []) |
| 64 | + mock_murfey_db = MagicMock() |
| 65 | + mock_murfey_db.exec.return_value.all.side_effect = db_call_order |
| 66 | + |
| 67 | + # Mock Prometheus object |
| 68 | + mock_prom = mocker.patch("murfey.workflows.register_processing_job.prom") |
| 69 | + |
| 70 | + # Run function and check results and calls |
| 71 | + message = { |
| 72 | + "session_id": 0, |
| 73 | + "source": "some_path", |
| 74 | + "tag": "some_tag", |
| 75 | + "recipe": "some_recipe", |
| 76 | + "parameters": {}, |
| 77 | + "job_parameters": {"dummy": "dummy"}, |
| 78 | + } |
| 79 | + result = run(message=message, murfey_db=mock_murfey_db) |
| 80 | + if dc_result is not None: |
| 81 | + if pj_result is not None: |
| 82 | + mock_prom.preprocessed_movies.labels.assert_called_once() |
| 83 | + if app_result is not None: |
| 84 | + assert {"success": True} |
| 85 | + else: |
| 86 | + if update_status is not None: |
| 87 | + assert result == {"success": True} |
| 88 | + else: |
| 89 | + if ispyb_session is not None: |
| 90 | + assert result == {"success": False, "requeue": True} |
| 91 | + else: |
| 92 | + assert result == {"success": True} |
| 93 | + else: |
| 94 | + if ispyb_session is not None: |
| 95 | + mock_transport_object.do_create_ispyb_job.assert_called_once() |
| 96 | + if insert_job is not None: |
| 97 | + if pj_result is not None: |
| 98 | + mock_prom.preporcessed_movies.labels.assert_called_once() |
| 99 | + if app_result is not None: |
| 100 | + if ispyb_session is not None: |
| 101 | + assert result == {"success": True} |
| 102 | + else: |
| 103 | + mock_transport_object.do_update_processing_status.assert_called_once() |
| 104 | + if update_status is not None: |
| 105 | + assert result == {"success": True} |
| 106 | + else: |
| 107 | + assert result == {"success": False, "requeue": True} |
| 108 | + else: |
| 109 | + assert {"success": True} |
| 110 | + else: |
| 111 | + assert result == {"success": False, "requeue": True} |
| 112 | + else: |
| 113 | + assert result == {"success": False, "requeue": True} |
| 114 | + else: |
| 115 | + assert result == {"success": False, "requeue": True} |
0 commit comments