1+ from pathlib import Path
2+ from typing import Any
3+ from unittest .mock import MagicMock
4+
15import pytest
6+ from pytest_mock import MockerFixture
27
38from murfey .workflows .clem .register_preprocessing_results import (
49 _register_clem_image_series ,
510 _register_dcg_and_atlas ,
611 _register_grid_square ,
712 run ,
813)
14+ from tests .conftest import ExampleVisit
15+
16+ visit_name = f"{ ExampleVisit .proposal_code } { ExampleVisit .proposal_number } -{ ExampleVisit .visit_number } "
17+ processed_dir_name = "processed"
18+ grid_name = "Grid_1"
19+ colors = ("gray" , "green" , "red" )
20+
21+
22+ @pytest .fixture
23+ def preprocessing_messages (tmp_path : Path ):
24+ # Make directory to where data for current grid is stored
25+ visit_dir = tmp_path / "data" / "2020" / visit_name
26+ processed_dir = visit_dir / processed_dir_name
27+ grid_dir = processed_dir / grid_name
28+ grid_dir .mkdir (parents = True , exist_ok = True )
29+
30+ # Construct all the datasets to be tested
31+ datasets : list [tuple [Path , bool , bool , tuple [int , int ], float , list [float ]]] = [
32+ (
33+ grid_dir / "Overview_1" / "Image_1" ,
34+ False ,
35+ True ,
36+ (2400 , 2400 ),
37+ 1e-6 ,
38+ [0.002 , 0.0044 , 0.002 , 0.0044 ],
39+ )
40+ ]
41+ # Add on metadata for a few grid squares
42+ datasets .extend (
43+ [
44+ (
45+ grid_dir / "TileScan_1" / f"Position_{ n } " ,
46+ True ,
47+ False ,
48+ (2048 , 2048 ),
49+ 1.6e-7 ,
50+ [0.003 , 0.00332768 , 0.003 , 0.00332768 ],
51+ )
52+ for n in range (5 )
53+ ]
54+ )
55+
56+ messages : list [dict [str , Any ]] = []
57+ for dataset in datasets :
58+ # Unpack items from list of dataset parameters
59+ series_path = dataset [0 ]
60+ series_name = str (series_path .relative_to (processed_dir )).replace ("/" , "--" )
61+ metadata = series_path / "metadata" / f"{ series_path .stem } .xml"
62+ metadata .parent .mkdir (parents = True , exist_ok = True )
63+ metadata .touch (exist_ok = True )
64+ output_files = {color : str (series_path / f"{ color } .tiff" ) for color in colors }
65+ for output_file in output_files .values ():
66+ Path (output_file ).touch (exist_ok = True )
67+ is_stack = dataset [1 ]
68+ is_montage = dataset [2 ]
69+ shape = dataset [3 ]
70+ pixel_size = dataset [4 ]
71+ extent = dataset [5 ]
72+
73+ message = {
74+ "session_id" : ExampleVisit .murfey_session_id ,
75+ "result" : {
76+ "series_name" : series_name ,
77+ "number_of_members" : 3 ,
78+ "is_stack" : is_stack ,
79+ "is_montage" : is_montage ,
80+ "output_files" : output_files ,
81+ "metadata" : str (metadata ),
82+ "parent_lif" : None ,
83+ "parent_tiffs" : {},
84+ "pixels_x" : shape [0 ],
85+ "pixels_y" : shape [1 ],
86+ "units" : "m" ,
87+ "pixel_size" : pixel_size ,
88+ "resolution" : 1 / pixel_size ,
89+ "extent" : extent ,
90+ },
91+ }
92+ messages .append (message )
93+ return messages
994
1095
1196@pytest .mark .skip
@@ -23,6 +108,43 @@ def test_register_grid_square():
23108 assert _register_grid_square
24109
25110
26- @pytest .mark .skip
27- def test_run ():
111+ def test_run (
112+ mocker : MockerFixture ,
113+ preprocessing_messages : list [dict [str , Any ]],
114+ ):
115+ # Mock the MurfeyDB connection
116+ mock_murfey_session_entry = MagicMock ()
117+ mock_murfey_session_entry .instrument_name = ExampleVisit .instrument_name
118+ mock_murfey_session_entry .visit = visit_name
119+ mock_murfey_db = MagicMock ()
120+ mock_murfey_db .exec ().return_value .one .return_value = mock_murfey_session_entry
121+
122+ # Mock the registration helper functions
123+ mock_register_clem_series = mocker .patch (
124+ "murfey.workflows.clem.register_preprocessing_results._register_clem_image_series"
125+ )
126+ mock_register_dcg_and_atlas = mocker .patch (
127+ "murfey.workflows.clem.register_preprocessing_results._register_dcg_and_atlas"
128+ )
129+ mock_register_grid_square = mocker .patch (
130+ "murfey.workflows.clem.register_preprocessing_results._register_grid_square"
131+ )
132+
133+ # Mock the align and merge workflow call
134+ mock_align_and_merge_call = mocker .patch (
135+ "murfey.workflows.clem.register_preprocessing_results.submit_cluster_request"
136+ )
137+
138+ for message in preprocessing_messages :
139+ result = run (
140+ message = message ,
141+ murfey_db = mock_murfey_db ,
142+ )
143+ assert result == {"success" : True }
144+ assert mock_register_clem_series .call_count == len (preprocessing_messages )
145+ assert mock_register_dcg_and_atlas .call_count == len (preprocessing_messages )
146+ assert mock_register_grid_square .call_count == len (preprocessing_messages ) - 1
147+ assert mock_align_and_merge_call .call_count == len (preprocessing_messages ) * len (
148+ colors
149+ )
28150 assert run
0 commit comments