22from pathlib import Path
33from unittest .mock import MagicMock
44
5+ import ispyb .sqlalchemy as ISPyBDB
56import pytest
67from pytest_mock import MockerFixture
7- from sqlmodel import Session , select
8+ from sqlalchemy import select as sa_select
9+ from sqlalchemy .orm import Session as SQLAlchemySession
10+ from sqlmodel import Session as SQLModelSession , select as sm_select
811
912import murfey .util .db as MurfeyDB
1013import murfey .workflows .fib .register_atlas
@@ -290,7 +293,9 @@ def test_register_fib_imaging_site():
290293def test_run_with_db (
291294 mocker : MockerFixture ,
292295 visit_dir : Path ,
293- murfey_db_session : Session ,
296+ murfey_db_session : SQLModelSession ,
297+ ispyb_db_session : SQLAlchemySession ,
298+ mock_ispyb_credentials ,
294299):
295300 test_files = (
296301 visit_dir / "maps/LayersData/Layer/Electron Snapshot/Electron Snapshot.tiff" ,
@@ -301,7 +306,7 @@ def test_run_with_db(
301306 # Add a test visit to the database
302307 if not (
303308 session_entry := murfey_db_session .exec (
304- select (MurfeyDB .Session ).where (MurfeyDB .Session .id == session_id )
309+ sm_select (MurfeyDB .Session ).where (MurfeyDB .Session .id == session_id )
305310 ).one_or_none ()
306311 ):
307312 session_entry = MurfeyDB .Session (id = session_id )
@@ -312,6 +317,36 @@ def test_run_with_db(
312317 murfey_db_session .add (session_entry )
313318 murfey_db_session .commit ()
314319
320+ # Mock the ISPyB connection where the TransportManager class is located
321+ mock_security_config = MagicMock ()
322+ mock_security_config .ispyb_credentials = mock_ispyb_credentials
323+ mocker .patch (
324+ "murfey.server.ispyb.get_security_config" ,
325+ return_value = mock_security_config ,
326+ )
327+ mocker .patch (
328+ "murfey.server.ispyb.ISPyBSession" ,
329+ return_value = ispyb_db_session ,
330+ )
331+
332+ # Mock the ISPYB connection when registering data collection group
333+ mocker .patch (
334+ "murfey.workflows.register_data_collection_group.ISPyBSession" ,
335+ return_value = ispyb_db_session ,
336+ )
337+
338+ # Patch the TransportManager object in the workflows called
339+ from murfey .server .ispyb import TransportManager
340+
341+ mocker .patch (
342+ "murfey.workflows.register_data_collection_group._transport_object" ,
343+ new = TransportManager ("PikaTransport" ),
344+ )
345+ mocker .patch (
346+ "murfey.workflows.register_atlas_update._transport_object" ,
347+ new = TransportManager ("PikaTransport" ),
348+ )
349+
315350 # Mock the metadata returned from the image file
316351 mock_metadata = [
317352 FIBAtlasMetadata (
@@ -354,10 +389,45 @@ def test_run_with_db(
354389 assert mock_parse .call_count == len (test_files )
355390 assert spy_register .call_count == len (test_files )
356391
392+ # Murfey's ImagingSite should have an entry
357393 search_results = murfey_db_session .exec (
358- select (MurfeyDB .ImagingSite ).where (
394+ sm_select (MurfeyDB .ImagingSite ).where (
359395 MurfeyDB .ImagingSite .session_id == session_id
360396 )
361397 ).all ()
362398 assert len (search_results ) == 1
363399 assert search_results [0 ].image_path == str (mock_metadata [- 1 ].file )
400+
401+ # Murfey's DataCollectionGroup should have an entry
402+ murfey_dcg_search = murfey_db_session .exec (
403+ sm_select (MurfeyDB .DataCollectionGroup ).where (
404+ MurfeyDB .DataCollectionGroup .session_id == session_id
405+ )
406+ ).all ()
407+ assert len (murfey_dcg_search ) == 1
408+
409+ # ISPyB's DataCollectionGroup should have an entry
410+ murfey_dcg = murfey_dcg_search [0 ]
411+ ispyb_dcg_search = (
412+ ispyb_db_session .execute (
413+ sa_select (ISPyBDB .DataCollectionGroup ).where (
414+ ISPyBDB .DataCollectionGroup .dataCollectionGroupId == murfey_dcg .id
415+ )
416+ )
417+ .scalars ()
418+ .all ()
419+ )
420+ assert len (ispyb_dcg_search ) == 1
421+
422+ # Atlas should have an entry
423+ ispyb_dcg = ispyb_dcg_search [0 ]
424+ ispyb_atlas_search = (
425+ ispyb_db_session .execute (
426+ sa_select (ISPyBDB .Atlas ).where (
427+ ISPyBDB .Atlas .dataCollectionGroupId == ispyb_dcg .dataCollectionGroupId
428+ )
429+ )
430+ .scalars ()
431+ .all ()
432+ )
433+ assert len (ispyb_atlas_search ) == 1
0 commit comments