Alex Klein | 69339cc | 2019-07-22 14:08:35 -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 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 6 | """API config object and related helper functionality.""" |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 10 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 11 | class ApiConfig(object): |
| 12 | """API Config class.""" |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 13 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 14 | def __init__(self, validate_only=False, mock_call=False, mock_error=False): |
| 15 | assert [validate_only, mock_call, mock_error].count(True) <= 1 |
| 16 | self.validate_only = validate_only |
| 17 | self.mock_call = mock_call |
| 18 | self.mock_error = mock_error |
| 19 | self._is_mock = self.mock_call or self.mock_error |
| 20 | |
| 21 | def __eq__(self, other): |
| 22 | if self.__class__ is other.__class__: |
| 23 | return ((self.validate_only, self.mock_call, self.mock_error) == |
| 24 | (other.validate_only, other.mock_call, other.mock_error)) |
| 25 | |
| 26 | return NotImplemented |
| 27 | |
| 28 | __hash__ = NotImplemented |
| 29 | |
| 30 | @property |
| 31 | def do_validation(self): |
| 32 | return not self._is_mock |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class ApiConfigMixin(object): |
| 36 | """Mixin to add an API Config factory properties. |
| 37 | |
| 38 | This is meant to be used for tests to make these configs more uniform across |
| 39 | all of the tests since there's very little to change anyway. |
| 40 | """ |
| 41 | |
| 42 | @property |
| 43 | def api_config(self): |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 44 | return ApiConfig() |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 45 | |
| 46 | @property |
| 47 | def validate_only_config(self): |
| 48 | return ApiConfig(validate_only=True) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame^] | 49 | |
| 50 | @property |
| 51 | def no_validate_config(self): |
| 52 | return self.mock_call_config |
| 53 | |
| 54 | @property |
| 55 | def mock_call_config(self): |
| 56 | return ApiConfig(mock_call=True) |
| 57 | |
| 58 | @property |
| 59 | def mock_error_config(self): |
| 60 | return ApiConfig(mock_error=True) |