Skip to content

Commit aa9a779

Browse files
committed
Run test workflow with Murfey and ISPyB databases
1 parent 01e6798 commit aa9a779

File tree

1 file changed

+93
-6
lines changed

1 file changed

+93
-6
lines changed

tests/workflows/clem/test_register_preprocessing_results.py

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
import pytest
66
from pytest_mock import MockerFixture
7+
from sqlalchemy.orm.session import Session as SQLAlchemySession
8+
from sqlmodel.orm.session import Session as SQLModelSession
79

10+
import murfey.util.db as MurfeyDB
811
from murfey.workflows.clem.register_preprocessing_results import (
912
_register_clem_image_series,
1013
_register_dcg_and_atlas,
1114
_register_grid_square,
1215
run,
1316
)
14-
from tests.conftest import ExampleVisit
17+
from tests.conftest import ExampleVisit, get_or_create_db_entry
1518

1619
visit_name = f"{ExampleVisit.proposal_code}{ExampleVisit.proposal_number}-{ExampleVisit.visit_number}"
1720
processed_dir_name = "processed"
@@ -20,9 +23,16 @@
2023

2124

2225
@pytest.fixture
23-
def preprocessing_messages(tmp_path: Path):
26+
def rsync_basepath(tmp_path: Path):
27+
return tmp_path / "data"
28+
29+
30+
def generate_preprocessing_messages(
31+
rsync_basepath: Path,
32+
session_id: int,
33+
):
2434
# Make directory to where data for current grid is stored
25-
visit_dir = tmp_path / "data" / "2020" / visit_name
35+
visit_dir = rsync_basepath / "2020" / visit_name
2636
processed_dir = visit_dir / processed_dir_name
2737
grid_dir = processed_dir / grid_name
2838
grid_dir.mkdir(parents=True, exist_ok=True)
@@ -71,7 +81,7 @@ def preprocessing_messages(tmp_path: Path):
7181
extent = dataset[5]
7282

7383
message = {
74-
"session_id": ExampleVisit.murfey_session_id,
84+
"session_id": session_id,
7585
"result": {
7686
"series_name": series_name,
7787
"number_of_members": 3,
@@ -110,7 +120,7 @@ def test_register_grid_square():
110120

111121
def test_run(
112122
mocker: MockerFixture,
113-
preprocessing_messages: list[dict[str, Any]],
123+
rsync_basepath: Path,
114124
):
115125
# Mock the MurfeyDB connection
116126
mock_murfey_session_entry = MagicMock()
@@ -135,6 +145,10 @@ def test_run(
135145
"murfey.workflows.clem.register_preprocessing_results.submit_cluster_request"
136146
)
137147

148+
preprocessing_messages = generate_preprocessing_messages(
149+
rsync_basepath=rsync_basepath,
150+
session_id=1,
151+
)
138152
for message in preprocessing_messages:
139153
result = run(
140154
message=message,
@@ -147,4 +161,77 @@ def test_run(
147161
assert mock_align_and_merge_call.call_count == len(preprocessing_messages) * len(
148162
colors
149163
)
150-
assert run
164+
165+
166+
def test_run_with_db(
167+
mocker: MockerFixture,
168+
rsync_basepath: Path,
169+
mock_ispyb_credentials,
170+
murfey_db_session: SQLModelSession,
171+
ispyb_db_session: SQLAlchemySession,
172+
):
173+
# Create a session to insert for this test
174+
murfey_session: MurfeyDB.Session = get_or_create_db_entry(
175+
murfey_db_session,
176+
MurfeyDB.Session,
177+
lookup_kwargs={
178+
"name": visit_name,
179+
"visit": visit_name,
180+
"instrument_name": ExampleVisit.instrument_name,
181+
},
182+
)
183+
184+
# Mock out module-level functions
185+
mock_security_config = MagicMock()
186+
mock_security_config.ispyb_credentials = mock_ispyb_credentials
187+
mock_get_security_config = mocker.patch("murfey.server.ispyb.get_security_config")
188+
mock_get_security_config.return_value = mock_security_config
189+
190+
# Mock the ISPyB connection
191+
mock_ispyb_sessionmaker = mocker.patch("murfey.server.ispyb.sessionmaker")
192+
mock_ispyb_sessionmaker.return_value.return_value = ispyb_db_session
193+
194+
# Mock out the machine config used in the helper sanitisation function
195+
mock_get_machine_config = mocker.patch("murfey.workflows.clem.get_machine_config")
196+
mock_machine_config = MagicMock()
197+
mock_machine_config.rsync_basepath = rsync_basepath
198+
mock_get_machine_config.return_value = {
199+
"": mock_machine_config,
200+
}
201+
202+
# Mock the align and merge workflow call
203+
mock_align_and_merge_call = mocker.patch(
204+
"murfey.workflows.clem.register_preprocessing_results.submit_cluster_request"
205+
)
206+
207+
# Patch the TransportManager object in the workflows called
208+
from murfey.server.ispyb import TransportManager
209+
210+
mocker.patch(
211+
"murfey.workflows.clem.register_preprocessing_results._transport_object",
212+
new=TransportManager("PikaTransport"),
213+
)
214+
mocker.patch(
215+
"murfey.workflows.register_data_collection_group._transport_object",
216+
new=TransportManager("PikaTransport"),
217+
)
218+
mocker.patch(
219+
"murfey.workflows.register_atlas_update._transport_object",
220+
new=TransportManager("PikaTransport"),
221+
)
222+
223+
# Run the function
224+
preprocessing_messages = generate_preprocessing_messages(
225+
rsync_basepath=rsync_basepath,
226+
session_id=murfey_session.id,
227+
)
228+
for message in preprocessing_messages:
229+
result = run(
230+
message=message,
231+
murfey_db=murfey_db_session,
232+
)
233+
assert result == {"success": True}
234+
assert mock_align_and_merge_call.call_count == len(preprocessing_messages) * len(
235+
colors
236+
)
237+
murfey_db_session.close()

0 commit comments

Comments
 (0)