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