Skip to content

Commit 39a1d9a

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

File tree

1 file changed

+87
-6
lines changed

1 file changed

+87
-6
lines changed

tests/workflows/clem/test_register_preprocessing_results.py

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@
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"
1821
grid_name = "Grid_1"
1922
colors = ("gray", "green", "red")
2023

2124

22-
@pytest.fixture
23-
def preprocessing_messages(tmp_path: Path):
25+
def generate_preprocessing_messages(
26+
rsync_basepath: Path,
27+
session_id: int,
28+
):
2429
# 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
2631
processed_dir = visit_dir / processed_dir_name
2732
grid_dir = processed_dir / grid_name
2833
grid_dir.mkdir(parents=True, exist_ok=True)
@@ -71,7 +76,7 @@ def preprocessing_messages(tmp_path: Path):
7176
extent = dataset[5]
7277

7378
message = {
74-
"session_id": ExampleVisit.murfey_session_id,
79+
"session_id": session_id,
7580
"result": {
7681
"series_name": series_name,
7782
"number_of_members": 3,
@@ -147,4 +152,80 @@ def test_run(
147152
assert mock_align_and_merge_call.call_count == len(preprocessing_messages) * len(
148153
colors
149154
)
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

Comments
 (0)