3131#
3232
3333import pytest
34- from unittest .mock import MagicMock
34+ from unittest .mock import MagicMock , call
3535import numpy as np
3636
3737AXES = ["x" , "y" , "z" , "theta" , "f" ]
@@ -55,6 +55,52 @@ def stage_controller(dummy_controller):
5555 dummy_controller ,
5656 )
5757
58+ # test before set position variables to MagicMock()
59+ def test_set_position (stage_controller ):
60+
61+ widgets = stage_controller .view .get_widgets ()
62+ vals = {}
63+ for axis in AXES :
64+ widgets [axis ].widget .trigger_focusout_validation = MagicMock ()
65+ vals [axis ] = np .random .randint (0 , 9 )
66+
67+ stage_controller .view .get_widgets = MagicMock (return_value = widgets )
68+ stage_controller .show_verbose_info = MagicMock ()
69+ position = {
70+ "x" : np .random .random (),
71+ "y" : np .random .random (),
72+ "z" : np .random .random (),
73+ }
74+ stage_controller .set_position (position )
75+ for axis in position .keys ():
76+ assert stage_controller .widget_vals [axis ].get () == position [axis ]
77+ assert widgets [axis ].widget .trigger_focusout_validation .called
78+ assert stage_controller .stage_setting_dict [axis ] == position .get (axis , 0 )
79+ stage_controller .show_verbose_info .assert_has_calls ([call ("Stage position changed" ), call ("Set stage position" )])
80+
81+ def test_set_position_silent (stage_controller ):
82+
83+ widgets = stage_controller .view .get_widgets ()
84+ vals = {}
85+ for axis in AXES :
86+ widgets [axis ].widget .trigger_focusout_validation = MagicMock ()
87+ vals [axis ] = np .random .randint (0 , 9 )
88+
89+ stage_controller .view .get_widgets = MagicMock (return_value = widgets )
90+ stage_controller .show_verbose_info = MagicMock ()
91+ position = {
92+ "x" : np .random .random (),
93+ "y" : np .random .random (),
94+ "z" : np .random .random (),
95+ }
96+ stage_controller .set_position_silent (position )
97+ for axis in position .keys ():
98+ assert stage_controller .widget_vals [axis ].get () == position [axis ]
99+ widgets [axis ].widget .trigger_focusout_validation .assert_called_once ()
100+ assert stage_controller .stage_setting_dict [axis ] == position .get (axis , 0 )
101+ stage_controller .show_verbose_info .assert_has_calls ([call ("Set stage position" )])
102+ assert call ("Stage position changed" ) not in stage_controller .show_verbose_info .mock_calls
103+
58104
59105@pytest .mark .parametrize (
60106 "flip_x, flip_y" ,
@@ -105,30 +151,6 @@ def test_stage_key_press(stage_controller, flip_x, flip_y):
105151 stage_config ["flip_y" ] = False
106152
107153
108- def test_set_position (stage_controller ):
109-
110- widgets = stage_controller .view .get_widgets ()
111- vals = {}
112- for axis in AXES :
113- widgets [axis ].widget .trigger_focusout_validation = MagicMock ()
114- vals [axis ] = np .random .randint (0 , 9 )
115- stage_controller .widget_vals [axis ].set = MagicMock ()
116-
117- stage_controller .view .get_widgets = MagicMock (return_value = widgets )
118- stage_controller .show_verbose_info = MagicMock ()
119- position = {
120- "x" : np .random .random (),
121- "y" : np .random .random (),
122- "z" : np .random .random (),
123- }
124- stage_controller .set_position (position )
125- for axis in AXES :
126- assert stage_controller .widget_vals [axis ].set .called
127- widgets [axis ].widget .trigger_focusout_validation .assert_called_once ()
128- assert stage_controller .stage_setting_dict [axis ] == position .get (axis , 0 )
129- stage_controller .show_verbose_info .assert_called_once_with ("Set stage position" )
130-
131-
132154def test_get_position (stage_controller ):
133155 import tkinter as tk
134156
0 commit comments