|
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" |
18 | 21 | grid_name = "Grid_1" |
19 | 22 | colors = ("gray", "green", "red") |
20 | 23 |
|
21 | 24 |
|
22 | | -@pytest.fixture |
23 | | -def preprocessing_messages(tmp_path: Path): |
| 25 | +def generate_preprocessing_messages( |
| 26 | + rsync_basepath: Path, |
| 27 | + session_id: int, |
| 28 | +): |
24 | 29 | # Make directory to where data for current grid is stored |
25 | | - visit_dir = tmp_path / "data" / "2020" / visit_name |
| 30 | + visit_dir = rsync_basepath / "2020" / visit_name |
26 | 31 | processed_dir = visit_dir / processed_dir_name |
27 | 32 | grid_dir = processed_dir / grid_name |
28 | 33 | grid_dir.mkdir(parents=True, exist_ok=True) |
@@ -71,7 +76,7 @@ def preprocessing_messages(tmp_path: Path): |
71 | 76 | extent = dataset[5] |
72 | 77 |
|
73 | 78 | message = { |
74 | | - "session_id": ExampleVisit.murfey_session_id, |
| 79 | + "session_id": session_id, |
75 | 80 | "result": { |
76 | 81 | "series_name": series_name, |
77 | 82 | "number_of_members": 3, |
@@ -147,4 +152,80 @@ def test_run( |
147 | 152 | assert mock_align_and_merge_call.call_count == len(preprocessing_messages) * len( |
148 | 153 | colors |
149 | 154 | ) |
150 | | - assert run |
| 155 | + |
| 156 | + |
| 157 | +def test_run_with_db( |
| 158 | + mocker: MockerFixture, |
| 159 | + tmp_path: Path, |
| 160 | + mock_ispyb_credentials, |
| 161 | + murfey_db_session: SQLModelSession, |
| 162 | + ispyb_db_session: SQLAlchemySession, |
| 163 | +): |
| 164 | + # Create a session to insert for this test |
| 165 | + murfey_session: MurfeyDB.Session = get_or_create_db_entry( |
| 166 | + murfey_db_session, |
| 167 | + MurfeyDB.Session, |
| 168 | + insert_kwargs={ |
| 169 | + "name": visit_name, |
| 170 | + "visit": visit_name, |
| 171 | + "instrument_name": ExampleVisit.instrument_name, |
| 172 | + }, |
| 173 | + ) |
| 174 | + |
| 175 | + # Create value for rsync basepath value |
| 176 | + rsync_basepath = tmp_path / "data" |
| 177 | + |
| 178 | + # Mock out module-level functions |
| 179 | + mock_security_config = MagicMock() |
| 180 | + mock_security_config.ispyb_credentials = mock_ispyb_credentials |
| 181 | + mock_get_security_config = mocker.patch("murfey.server.ispyb.get_security_config") |
| 182 | + mock_get_security_config.return_value = mock_security_config |
| 183 | + |
| 184 | + # Mock the ISPyB connection |
| 185 | + mock_ispyb_sessionmaker = mocker.patch("murfey.server.ispyb.sessionmaker") |
| 186 | + mock_ispyb_sessionmaker.return_value.return_value = ispyb_db_session |
| 187 | + |
| 188 | + # Mock out the machine config used in the helper sanitisation function |
| 189 | + mock_get_machine_config = mocker.patch("murfey.workflows.clem.get_machine_config") |
| 190 | + mock_machine_config = MagicMock() |
| 191 | + mock_machine_config.rsync_basepath = rsync_basepath |
| 192 | + mock_get_machine_config.return_value = { |
| 193 | + "": mock_machine_config, |
| 194 | + } |
| 195 | + |
| 196 | + # Mock the align and merge workflow call |
| 197 | + mock_align_and_merge_call = mocker.patch( |
| 198 | + "murfey.workflows.clem.register_preprocessing_results.submit_cluster_request" |
| 199 | + ) |
| 200 | + |
| 201 | + # Patch the TransportManager object in the workflows called |
| 202 | + from murfey.server.ispyb import TransportManager |
| 203 | + |
| 204 | + mocker.patch( |
| 205 | + "murfey.workflows.clem.register_preprocessing_results._transport_object", |
| 206 | + new=TransportManager("PikaTransport"), |
| 207 | + ) |
| 208 | + mocker.patch( |
| 209 | + "murfey.workflows.register_data_collection_group._transport_object", |
| 210 | + new=TransportManager("PikaTransport"), |
| 211 | + ) |
| 212 | + mocker.patch( |
| 213 | + "murfey.workflows.register_atlas_update._transport_object", |
| 214 | + new=TransportManager("PikaTransport"), |
| 215 | + ) |
| 216 | + |
| 217 | + # Run the function |
| 218 | + preprocessing_messages = generate_preprocessing_messages( |
| 219 | + rsync_basepath=rsync_basepath, |
| 220 | + session_id=murfey_session.id, |
| 221 | + ) |
| 222 | + for message in preprocessing_messages: |
| 223 | + result = run( |
| 224 | + message=message, |
| 225 | + murfey_db=murfey_db_session, |
| 226 | + ) |
| 227 | + assert result == {"success": True} |
| 228 | + assert mock_align_and_merge_call.call_count == len(preprocessing_messages) * len( |
| 229 | + colors |
| 230 | + ) |
| 231 | + murfey_db_session.close() |
0 commit comments