<commit_before>from hamcrest import *

import ReflectometryServer
import unittest

from ReflectometryServer import *
from ReflectometryServer.test_modules.data_mother import MockChannelAccess, create_mock_axis

from server_common.channel_access import UnableToConnectToPVException

FLOAT_TOLERANCE = 1e-9


class TestEngineeringCorrections(unittest.TestCase):

    def _setup_driver_axis_and_correction(self, correction):
        comp = Component("comp", PositionAndAngle(0.0, 0.0, 0.0))
        mock_axis = create_mock_axis("MOT:MTR0101", 0, 1)
        driver = DisplacementDriver(comp, mock_axis, engineering_correct=ConstantCorrection(correction))
        return driver, mock_axis, comp

    def test_GIVEN_engineering_correction_offset_of_1_WHEN_driver_told_to_go_to_0_THEN_pv_sent_to_1(self):
        expected_correction = 1
        driver, mock_axis, comp = self._setup_driver_axis_and_correction(expected_correction)
        driver.perform_move(1)

        result = mock_axis.sp

        assert_that(result, is_(close_to(expected_correction, FLOAT_TOLERANCE)))

    def test_GIVEN_engineering_correction_offset_of_1_on_angle_driver_WHEN_driver_told_to_go_to_0_THEN_pv_sent_to_1(self):
        expected_correction = 1
        comp = TiltingComponent("comp", PositionAndAngle(0.0, 0.0, 0.0))
        mock_axis = create_mock_axis("MOT:MTR0101", 0, 1)
        driver = AngleDriver(comp, mock_axis, engineering_correct=ConstantCorrection(expected_correction))
        driver.perform_move(1)

        result = mock_axis.sp

        assert_that(result, is_(close_to(expected_correction, FLOAT_TOLERANCE)))

    def test_GIVEN_engineering_correction_offset_of_1_WHEN_driver_is_at_2_THEN_read_back_is_at_1(self):
        expected_correct_value = 1
        correction = 1
        move_to = expected_correct_value + correction
        driver, mock_axis, comp = self._setup_driver_axis_and_correction(correction)
        mock_axis.sp = move_to

        result = comp.beam_path_rbv.get_displacement()

        assert_that(result, is_(close_to(expected_correct_value, FLOAT_TOLERANCE)))

    def test_GIVEN_engineering_correction_offset_of_1_WHEN_at_set_point_THEN_at_target_setpoint_is_true(self):
        correction = 4
        driver, mock_axis, comp = self._setup_driver_axis_and_correction(correction)
        comp.beam_path_set_point.set_displacement(2, None, None)
        driver.perform_move(1)

        result = driver.at_target_setpoint()

        assert_that(result, is_(True), "Axis is at set point")<commit_msg>Remember to set driver is changed to simulate request by component<commit_after>from hamcrest import *

import ReflectometryServer
import unittest

from ReflectometryServer import *
from ReflectometryServer.test_modules.data_mother import MockChannelAccess, create_mock_axis

from server_common.channel_access import UnableToConnectToPVException

FLOAT_TOLERANCE = 1e-9


class TestEngineeringCorrections(unittest.TestCase):

    def _setup_driver_axis_and_correction(self, correction):
        comp = Component("comp", PositionAndAngle(0.0, 0.0, 0.0))
        mock_axis = create_mock_axis("MOT:MTR0101", 0, 1)
        driver = DisplacementDriver(comp, mock_axis, engineering_correct=ConstantCorrection(correction))
        driver._is_changed = lambda: True  # simulate that the component has requested a change
        return driver, mock_axis, comp

    def test_GIVEN_engineering_correction_offset_of_1_WHEN_driver_told_to_go_to_0_THEN_pv_sent_to_1(self):
        expected_correction = 1
        driver, mock_axis, comp = self._setup_driver_axis_and_correction(expected_correction)
        driver.perform_move(1)

        result = mock_axis.sp

        assert_that(result, is_(close_to(expected_correction, FLOAT_TOLERANCE)))

    def test_GIVEN_engineering_correction_offset_of_1_on_angle_driver_WHEN_driver_told_to_go_to_0_THEN_pv_sent_to_1(self):
        expected_correction = 1
        comp = TiltingComponent("comp", PositionAndAngle(0.0, 0.0, 0.0))
        mock_axis = create_mock_axis("MOT:MTR0101", 0, 1)
        driver = AngleDriver(comp, mock_axis, engineering_correct=ConstantCorrection(expected_correction))
        driver._is_changed = lambda: True  # simulate that the component has requested a change
        driver.perform_move(1)

        result = mock_axis.sp

        assert_that(result, is_(close_to(expected_correction, FLOAT_TOLERANCE)))

    def test_GIVEN_engineering_correction_offset_of_1_WHEN_driver_is_at_2_THEN_read_back_is_at_1(self):
        expected_correct_value = 1
        correction = 1
        move_to = expected_correct_value + correction
        driver, mock_axis, comp = self._setup_driver_axis_and_correction(correction)
        mock_axis.sp = move_to

        result = comp.beam_path_rbv.get_displacement()

        assert_that(result, is_(close_to(expected_correct_value, FLOAT_TOLERANCE)))

    def test_GIVEN_engineering_correction_offset_of_1_WHEN_at_set_point_THEN_at_target_setpoint_is_true(self):
        correction = 4
        driver, mock_axis, comp = self._setup_driver_axis_and_correction(correction)
        comp.beam_path_set_point.set_displacement(2, None, None)
        driver.perform_move(1)

        result = driver.at_target_setpoint()

        assert_that(result, is_(True), "Axis is at set point")
{'fraction_non_alphanumeric': 0.06895245311612047, 'fraction_numerical': 0.01704868346277704, 'mean_word_length': 4.6350053361792956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3834323', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1692, 'n_words': 273}