|
| 1 | +import unittest |
| 2 | +from parameterized import parameterized as param |
| 3 | + |
| 4 | +from utilities.utilities import load_config_if_not_already_loaded, g, \ |
| 5 | + set_genie_python_raises_exceptions |
| 6 | + |
| 7 | +ADV_CONFIG_NAME = "advanced" |
| 8 | + |
| 9 | + |
| 10 | +class TestAdvancedMotorControls(unittest.TestCase): |
| 11 | + def setUp(self): |
| 12 | + g.set_instrument(None) |
| 13 | + load_config_if_not_already_loaded(ADV_CONFIG_NAME) |
| 14 | + set_genie_python_raises_exceptions(True) |
| 15 | + |
| 16 | + @param.expand([True, False]) |
| 17 | + def test_GIVEN_manager_mode_WHEN_calling_get_manager_mode_THEN_returns_true(self, manager_mode): |
| 18 | + # Checks that the get_manager_mode() function works as expected. |
| 19 | + g.set_pv("CS:MANAGER", manager_mode, wait=True, is_local=True) |
| 20 | + self.assertTrue(g.adv.get_manager_mode() == manager_mode) |
| 21 | + |
| 22 | + def test_GIVEN_no_manager_mode_WHEN_setting_motor_position_THEN_exception_is_raised(self): |
| 23 | + # Checks that the user will not be allowed to change the motor position without being in manager mode |
| 24 | + g.set_pv("CS:MANAGER", "No", wait=True, is_local=True) |
| 25 | + |
| 26 | + with self.assertRaises(RuntimeError): |
| 27 | + g.adv.redefine_motor_position("MTR0101", 1000) |
| 28 | + |
| 29 | + def test_GIVEN_invalid_motor_name_WHEN_setting_motor_position_THEN_exception_is_raised(self): |
| 30 | + # Checks that the set_motor_position function will only accept motors it recognises |
| 31 | + g.set_pv("CS:MANAGER", "Yes", wait=True, is_local=True) |
| 32 | + |
| 33 | + with self.assertRaises(ValueError): |
| 34 | + g.adv.redefine_motor_position("INVALID_MOTOR_NAME", 1000) |
| 35 | + |
| 36 | + def test_GIVEN_foff_is_variable_and_set_is_use_WHEN_setting_motor_position_THEN_foff_and_set_change_before_and_after(self): |
| 37 | + # Before changing motor position, check that SET mode is on Set |
| 38 | + # and FOFF is on Frozen |
| 39 | + |
| 40 | + foff_value = "Variable" |
| 41 | + set_value = "Use" |
| 42 | + |
| 43 | + g.set_pv("MOT:MTR0101.FOFF", foff_value, wait=True, is_local=True) # Frozen mode |
| 44 | + g.set_pv("MOT:MTR0101.SET", set_value, wait=True, is_local=True) # Use mode |
| 45 | + |
| 46 | + with g.adv.motor_in_set_mode(g.my_pv_prefix + "MOT:MTR0101"): |
| 47 | + self.assertTrue(g.get_pv("MOT:MTR0101.SET", to_string=True, is_local=True) == "Set") |
| 48 | + self.assertTrue(g.get_pv("MOT:MTR0101.FOFF", to_string=True, is_local=True) == "Frozen") |
| 49 | + |
| 50 | + self.assertTrue(g.get_pv("MOT:MTR0101.SET", to_string=True, is_local=True) == "Use") |
| 51 | + # Check that MOT:MTR0101.SET is in Use mode after calling set_motor_position() |
| 52 | + self.assertTrue(g.get_pv("MOT:MTR0101.FOFF", to_string=True, is_local=True) == foff_value) |
| 53 | + # Check that MOT:MTR0101.FFOF is in the same mode before and after calling set_motor_position() |
| 54 | + |
| 55 | + @param.expand([1000, -1000]) |
| 56 | + def test_GIVEN_manager_mode_and_valid_motor_name_WHEN_setting_motor_position_THEN_motor_position_set(self, motor_value): |
| 57 | + # Checks that for a combination of valid parameters there are no exceptions |
| 58 | + g.set_pv("CS:MANAGER", "Yes", wait=True, is_local=True) |
| 59 | + |
| 60 | + g.adv.redefine_motor_position("MTR0101", motor_value) |
| 61 | + |
| 62 | + self.assertTrue(motor_value == g.get_pv("MOT:MTR0101.VAL", to_string=False, is_local=True)) |
| 63 | + # Assert that the motor position changes after calling set_motor_position() |
| 64 | + |
| 65 | + def test_GIVEN_motor_is_moving_WHEN_setting_motor_position_THEN_exception_raised(self): |
| 66 | + # Checks that the motor is not allowed to be repositioned while it is already moving |
| 67 | + g.set_pv("CS:MANAGER", "Yes", wait=True, is_local=True) |
| 68 | + g.set_pv("MOT:MTR0101.SET", 0, wait=True, is_local=True) # Use mode |
| 69 | + |
| 70 | + g.set_pv("MOT:MTR0101.VAL", 30000.0, wait=False, is_local=True) # Set position so that motor begins moving |
| 71 | + |
| 72 | + with self.assertRaises(RuntimeError): |
| 73 | + g.adv.redefine_motor_position("MTR0101", 1000) # Check that it throws as exception as it is moving |
| 74 | + |
| 75 | + def test_GIVEN_invalid_pv_WHEN_calling_motor_in_set_mode_THEN_exception_raised(self): |
| 76 | + # Checks that the function motor_in_set_mode will not accept an invalid pv |
| 77 | + with self.assertRaises(ValueError): |
| 78 | + with g.adv.motor_in_set_mode(g.my_pv_prefix + "MOT:INVALID_MOTOR_NAME"): None |
| 79 | + |
| 80 | + def test_GIVEN_valid_pv_but_not_a_motor_pv_WHEN_calling_motor_in_set_mode_THEN_exception_raised(self): |
| 81 | + # Checks that the function motor_in_set_mode will not accept a valid pv that does not point to a motor |
| 82 | + with self.assertRaises(ValueError): |
| 83 | + with g.adv.motor_in_set_mode(g.my_pv_prefix + "CS:MANAGER"): None |
| 84 | + |
| 85 | + def tearDown(self): |
| 86 | + g.set_pv("MOT:MTR0101.STOP", 1, wait=True, is_local=True) # Make sure motor is not moving |
| 87 | + g.set_pv("MOT:MTR0101.SET", 1, wait=True, is_local=True) # Set mode |
| 88 | + g.set_pv("MOT:MTR0101.VAL", 0.0, wait=True, is_local=True) # Motor is repositioned |
| 89 | + g.set_pv("CS:MANAGER", "No", wait=True, is_local=True) # Make sure not in manager mode |
| 90 | + set_genie_python_raises_exceptions(False) |
0 commit comments