1- """Tests for the AIOCAControlSystem class.
2- """
1+ """Tests for the AIOCAControlSystem class."""
2+
3+ from unittest .mock import MagicMock , patch
34
4- from unittest .mock import patch , MagicMock
55import pytest
66from constants import RB_PV , SP_PV
77from testfixtures import LogCapture
88
99import pytac
1010from pytac .aioca_cs import AIOCAControlSystem
1111
12+
1213class CANothing (Exception ):
1314 """A minimal mock of the cothread CANothing exception class."""
1415
1516 def __init__ (self , name , errorcode = True ):
1617 self .ok = errorcode
1718 self .name = name
1819
20+
1921@pytest .fixture
2022def cs ():
2123 return AIOCAControlSystem (wait = True , timeout = 2.0 )
2224
2325
2426@patch ("pytac.aioca_cs.caget" )
25- async def test_get_single_calls_caget_correctly (caget :MagicMock , cs ):
27+ async def test_get_single_calls_caget_correctly (caget : MagicMock , cs ):
2628 caget .return_value = 42
2729 assert (await cs .get_single (RB_PV )) == 42
2830 caget .assert_called_with (RB_PV , throw = True , timeout = 2.0 )
2931
3032
3133@patch ("pytac.aioca_cs.caget" )
32- async def test_get_multiple_calls_caget_correctly (caget :MagicMock , cs ):
34+ async def test_get_multiple_calls_caget_correctly (caget : MagicMock , cs ):
3335 """caget is called with throw=False despite throw=True being the default
3436 for get_multiple as we always want our get operation to fully complete,
3537 rather than being stopped halway through by an error raised from
@@ -42,13 +44,13 @@ async def test_get_multiple_calls_caget_correctly(caget:MagicMock, cs):
4244
4345
4446@patch ("pytac.aioca_cs.caput" )
45- async def test_set_single_calls_caput_correctly (caput :MagicMock , cs ):
47+ async def test_set_single_calls_caput_correctly (caput : MagicMock , cs ):
4648 assert await cs .set_single (SP_PV , 42 ) is True
4749 caput .assert_called_with (SP_PV , 42 , throw = True , timeout = 2.0 , wait = True )
4850
4951
5052@patch ("pytac.aioca_cs.caput" )
51- async def test_set_multiple_calls_caput_correctly (caput :MagicMock , cs ):
53+ async def test_set_multiple_calls_caput_correctly (caput : MagicMock , cs ):
5254 """caput is called with throw=False despite throw=True being the default
5355 for set_multiple as we always want our set operation to fully complete,
5456 rather than being stopped halway through by an error raised from
@@ -63,7 +65,7 @@ async def test_set_multiple_calls_caput_correctly(caput:MagicMock, cs):
6365
6466@patch ("pytac.aioca_cs.caget" )
6567@patch ("pytac.aioca_cs.CANothing" , CANothing )
66- async def test_get_multiple_raises_ControlSystemException (caget :MagicMock , cs ):
68+ async def test_get_multiple_raises_ControlSystemException (caget : MagicMock , cs ):
6769 """Here we check that errors are thrown, suppressed and logged correctly."""
6870 caget .return_value = [12 , CANothing ("pv" , False )]
6971 with pytest .raises (pytac .exceptions .ControlSystemException ):
@@ -75,19 +77,22 @@ async def test_get_multiple_raises_ControlSystemException(caget:MagicMock, cs):
7577
7678@patch ("pytac.aioca_cs.caput" )
7779@patch ("pytac.aioca_cs.CANothing" , CANothing )
78- async def test_set_multiple_raises_ControlSystemException (caput :MagicMock , cs ):
80+ async def test_set_multiple_raises_ControlSystemException (caput : MagicMock , cs ):
7981 """Here we check that errors are thrown, suppressed and logged correctly."""
8082 caput .return_value = [CANothing ("pv1" , True ), CANothing ("pv2" , False )]
8183 with pytest .raises (pytac .exceptions .ControlSystemException ):
8284 await cs .set_multiple ([RB_PV , SP_PV ], [42 , 6 ])
8385 with LogCapture () as log :
84- assert await cs .set_multiple ([RB_PV , SP_PV ], [42 , 6 ], throw = False ) == [True , False ]
86+ assert await cs .set_multiple ([RB_PV , SP_PV ], [42 , 6 ], throw = False ) == [
87+ True ,
88+ False ,
89+ ]
8590 log .check (("root" , "WARNING" , "Cannot connect to pv2." ))
8691
8792
8893@patch ("pytac.aioca_cs.caget" )
8994@patch ("pytac.aioca_cs.CANothing" , CANothing )
90- async def test_get_single_raises_ControlSystemException (caget :MagicMock , cs ):
95+ async def test_get_single_raises_ControlSystemException (caget : MagicMock , cs ):
9196 """Here we check that errors are thrown, suppressed and logged correctly."""
9297 caget .side_effect = CANothing ("pv" , False )
9398 with LogCapture () as log :
@@ -99,7 +104,7 @@ async def test_get_single_raises_ControlSystemException(caget:MagicMock, cs):
99104
100105@patch ("pytac.aioca_cs.caput" )
101106@patch ("pytac.aioca_cs.CANothing" , CANothing )
102- async def test_set_single_raises_ControlSystemException (caput :MagicMock , cs ):
107+ async def test_set_single_raises_ControlSystemException (caput : MagicMock , cs ):
103108 """Here we check that errors are thrown, suppressed and logged correctly."""
104109 caput .side_effect = CANothing ("pv" , False )
105110 with LogCapture () as log :
0 commit comments