blob: 6880391ffa61e950d21dca23d1ccb0b0b6cc8f19 [file] [log] [blame]
Alex Klein69339cc2019-07-22 14:08:35 -06001# -*- 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 Klein2008aee2019-08-20 16:25:27 -06006"""API config object and related helper functionality."""
Alex Klein69339cc2019-07-22 14:08:35 -06007
8from __future__ import print_function
9
Alex Klein69339cc2019-07-22 14:08:35 -060010
Alex Klein2008aee2019-08-20 16:25:27 -060011class ApiConfig(object):
12 """API Config class."""
Alex Klein69339cc2019-07-22 14:08:35 -060013
Alex Klein2008aee2019-08-20 16:25:27 -060014 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 Klein69339cc2019-07-22 14:08:35 -060033
34
35class 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 Klein2008aee2019-08-20 16:25:27 -060044 return ApiConfig()
Alex Klein69339cc2019-07-22 14:08:35 -060045
46 @property
47 def validate_only_config(self):
48 return ApiConfig(validate_only=True)
Alex Klein2008aee2019-08-20 16:25:27 -060049
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)