| # 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 lock 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 lock |
| |
| |
| _FAKE_ERROR_MSG = 'fake-error-msg' |
| |
| |
| class LockCommandHandlerTest(unittest.TestCase): |
| """Unit tests for LockCommandHandler.""" |
| |
| def setUp(self): |
| super().setUp() |
| self.dut = mock.Mock() |
| self.handler = lock.LockCommandHandler(self.dut) |
| |
| @parameterized.expand([(True,), (False,)]) |
| def test_01_get_is_locked_on_success(self, locked_state): |
| """Verifies get_is_locked on success.""" |
| self.dut.pw_rpc_lock.state = locked_state |
| |
| is_locked = self.handler._get_is_locked({}) |
| |
| self.assertEqual(locked_state, is_locked) |
| |
| def test_01_get_is_locked_on_failure(self): |
| """Verifies get_is_locked on failure.""" |
| mock_state = mock.PropertyMock( |
| side_effect=errors.DeviceError(_FAKE_ERROR_MSG)) |
| type(self.dut.pw_rpc_lock).state = mock_state |
| with self.assertRaisesRegex(errors.DeviceError, _FAKE_ERROR_MSG): |
| self.handler._get_is_locked({}) |
| |
| def test_02_set_lock_on_success(self): |
| """Verifies set_lock on success.""" |
| self.handler._set_lock({}) |
| self.dut.pw_rpc_lock.lock.assert_called_once() |
| |
| def test_02_set_lock_on_failure(self): |
| """Verifies set_lock on failure.""" |
| self.dut.pw_rpc_lock.lock.side_effect = errors.DeviceError('') |
| with self.assertRaises(errors.DeviceError): |
| self.handler._set_lock({}) |
| |
| def test_03_set_unlock_on_success(self): |
| """Verifies set_unlock on success.""" |
| self.handler._set_unlock({}) |
| self.dut.pw_rpc_lock.unlock.assert_called_once() |
| |
| def test_03_set_unlock_on_failure(self): |
| """Verifies set_unlock on failure.""" |
| self.dut.pw_rpc_lock.unlock.side_effect = errors.DeviceError('') |
| with self.assertRaises(errors.DeviceError): |
| self.handler._set_unlock({}) |
| |
| |
| if __name__ == '__main__': |
| unittest.main(failfast=True) |