44
55import pytest
66from 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
811from 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
1619visit_name = f"{ ExampleVisit .proposal_code } { ExampleVisit .proposal_number } -{ ExampleVisit .visit_number } "
1720processed_dir_name = "processed"
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
111121def 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 = ExampleVisit .murfey_session_id ,
151+ )
138152 for message in preprocessing_messages :
139153 result = run (
140154 message = message ,
@@ -147,4 +161,79 @@ 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+ "id" : ExampleVisit .murfey_session_id + 1 ,
179+ "name" : visit_name ,
180+ "visit" : visit_name ,
181+ "instrument_name" : ExampleVisit .instrument_name ,
182+ },
183+ )
184+
185+ # Mock out module-level functions
186+ mock_security_config = MagicMock ()
187+ mock_security_config .ispyb_credentials = mock_ispyb_credentials
188+ mock_get_security_config = mocker .patch ("murfey.server.ispyb.get_security_config" )
189+ mock_get_security_config .return_value = mock_security_config
190+
191+ # Mock the ISPyB connection
192+ mock_ispyb_sessionmaker = mocker .patch ("murfey.server.ispyb.sessionmaker" )
193+ mock_ispyb_sessionmaker .return_value .return_value = ispyb_db_session
194+
195+ # Mock out the machine config used in the helper sanitisation function
196+ mock_get_machine_config = mocker .patch ("murfey.workflows.clem.get_machine_config" )
197+ mock_machine_config = MagicMock ()
198+ mock_machine_config .rsync_basepath = rsync_basepath
199+ mock_get_machine_config .return_value = {
200+ # "": mock_machine_config,
201+ ExampleVisit .instrument_name : mock_machine_config ,
202+ }
203+
204+ # Mock the align and merge workflow call
205+ mock_align_and_merge_call = mocker .patch (
206+ "murfey.workflows.clem.register_preprocessing_results.submit_cluster_request"
207+ )
208+
209+ # Patch the TransportManager object in the workflows called
210+ from murfey .server .ispyb import TransportManager
211+
212+ mocker .patch (
213+ "murfey.workflows.clem.register_preprocessing_results._transport_object" ,
214+ new = TransportManager ("PikaTransport" ),
215+ )
216+ mocker .patch (
217+ "murfey.workflows.register_data_collection_group._transport_object" ,
218+ new = TransportManager ("PikaTransport" ),
219+ )
220+ mocker .patch (
221+ "murfey.workflows.register_atlas_update._transport_object" ,
222+ new = TransportManager ("PikaTransport" ),
223+ )
224+
225+ # Run the function
226+ preprocessing_messages = generate_preprocessing_messages (
227+ rsync_basepath = rsync_basepath ,
228+ session_id = murfey_session .id ,
229+ )
230+ for message in preprocessing_messages :
231+ result = run (
232+ message = message ,
233+ murfey_db = murfey_db_session ,
234+ )
235+ assert result == {"success" : True }
236+ assert mock_align_and_merge_call .call_count == len (preprocessing_messages ) * len (
237+ colors
238+ )
239+ murfey_db_session .close ()
0 commit comments