|
| 1 | +from unittest import mock |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from backports.entry_points_selectable import entry_points |
| 6 | +from pytest_mock import MockerFixture |
| 7 | + |
| 8 | +from murfey.server.feedback import feedback_callback |
| 9 | + |
| 10 | +feedback_callback_params_matrix = ( |
| 11 | + # Murfey workflows currently present in pyproject.toml |
| 12 | + ("atlas_update",), |
| 13 | + ("clem.align_and_merge",), |
| 14 | + ("clem.process_raw_lifs",), |
| 15 | + ("clem.process_raw_tiffs",), |
| 16 | + ("clem.register_align_and_merge_result",), |
| 17 | + ("clem.register_preprocessing_result",), |
| 18 | + ("data_collection",), |
| 19 | + ("data_collection_group",), |
| 20 | + ("pato",), |
| 21 | + ("picked_particles",), |
| 22 | + ("picked_tomogram",), |
| 23 | + ("processing_job",), |
| 24 | + ("spa.flush_spa_preprocess",), |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("test_params", feedback_callback_params_matrix) |
| 29 | +def test_feedback_callback( |
| 30 | + mocker: MockerFixture, |
| 31 | + test_params: tuple[str], |
| 32 | +): |
| 33 | + """ |
| 34 | + Checks that feedback-callback loop works correctly for the entry points-based workflows |
| 35 | + """ |
| 36 | + |
| 37 | + # Unpack test params |
| 38 | + (entry_point_name,) = test_params |
| 39 | + |
| 40 | + # Mock the Murfey database creation in the main body of the module |
| 41 | + mock_get_security_config = mocker.patch( |
| 42 | + "murfey.server.feedback.get_security_config" |
| 43 | + ) |
| 44 | + mock_get_security_config.return_value = MagicMock() |
| 45 | + mock_url = mocker.patch("murfey.server.feedback.url") |
| 46 | + mock_url.return_value = mock.sentinel |
| 47 | + mock_create_engine = mocker.patch("murfey.server.feedback.create_engine") |
| 48 | + mock_create_engine.return_value = MagicMock() |
| 49 | + mock_murfey_db = MagicMock() |
| 50 | + mock_sql_session = mocker.patch("murfey.server.feedback.Session") |
| 51 | + mock_sql_session.return_value = mock_murfey_db |
| 52 | + |
| 53 | + # Load the entry point and patch the executable it calls |
| 54 | + eps = list(entry_points().select(group="murfey.workflows", name=entry_point_name)) |
| 55 | + assert len(eps) == 1 # Entry point should be present and unique |
| 56 | + mock_function = mocker.patch(eps[0].value.replace(":", ".")) |
| 57 | + |
| 58 | + header = {"dummy": "dummy"} |
| 59 | + message = {"register": entry_point_name} |
| 60 | + |
| 61 | + # Run the function and check that it calls the entry point correctly |
| 62 | + feedback_callback(header, message, mock_murfey_db) |
| 63 | + mock_function.assert_called_once_with(message=message, murfey_db=mock_murfey_db) |
0 commit comments