11from collections .abc import AsyncGenerator
22from contextlib import ExitStack
3- from unittest .mock import MagicMock
3+ from unittest .mock import AsyncMock , MagicMock
44
55import pytest
66from ophyd_async .core import init_devices
7- from ophyd_async .testing import get_mock_put
7+ from ophyd_async .testing import assert_value , get_mock_put
88
99from dodal .common .beamlines .beamline_parameters import GDABeamlineParameters
1010from dodal .devices .aperturescatterguard import ApertureScatterguard , ApertureValue
@@ -32,6 +32,8 @@ async def scintillator_and_ap_sg(
3232) -> AsyncGenerator [tuple [Scintillator , MagicMock ], None ]:
3333 async with init_devices (mock = True ):
3434 mock_ap_sg = MagicMock ()
35+ mock_ap_sg .return_value .selected_aperture .set = AsyncMock ()
36+ mock_ap_sg .return_value .selected_aperture .get_value = AsyncMock ()
3537 scintillator = Scintillator (
3638 prefix = "" ,
3739 name = "test_scin" ,
@@ -49,6 +51,7 @@ async def scintillator_and_ap_sg(
4951@pytest .mark .parametrize (
5052 "y, z, expected_position" ,
5153 [
54+ (100.855 , 101.5115 , InOut .IN ),
5255 (- 0.02 , 0.1 , InOut .OUT ),
5356 (0.1 , 0.1 , InOut .UNKNOWN ),
5457 (10.2 , 15.6 , InOut .UNKNOWN ),
@@ -84,34 +87,56 @@ async def test_given_aperture_scatterguard_parked_when_set_to_out_position_then_
8487
8588 await scintillator .selected_pos .set (InOut .OUT )
8689
87- assert await scintillator .y_mm .user_setpoint . get_value () == - 0.02
88- assert await scintillator .z_mm .user_setpoint . get_value () == 0.1
90+ await assert_value ( scintillator .y_mm .user_setpoint , - 0.02 )
91+ await assert_value ( scintillator .z_mm .user_setpoint , 0.1 )
8992
9093
91- async def test_given_aperture_scatterguard_not_parked_when_set_to_out_position_then_exception_raised (
94+ async def test_given_aperture_scatterguard_parked_when_set_to_in_position_then_returns_expected (
9295 scintillator_and_ap_sg : tuple [Scintillator , ApertureScatterguard ],
96+ ):
97+ scintillator , ap_sg = scintillator_and_ap_sg
98+ ap_sg .return_value .selected_aperture .get_value .return_value = ApertureValue .PARKED # type: ignore
99+
100+ await scintillator .selected_pos .set (InOut .IN )
101+
102+ await assert_value (scintillator .y_mm .user_setpoint , 100.855 )
103+ await assert_value (scintillator .z_mm .user_setpoint , 101.5115 )
104+
105+
106+ @pytest .mark .parametrize ("scint_pos" , [InOut .OUT , InOut .IN ])
107+ async def test_given_aperture_scatterguard_not_parked_when_set_to_in_or_out_position_then_exception_raised (
108+ scintillator_and_ap_sg : tuple [Scintillator , ApertureScatterguard ], scint_pos
93109):
94110 for position in ApertureValue :
95111 if position != ApertureValue .PARKED :
96112 scintillator , ap_sg = scintillator_and_ap_sg
97113 ap_sg .return_value .selected_aperture .get_value .return_value = position # type: ignore
98-
99114 with pytest .raises (ValueError ):
100- await scintillator .selected_pos .set (InOut . OUT )
115+ await scintillator .selected_pos .set (scint_pos )
101116
102117
103- async def test_given_scintillator_already_out_when_moved_out_then_does_nothing (
118+ @pytest .mark .parametrize (
119+ "y, z, expected_position" ,
120+ [
121+ (100.855 , 101.5115 , InOut .IN ),
122+ (- 0.02 , 0.1 , InOut .OUT ),
123+ ],
124+ )
125+ async def test_given_scintillator_already_out_when_moved_in_or_out_then_does_nothing (
104126 scintillator_and_ap_sg : tuple [Scintillator , ApertureScatterguard ],
127+ expected_position ,
128+ y ,
129+ z ,
105130):
106131 scintillator , ap_sg = scintillator_and_ap_sg
107- await scintillator .y_mm .set (0 )
108- await scintillator .z_mm .set (0 )
132+ await scintillator .y_mm .set (y )
133+ await scintillator .z_mm .set (z )
109134
110135 get_mock_put (scintillator .y_mm .user_setpoint ).reset_mock ()
111136 get_mock_put (scintillator .z_mm .user_setpoint ).reset_mock ()
112137
113138 ap_sg .return_value .selected_aperture .get_value .return_value = ApertureValue .LARGE # type: ignore
114- await scintillator .selected_pos .set (InOut . OUT )
139+ await scintillator .selected_pos .set (expected_position )
115140
116141 get_mock_put (scintillator .y_mm .user_setpoint ).assert_not_called ()
117142 get_mock_put (scintillator .z_mm .user_setpoint ).assert_not_called ()
0 commit comments