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,86 @@ 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 the ISPyB connection where the TransportManager class is located
186+ mock_security_config = MagicMock ()
187+ mock_security_config .ispyb_credentials = mock_ispyb_credentials
188+ mocker .patch (
189+ "murfey.server.ispyb.get_security_config" ,
190+ return_value = mock_security_config ,
191+ )
192+ mocker .patch (
193+ "murfey.server.ispyb.ISPyBSession" ,
194+ return_value = ispyb_db_session ,
195+ )
196+
197+ # Mock the ISPYB connection when registering data collection group
198+ mocker .patch (
199+ "murfey.workflows.register_data_collection_group.ISPyBSession" ,
200+ return_value = ispyb_db_session ,
201+ )
202+
203+ # Mock out the machine config used in the helper sanitisation function
204+ mock_get_machine_config = mocker .patch ("murfey.workflows.clem.get_machine_config" )
205+ mock_machine_config = MagicMock ()
206+ mock_machine_config .rsync_basepath = rsync_basepath
207+ mock_get_machine_config .return_value = {
208+ ExampleVisit .instrument_name : mock_machine_config ,
209+ }
210+
211+ # Mock the align and merge workflow call
212+ mock_align_and_merge_call = mocker .patch (
213+ "murfey.workflows.clem.register_preprocessing_results.submit_cluster_request"
214+ )
215+
216+ # Patch the TransportManager object in the workflows called
217+ from murfey .server .ispyb import TransportManager
218+
219+ mocker .patch (
220+ "murfey.workflows.clem.register_preprocessing_results._transport_object" ,
221+ new = TransportManager ("PikaTransport" ),
222+ )
223+ mocker .patch (
224+ "murfey.workflows.register_data_collection_group._transport_object" ,
225+ new = TransportManager ("PikaTransport" ),
226+ )
227+ mocker .patch (
228+ "murfey.workflows.register_atlas_update._transport_object" ,
229+ new = TransportManager ("PikaTransport" ),
230+ )
231+
232+ # Run the function
233+ preprocessing_messages = generate_preprocessing_messages (
234+ rsync_basepath = rsync_basepath ,
235+ session_id = murfey_session .id ,
236+ )
237+ for message in preprocessing_messages :
238+ result = run (
239+ message = message ,
240+ murfey_db = murfey_db_session ,
241+ )
242+ assert result == {"success" : True }
243+ assert mock_align_and_merge_call .call_count == len (preprocessing_messages ) * len (
244+ colors
245+ )
246+ murfey_db_session .close ()
0 commit comments