11from pathlib import Path
22from unittest .mock import MagicMock
33
4- import numpy as np
5- import PIL .Image
64import pytest
75from pytest_mock import MockerFixture
86
97from murfey .server .api .workflow_fib import (
108 FIBAtlasFile ,
11- FIBGIFParameters ,
129 make_gif ,
1310 register_fib_atlas ,
1411)
12+ from murfey .util .models import FIBGIFParameters
1513
1614
1715@pytest .mark .parametrize (
@@ -33,7 +31,7 @@ def test_register_fib_atlas(
3331 # Mock the logger
3432 mock_logger = mocker .patch ("murfey.server.api.workflow_fib.logger" )
3533
36- # Mock the tranposrt object
34+ # Mock the transport object
3735 if has_transport_object :
3836 mock_transport_object = MagicMock ()
3937 mock_transport_object .feedback_queue = "dummy"
@@ -67,74 +65,61 @@ def test_register_fib_atlas(
6765 mock_logger .error .assert_called_with ("No TransportManager object was set up" )
6866
6967
68+ @pytest .mark .parametrize (
69+ "has_transport_object" ,
70+ (
71+ True ,
72+ False ,
73+ ),
74+ )
7075@pytest .mark .asyncio
7176async def test_make_gif (
7277 mocker : MockerFixture ,
7378 tmp_path : Path ,
79+ has_transport_object : bool ,
7480):
75- # Set up test variables
76- session_id = 10
77- instrument_name = "test_instrument"
78- rsync_basepath = tmp_path / "data"
79- visit_name = "cm12345-6"
80- year = 2020
81- visit_dir = rsync_basepath / str (year ) / visit_name
82- lamella_num = 12
83- lamella_folder = "Lamella"
84- if lamella_num > 1 :
85- lamella_folder += f" ({ lamella_num } )"
86- output_file = (
87- visit_dir
88- / "processed"
89- / "project_name"
90- / "grid_1"
91- / "drift_correction"
92- / f"lamella_{ lamella_num } .gif"
93- )
94-
95- # Create a list of test image file paths
96- raw_images = [
97- visit_dir
98- / "autotem"
99- / visit_name
100- / "Sites"
101- / lamella_folder
102- / "DCImages/DCM_asdfjkl/asdfjkl-Polishing-dc_rescan-image-.png"
103- ] * 5
104- # Mock the output of PIL.Image.open to always return a NumPY array
105- mocker .patch (
106- "murfey.server.api.workflow_fib.PIL.Image.open" ,
107- return_value = PIL .Image .fromarray (np .ones ((512 , 512 ), dtype = np .uint16 )),
108- )
109-
110- # Create the Pydantic model
111- params = FIBGIFParameters (
112- lamella_number = lamella_num ,
113- images = [str (f ) for f in raw_images ],
114- output_file = output_file ,
115- )
81+ # Set up the variables
82+ session_id = 1
83+ gif_params_dict = {
84+ "lamella_number" : 1 ,
85+ "images" : [
86+ str (tmp_path / "some_file.png" ),
87+ ],
88+ "output_file" : str (tmp_path / "target_file.gif" ),
89+ }
90+ gif_params = FIBGIFParameters (** gif_params_dict )
11691
117- # Mock the database query
118- mock_db = MagicMock ()
119- mock_db .exec .return_value .one .return_value .instrument_name = instrument_name
120- mock_db .exec .return_value .one .return_value .visit = visit_name
92+ # Mock the logger
93+ mock_logger = mocker .patch ("murfey.server.api.workflow_fib.logger" )
12194
122- # Mock the machine config and 'get_machine_config'
123- mock_machine_config = MagicMock ()
124- mock_machine_config .mkdir_chmod = 0o2775
125- mock_machine_config .rsync_basepath = rsync_basepath
126- mocker .patch (
127- "murfey.server.api.workflow_fib.get_machine_config" ,
128- return_value = {
129- instrument_name : mock_machine_config ,
130- },
131- )
95+ # Mock the transport object
96+ if has_transport_object :
97+ mock_transport_object = MagicMock ()
98+ mock_transport_object .feedback_queue = "dummy"
99+ mocker .patch (
100+ "murfey.server.api.workflow_fib._transport_object" ,
101+ mock_transport_object ,
102+ )
103+ else :
104+ mocker .patch (
105+ "murfey.server.api.workflow_fib._transport_object" ,
106+ None ,
107+ )
132108
133- # Run the function and check that the expected outputs are there
134- result = await make_gif (
109+ # Run the function and check that the expected calls were made
110+ await make_gif (
135111 session_id = session_id ,
136- gif_params = params ,
137- db = mock_db ,
112+ gif_params = gif_params ,
138113 )
139- assert output_file .exists ()
140- assert result .get ("output_gif" ) == str (output_file )
114+
115+ if has_transport_object :
116+ mock_transport_object .send .assert_called_with (
117+ "dummy" ,
118+ {
119+ "register" : "fib.make_milling_gif" ,
120+ "session_id" : session_id ,
121+ "gif_params" : gif_params_dict ,
122+ },
123+ )
124+ else :
125+ mock_logger .error .assert_called_with ("No TransportManager object was set up" )
0 commit comments