Skip to content

Commit 66c1efe

Browse files
committed
Attempt to test workflow with Murfey and ISPyB databases
1 parent 6cba6a7 commit 66c1efe

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

tests/workflows/clem/test_register_preprocessing_results.py

Lines changed: 77 additions & 4 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,14 @@
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+
@pytest.fixture
31+
def preprocessing_messages(rsync_basepath: Path):
2432
# 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
2634
processed_dir = visit_dir / processed_dir_name
2735
grid_dir = processed_dir / grid_name
2836
grid_dir.mkdir(parents=True, exist_ok=True)
@@ -147,4 +155,69 @@ def test_run(
147155
assert mock_align_and_merge_call.call_count == len(preprocessing_messages) * len(
148156
colors
149157
)
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

Comments
 (0)