| # Copyright 2021 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Unit tests for light command handlers.""" |
| from parameterized import parameterized |
| import unittest |
| from unittest import mock |
| |
| from gazoo_device import errors |
| |
| from local_agent.translation_layer.command_handlers import light |
| |
| |
| _FAKE_BRIGHTNESS = 100 |
| _FAKE_HUE = 10 |
| _FAKE_SATURATION = 10 |
| _FAKE_ERROR_MSG = 'fake-error-msg' |
| |
| |
| class LightCommandHandlerTest(unittest.TestCase): |
| """Unit tests for LightCommandHandler.""" |
| |
| def setUp(self): |
| super().setUp() |
| self.dut = mock.Mock() |
| self.handler = light.LightCommandHandler(self.dut) |
| |
| @parameterized.expand([(True, 'on'), (False, 'off')]) |
| def test_01_get_on_off_on_success(self, light_state, expected_state): |
| """Verifies get_on_off on success.""" |
| self.dut.pw_rpc_light.state = light_state |
| |
| state = self.handler._get_on_off({}) |
| |
| self.assertEqual(expected_state, state) |
| |
| def test_01_get_on_off_on_failure(self): |
| """Verifies get_on_off on failure.""" |
| mock_state = mock.PropertyMock( |
| side_effect=errors.DeviceError(_FAKE_ERROR_MSG)) |
| type(self.dut.pw_rpc_light).state = mock_state |
| with self.assertRaisesRegex(errors.DeviceError, _FAKE_ERROR_MSG): |
| self.handler._get_on_off({}) |
| |
| def test_02_set_on_on_success(self): |
| """Verifies set_on on success.""" |
| self.handler._set_on({}) |
| self.dut.pw_rpc_light.on.assert_called_once() |
| |
| def test_02_set_on_on_failure(self): |
| """Verifies set_on on failure.""" |
| self.dut.pw_rpc_light.on.side_effect = errors.DeviceError('') |
| with self.assertRaises(errors.DeviceError): |
| self.handler._set_on({}) |
| |
| def test_03_set_off_on_success(self): |
| """Verifies set_off on success.""" |
| self.handler._set_off({}) |
| self.dut.pw_rpc_light.off.assert_called_once() |
| |
| def test_03_set_off_on_failure(self): |
| """Verifies set_off on failure.""" |
| self.dut.pw_rpc_light.off.side_effect = errors.DeviceError('') |
| with self.assertRaises(errors.DeviceError): |
| self.handler._set_off({}) |
| |
| def test_04_get_brightness_on_success(self): |
| """Verifies get_brightness on success.""" |
| self.dut.pw_rpc_light.brightness = _FAKE_BRIGHTNESS |
| |
| brightness = self.handler._get_brightness({}) |
| |
| self.assertEqual(_FAKE_BRIGHTNESS, brightness) |
| |
| def test_04_get_brightness_on_failure(self): |
| """Verifies get_brightness on failure.""" |
| mock_state = mock.PropertyMock( |
| side_effect=errors.DeviceError(_FAKE_ERROR_MSG)) |
| type(self.dut.pw_rpc_light).brightness = mock_state |
| with self.assertRaisesRegex(errors.DeviceError, _FAKE_ERROR_MSG): |
| self.handler._get_brightness({}) |
| |
| @mock.patch.object(light.LightCommandHandler, 'validate_key_in_params') |
| def test_05_set_brightness_on_success(self, mock_validate_key_in_params): |
| """Verifies set_brightness on success.""" |
| self.handler._set_brightness({'level': _FAKE_BRIGHTNESS}) |
| |
| mock_validate_key_in_params.assert_called_once() |
| self.dut.pw_rpc_light.on.assert_called_once_with(level=_FAKE_BRIGHTNESS) |
| |
| @mock.patch.object(light.LightCommandHandler, 'validate_key_in_params') |
| def test_05_set_brightness_on_failure(self, mock_validate_key_in_params): |
| """Verifies set_brightness on failure.""" |
| self.dut.pw_rpc_light.on.side_effect = errors.DeviceError('') |
| with self.assertRaises(errors.DeviceError): |
| self.handler._set_brightness({'level': _FAKE_BRIGHTNESS}) |
| |
| def test_06_get_color_on_success(self): |
| """Verifies get_color on success.""" |
| self.dut.pw_rpc_light.color.hue = _FAKE_HUE |
| self.dut.pw_rpc_light.color.saturation = _FAKE_SATURATION |
| expected_response = {'hue': _FAKE_HUE, 'saturation': _FAKE_SATURATION} |
| |
| color = self.handler._get_color({}) |
| |
| self.assertEqual(expected_response, color) |
| |
| def test_06_get_color_on_failure(self): |
| """Verifies get_color on failure.""" |
| mock_state = mock.PropertyMock( |
| side_effect=errors.DeviceError(_FAKE_ERROR_MSG)) |
| type(self.dut.pw_rpc_light.color).hue = mock_state |
| with self.assertRaisesRegex(errors.DeviceError, _FAKE_ERROR_MSG): |
| self.handler._get_color({}) |
| |
| @mock.patch.object(light.LightCommandHandler, 'validate_key_in_params') |
| def test_07_set_color_on_success(self, mock_validate_key_in_params): |
| """Verifies set_color on success.""" |
| params = {'hue': _FAKE_HUE, 'saturation': _FAKE_SATURATION} |
| |
| self.handler._set_color(params) |
| |
| self.assertEqual(2, mock_validate_key_in_params.call_count) |
| self.dut.pw_rpc_light.on.assert_called_once_with( |
| hue=_FAKE_HUE, saturation=_FAKE_SATURATION) |
| |
| @mock.patch.object(light.LightCommandHandler, 'validate_key_in_params') |
| def test_07_set_color_on_failure(self, mock_validate_key_in_params): |
| """Verifies set_color on failure.""" |
| params = {'hue': _FAKE_HUE, 'saturation': _FAKE_SATURATION} |
| self.dut.pw_rpc_light.on.side_effect = errors.DeviceError('') |
| with self.assertRaises(errors.DeviceError): |
| self.handler._set_color(params) |
| |
| |
| if __name__ == '__main__': |
| unittest.main(failfast=True) |