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
1114from murfey .workflows .fib .register_atlas import FIBAtlasMetadata , _parse_metadata , run
15+ from tests .conftest import ExampleVisit
1216
1317session_id = 10
14- visit_name = "cm12345-6 "
15- instrument_name = "test_instrument"
18+ visit_name = f" { ExampleVisit . proposal_code } { ExampleVisit . proposal_number } - { ExampleVisit . visit_number } "
19+ instrument_name = ExampleVisit . instrument_name
1620
1721
1822@pytest .fixture
@@ -290,7 +294,9 @@ def test_register_fib_imaging_site():
290294def test_run_with_db (
291295 mocker : MockerFixture ,
292296 visit_dir : Path ,
293- murfey_db_session : Session ,
297+ murfey_db_session : SQLModelSession ,
298+ ispyb_db_session : SQLAlchemySession ,
299+ mock_ispyb_credentials ,
294300):
295301 test_files = (
296302 visit_dir / "maps/LayersData/Layer/Electron Snapshot/Electron Snapshot.tiff" ,
@@ -301,7 +307,7 @@ def test_run_with_db(
301307 # Add a test visit to the database
302308 if not (
303309 session_entry := murfey_db_session .exec (
304- select (MurfeyDB .Session ).where (MurfeyDB .Session .id == session_id )
310+ sm_select (MurfeyDB .Session ).where (MurfeyDB .Session .id == session_id )
305311 ).one_or_none ()
306312 ):
307313 session_entry = MurfeyDB .Session (id = session_id )
@@ -312,6 +318,36 @@ def test_run_with_db(
312318 murfey_db_session .add (session_entry )
313319 murfey_db_session .commit ()
314320
321+ # Mock the ISPyB connection where the TransportManager class is located
322+ mock_security_config = MagicMock ()
323+ mock_security_config .ispyb_credentials = mock_ispyb_credentials
324+ mocker .patch (
325+ "murfey.server.ispyb.get_security_config" ,
326+ return_value = mock_security_config ,
327+ )
328+ mocker .patch (
329+ "murfey.server.ispyb.ISPyBSession" ,
330+ return_value = ispyb_db_session ,
331+ )
332+
333+ # Mock the ISPYB connection when registering data collection group
334+ mocker .patch (
335+ "murfey.workflows.register_data_collection_group.ISPyBSession" ,
336+ return_value = ispyb_db_session ,
337+ )
338+
339+ # Patch the TransportManager object in the workflows called
340+ from murfey .server .ispyb import TransportManager
341+
342+ mocker .patch (
343+ "murfey.workflows.register_data_collection_group._transport_object" ,
344+ new = TransportManager ("PikaTransport" ),
345+ )
346+ mocker .patch (
347+ "murfey.workflows.register_atlas_update._transport_object" ,
348+ new = TransportManager ("PikaTransport" ),
349+ )
350+
315351 # Mock the metadata returned from the image file
316352 mock_metadata = [
317353 FIBAtlasMetadata (
@@ -354,10 +390,45 @@ def test_run_with_db(
354390 assert mock_parse .call_count == len (test_files )
355391 assert spy_register .call_count == len (test_files )
356392
393+ # Murfey's ImagingSite should have an entry
357394 search_results = murfey_db_session .exec (
358- select (MurfeyDB .ImagingSite ).where (
395+ sm_select (MurfeyDB .ImagingSite ).where (
359396 MurfeyDB .ImagingSite .session_id == session_id
360397 )
361398 ).all ()
362399 assert len (search_results ) == 1
363400 assert search_results [0 ].image_path == str (mock_metadata [- 1 ].file )
401+
402+ # Murfey's DataCollectionGroup should have an entry
403+ murfey_dcg_search = murfey_db_session .exec (
404+ sm_select (MurfeyDB .DataCollectionGroup ).where (
405+ MurfeyDB .DataCollectionGroup .session_id == session_id
406+ )
407+ ).all ()
408+ assert len (murfey_dcg_search ) == 1
409+
410+ # ISPyB's DataCollectionGroup should have an entry
411+ murfey_dcg = murfey_dcg_search [0 ]
412+ ispyb_dcg_search = (
413+ ispyb_db_session .execute (
414+ sa_select (ISPyBDB .DataCollectionGroup ).where (
415+ ISPyBDB .DataCollectionGroup .dataCollectionGroupId == murfey_dcg .id
416+ )
417+ )
418+ .scalars ()
419+ .all ()
420+ )
421+ assert len (ispyb_dcg_search ) == 1
422+
423+ # Atlas should have an entry
424+ ispyb_dcg = ispyb_dcg_search [0 ]
425+ ispyb_atlas_search = (
426+ ispyb_db_session .execute (
427+ sa_select (ISPyBDB .Atlas ).where (
428+ ISPyBDB .Atlas .dataCollectionGroupId == ispyb_dcg .dataCollectionGroupId
429+ )
430+ )
431+ .scalars ()
432+ .all ()
433+ )
434+ assert len (ispyb_atlas_search ) == 1
0 commit comments