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