Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Tests for the faux module.""" |
| 6 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 7 | from chromite.api import faux |
| 8 | from chromite.api.api_config import ApiConfigMixin |
| 9 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
| 10 | from chromite.lib import cros_test_lib |
| 11 | |
| 12 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 13 | class MockResponsesTest(cros_test_lib.TestCase, ApiConfigMixin): |
| 14 | """Tests for faux's mock response functionality.""" |
| 15 | |
| 16 | _IMPL_RESULT = 'result' |
| 17 | _SUCCESS_RESULT = 'success' |
| 18 | _ERROR_RESULT = 'error' |
| 19 | _ALL_RESULT = 'all' |
| 20 | |
| 21 | def setUp(self): |
| 22 | self.request = build_api_test_pb2.TestRequestMessage() |
| 23 | self.response = build_api_test_pb2.TestResultMessage() |
| 24 | |
| 25 | def _faux_success(self, _input_proto, output_proto, _config): |
| 26 | """Faux success method.""" |
| 27 | output_proto.result = self._SUCCESS_RESULT |
| 28 | |
| 29 | def _faux_error(self, _input_proto, output_proto, _config): |
| 30 | """Faux error method.""" |
| 31 | output_proto.result = self._ERROR_RESULT |
| 32 | |
| 33 | def _faux_all(self, _input_proto, output_proto, config): |
| 34 | """All responses method.""" |
| 35 | self.assertIn(config, [self.mock_call_config, self.mock_error_config]) |
| 36 | output_proto.result = self._ALL_RESULT |
| 37 | |
| 38 | def test_call_called(self): |
| 39 | """Test a faux call.""" |
| 40 | |
| 41 | @faux.error(self._faux_error) |
| 42 | @faux.success(self._faux_success) |
| 43 | def impl(_input_proto, _output_proto, _config): |
| 44 | self.fail('Implementation was called.') |
| 45 | |
| 46 | impl(self.request, self.response, self.mock_call_config) |
| 47 | |
| 48 | self.assertEqual(self.response.result, self._SUCCESS_RESULT) |
| 49 | |
| 50 | def test_error_called(self): |
| 51 | """Test the faux error intercepts the call.""" |
| 52 | @faux.success(self._faux_success) |
| 53 | @faux.error(self._faux_error) |
| 54 | def impl(_input_proto, _output_proto, _config): |
| 55 | self.fail('Implementation was called.') |
| 56 | |
| 57 | impl(self.request, self.response, self.mock_error_config) |
| 58 | |
| 59 | self.assertEqual(self.response.result, self._ERROR_RESULT) |
| 60 | |
| 61 | def test_impl_called(self): |
| 62 | """Test the call is not mocked when not requested.""" |
| 63 | @faux.error(self._faux_error) |
| 64 | @faux.success(self._faux_success) |
| 65 | def impl(_input_proto, output_proto, _config): |
| 66 | output_proto.result = self._IMPL_RESULT |
| 67 | |
| 68 | impl(self.request, self.response, self.api_config) |
| 69 | |
| 70 | self.assertEqual(self.response.result, self._IMPL_RESULT) |
| 71 | |
| 72 | def test_all_responses_success(self): |
| 73 | """Test the call is intercepted by the all responses decorator.""" |
| 74 | |
| 75 | @faux.all_responses(self._faux_all) |
| 76 | def impl(_input_proto, _output_proto, _config): |
| 77 | self.fail('Implementation was called.') |
| 78 | |
| 79 | impl(self.request, self.response, self.mock_call_config) |
| 80 | self.assertEqual(self.response.result, self._ALL_RESULT) |
| 81 | |
| 82 | def test_all_responses_error(self): |
| 83 | """Test the call is intercepted by the all responses decorator.""" |
| 84 | |
| 85 | @faux.all_responses(self._faux_all) |
| 86 | def impl(_input_proto, _output_proto, _config): |
| 87 | self.fail('Implementation was called.') |
| 88 | |
| 89 | impl(self.request, self.response, self.mock_error_config) |
| 90 | self.assertEqual(self.response.result, self._ALL_RESULT) |
| 91 | |
| 92 | def test_all_responses_impl(self): |
| 93 | """Test the call is intercepted by the all responses decorator.""" |
| 94 | |
| 95 | @faux.all_responses(self._faux_all) |
| 96 | def impl(_input_proto, output_proto, _config): |
| 97 | output_proto.result = self._IMPL_RESULT |
| 98 | |
| 99 | impl(self.request, self.response, self.api_config) |
| 100 | self.assertEqual(self.response.result, self._IMPL_RESULT) |