Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame^] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 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): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 14 | """Tests for faux's mock response functionality.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 15 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 16 | _IMPL_RESULT = "result" |
| 17 | _SUCCESS_RESULT = "success" |
| 18 | _ERROR_RESULT = "error" |
| 19 | _ALL_RESULT = "all" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 20 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 21 | def setUp(self): |
| 22 | self.request = build_api_test_pb2.TestRequestMessage() |
| 23 | self.response = build_api_test_pb2.TestResultMessage() |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 24 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | def _faux_success(self, _input_proto, output_proto, _config): |
| 26 | """Faux success method.""" |
| 27 | output_proto.result = self._SUCCESS_RESULT |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 28 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | def _faux_error(self, _input_proto, output_proto, _config): |
| 30 | """Faux error method.""" |
| 31 | output_proto.result = self._ERROR_RESULT |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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 |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 37 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | def test_call_called(self): |
| 39 | """Test a faux call.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 40 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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.") |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 45 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | impl(self.request, self.response, self.mock_call_config) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | self.assertEqual(self.response.result, self._SUCCESS_RESULT) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | def test_error_called(self): |
| 51 | """Test the faux error intercepts the call.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | @faux.success(self._faux_success) |
| 54 | @faux.error(self._faux_error) |
| 55 | def impl(_input_proto, _output_proto, _config): |
| 56 | self.fail("Implementation was called.") |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | impl(self.request, self.response, self.mock_error_config) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | self.assertEqual(self.response.result, self._ERROR_RESULT) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | def test_impl_called(self): |
| 63 | """Test the call is not mocked when not requested.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | @faux.error(self._faux_error) |
| 66 | @faux.success(self._faux_success) |
| 67 | def impl(_input_proto, output_proto, _config): |
| 68 | output_proto.result = self._IMPL_RESULT |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | impl(self.request, self.response, self.api_config) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 71 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | self.assertEqual(self.response.result, self._IMPL_RESULT) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | def test_all_responses_success(self): |
| 75 | """Test the call is intercepted by the all responses decorator.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | @faux.all_responses(self._faux_all) |
| 78 | def impl(_input_proto, _output_proto, _config): |
| 79 | self.fail("Implementation was called.") |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | impl(self.request, self.response, self.mock_call_config) |
| 82 | self.assertEqual(self.response.result, self._ALL_RESULT) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | def test_all_responses_error(self): |
| 85 | """Test the call is intercepted by the all responses decorator.""" |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | @faux.all_responses(self._faux_all) |
| 88 | def impl(_input_proto, _output_proto, _config): |
| 89 | self.fail("Implementation was called.") |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | impl(self.request, self.response, self.mock_error_config) |
| 92 | self.assertEqual(self.response.result, self._ALL_RESULT) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 93 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | def test_all_responses_impl(self): |
| 95 | """Test the call is intercepted by the all responses decorator.""" |
| 96 | |
| 97 | @faux.all_responses(self._faux_all) |
| 98 | def impl(_input_proto, output_proto, _config): |
| 99 | output_proto.result = self._IMPL_RESULT |
| 100 | |
| 101 | impl(self.request, self.response, self.api_config) |
| 102 | self.assertEqual(self.response.result, self._IMPL_RESULT) |