|
5 | 5 | import pytest |
6 | 6 | from pytest_mock import MockerFixture |
7 | 7 |
|
| 8 | +import murfey.util.db as MurfeyDB |
| 9 | +from murfey.server.run import _set_up_transport |
8 | 10 | from murfey.workflows.clem.register_preprocessing_results import ( |
9 | 11 | _register_clem_image_series, |
10 | 12 | _register_dcg_and_atlas, |
11 | 13 | _register_grid_square, |
12 | 14 | run, |
13 | 15 | ) |
14 | | -from tests.conftest import ExampleVisit |
| 16 | +from tests.conftest import ExampleVisit, get_or_create_db_entry |
15 | 17 |
|
16 | 18 | visit_name = f"{ExampleVisit.proposal_code}{ExampleVisit.proposal_number}-{ExampleVisit.visit_number}" |
17 | 19 | processed_dir_name = "processed" |
|
20 | 22 |
|
21 | 23 |
|
22 | 24 | @pytest.fixture |
23 | | -def preprocessing_messages(tmp_path: Path): |
| 25 | +def rsync_basepath(tmp_path: Path): |
| 26 | + return tmp_path / "data" |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def preprocessing_messages(rsync_basepath: Path): |
24 | 31 | # Make directory to where data for current grid is stored |
25 | | - visit_dir = tmp_path / "data" / "2020" / visit_name |
| 32 | + visit_dir = rsync_basepath / "2020" / visit_name |
26 | 33 | processed_dir = visit_dir / processed_dir_name |
27 | 34 | grid_dir = processed_dir / grid_name |
28 | 35 | grid_dir.mkdir(parents=True, exist_ok=True) |
@@ -147,4 +154,66 @@ def test_run( |
147 | 154 | assert mock_align_and_merge_call.call_count == len(preprocessing_messages) * len( |
148 | 155 | colors |
149 | 156 | ) |
150 | | - assert run |
| 157 | + |
| 158 | + |
| 159 | +def test_run_with_db( |
| 160 | + mocker: MockerFixture, |
| 161 | + preprocessing_messages: list[dict[str, Any]], |
| 162 | + rsync_basepath: Path, |
| 163 | + murfey_db_session, |
| 164 | + seed_murfey_db, |
| 165 | + ispyb_db_session_factory, |
| 166 | + ispyb_db_session, |
| 167 | + seed_ispyb_db, |
| 168 | +): |
| 169 | + # Mock out module-level functions |
| 170 | + mock_get_security_config = mocker.patch("murfey.server.ispyb.get_security_config") |
| 171 | + mock_get_security_config.return_value = MagicMock() |
| 172 | + mock_url = mocker.patch("murfey.server.ispyb.url") |
| 173 | + mock_url.return_value = MagicMock() |
| 174 | + mock_create_engine = mocker.patch("murfey.server.ispyb.create_engine") |
| 175 | + mock_create_engine.return_value = MagicMock() |
| 176 | + |
| 177 | + # Mock out the machine config used in the helper sanitisation function |
| 178 | + mock_get_machine_config = mocker.patch("murfey.workflows.clem.get_machine_config") |
| 179 | + mock_machine_config = MagicMock() |
| 180 | + mock_machine_config.rsync_basepath = rsync_basepath |
| 181 | + mock_get_machine_config.return_value = { |
| 182 | + ExampleVisit.instrument_name: mock_machine_config, |
| 183 | + } |
| 184 | + # Mock the ISPyB connection |
| 185 | + mock_ispyb_sessionmaker = mocker.patch("murfey.server.ispyb.sessionmaker") |
| 186 | + mock_ispyb_sessionmaker.return_value = ispyb_db_session_factory |
| 187 | + mock_ispyb_sessionmaker.return_value.return_value = ispyb_db_session |
| 188 | + |
| 189 | + # Populate the Murfey database |
| 190 | + murfey_session_entry: MurfeyDB.Session = get_or_create_db_entry( |
| 191 | + murfey_db_session, |
| 192 | + MurfeyDB.Session, |
| 193 | + lookup_kwargs={"id": ExampleVisit.murfey_session_id}, |
| 194 | + ) |
| 195 | + murfey_session_entry.visit = visit_name |
| 196 | + murfey_session_entry.name = visit_name |
| 197 | + murfey_session_entry.instrument_name = ExampleVisit.instrument_name |
| 198 | + murfey_db_session.add(murfey_session_entry) |
| 199 | + murfey_db_session.commit() |
| 200 | + |
| 201 | + # Mock the align and merge workflow call |
| 202 | + mock_align_and_merge_call = mocker.patch( |
| 203 | + "murfey.workflows.clem.register_preprocessing_results.submit_cluster_request" |
| 204 | + ) |
| 205 | + |
| 206 | + # Set up the transport manager |
| 207 | + _set_up_transport("PikaTransport") |
| 208 | + |
| 209 | + # Run the function |
| 210 | + for message in preprocessing_messages: |
| 211 | + result = run( |
| 212 | + message=message, |
| 213 | + murfey_db=murfey_db_session, |
| 214 | + ) |
| 215 | + assert result == {"success": True} |
| 216 | + assert mock_align_and_merge_call.call_count == len(preprocessing_messages) * len( |
| 217 | + colors |
| 218 | + ) |
| 219 | + murfey_db_session.close() |
0 commit comments