blob: 147e0c4b1d30595c045c4285ed2db05d002b6eea [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Klein2008aee2019-08-20 16:25:27 -06002# 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 Klein2008aee2019-08-20 16:25:27 -06007from chromite.api import faux
8from chromite.api.api_config import ApiConfigMixin
9from chromite.api.gen.chromite.api import build_api_test_pb2
10from chromite.lib import cros_test_lib
11
12
Alex Klein2008aee2019-08-20 16:25:27 -060013class MockResponsesTest(cros_test_lib.TestCase, ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060014 """Tests for faux's mock response functionality."""
Alex Klein2008aee2019-08-20 16:25:27 -060015
Alex Klein1699fab2022-09-08 08:46:06 -060016 _IMPL_RESULT = "result"
17 _SUCCESS_RESULT = "success"
18 _ERROR_RESULT = "error"
19 _ALL_RESULT = "all"
Alex Klein2008aee2019-08-20 16:25:27 -060020
Alex Klein1699fab2022-09-08 08:46:06 -060021 def setUp(self):
22 self.request = build_api_test_pb2.TestRequestMessage()
23 self.response = build_api_test_pb2.TestResultMessage()
Alex Klein2008aee2019-08-20 16:25:27 -060024
Alex Klein1699fab2022-09-08 08:46:06 -060025 def _faux_success(self, _input_proto, output_proto, _config):
26 """Faux success method."""
27 output_proto.result = self._SUCCESS_RESULT
Alex Klein2008aee2019-08-20 16:25:27 -060028
Alex Klein1699fab2022-09-08 08:46:06 -060029 def _faux_error(self, _input_proto, output_proto, _config):
30 """Faux error method."""
31 output_proto.result = self._ERROR_RESULT
Alex Klein2008aee2019-08-20 16:25:27 -060032
Alex Klein1699fab2022-09-08 08:46:06 -060033 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 Klein2008aee2019-08-20 16:25:27 -060037
Alex Klein1699fab2022-09-08 08:46:06 -060038 def test_call_called(self):
39 """Test a faux call."""
Alex Klein2008aee2019-08-20 16:25:27 -060040
Alex Klein1699fab2022-09-08 08:46:06 -060041 @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 Klein2008aee2019-08-20 16:25:27 -060045
Alex Klein1699fab2022-09-08 08:46:06 -060046 impl(self.request, self.response, self.mock_call_config)
Alex Klein2008aee2019-08-20 16:25:27 -060047
Alex Klein1699fab2022-09-08 08:46:06 -060048 self.assertEqual(self.response.result, self._SUCCESS_RESULT)
Alex Klein2008aee2019-08-20 16:25:27 -060049
Alex Klein1699fab2022-09-08 08:46:06 -060050 def test_error_called(self):
51 """Test the faux error intercepts the call."""
Alex Klein2008aee2019-08-20 16:25:27 -060052
Alex Klein1699fab2022-09-08 08:46:06 -060053 @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 Klein2008aee2019-08-20 16:25:27 -060057
Alex Klein1699fab2022-09-08 08:46:06 -060058 impl(self.request, self.response, self.mock_error_config)
Alex Klein2008aee2019-08-20 16:25:27 -060059
Alex Klein1699fab2022-09-08 08:46:06 -060060 self.assertEqual(self.response.result, self._ERROR_RESULT)
Alex Klein2008aee2019-08-20 16:25:27 -060061
Alex Klein1699fab2022-09-08 08:46:06 -060062 def test_impl_called(self):
63 """Test the call is not mocked when not requested."""
Alex Klein2008aee2019-08-20 16:25:27 -060064
Alex Klein1699fab2022-09-08 08:46:06 -060065 @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 Klein2008aee2019-08-20 16:25:27 -060069
Alex Klein1699fab2022-09-08 08:46:06 -060070 impl(self.request, self.response, self.api_config)
Alex Klein2008aee2019-08-20 16:25:27 -060071
Alex Klein1699fab2022-09-08 08:46:06 -060072 self.assertEqual(self.response.result, self._IMPL_RESULT)
Alex Klein2008aee2019-08-20 16:25:27 -060073
Alex Klein1699fab2022-09-08 08:46:06 -060074 def test_all_responses_success(self):
75 """Test the call is intercepted by the all responses decorator."""
Alex Klein2008aee2019-08-20 16:25:27 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 @faux.all_responses(self._faux_all)
78 def impl(_input_proto, _output_proto, _config):
79 self.fail("Implementation was called.")
Alex Klein2008aee2019-08-20 16:25:27 -060080
Alex Klein1699fab2022-09-08 08:46:06 -060081 impl(self.request, self.response, self.mock_call_config)
82 self.assertEqual(self.response.result, self._ALL_RESULT)
Alex Klein2008aee2019-08-20 16:25:27 -060083
Alex Klein1699fab2022-09-08 08:46:06 -060084 def test_all_responses_error(self):
85 """Test the call is intercepted by the all responses decorator."""
Alex Klein2008aee2019-08-20 16:25:27 -060086
Alex Klein1699fab2022-09-08 08:46:06 -060087 @faux.all_responses(self._faux_all)
88 def impl(_input_proto, _output_proto, _config):
89 self.fail("Implementation was called.")
Alex Klein2008aee2019-08-20 16:25:27 -060090
Alex Klein1699fab2022-09-08 08:46:06 -060091 impl(self.request, self.response, self.mock_error_config)
92 self.assertEqual(self.response.result, self._ALL_RESULT)
Alex Klein2008aee2019-08-20 16:25:27 -060093
Alex Klein1699fab2022-09-08 08:46:06 -060094 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)