2 Copyright (c) 2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 from accuracy_checker.dependency import ClassProvider, get_opts
20 def test_get_opts_positional_and_kwargs():
21 opts = {'o': ((1,), {'a': 1})}
22 args, kwargs = get_opts(opts['o'])
25 assert kwargs == {'a': 1}
28 def test_get_opts_kwargs_only():
29 opts = {'o': {'a': 1}}
30 args, kwargs = get_opts(opts['o'])
33 assert kwargs == {'a': 1}
36 def test_get_opts_positional_only():
37 opts = {'o': (1, 2, 3)}
38 args, kwargs = get_opts(opts['o'])
40 assert args == (1, 2, 3)
44 def test_class_provider():
45 class BaseService(ClassProvider):
46 __provider_type__ = 'Service'
48 class ServiceA(BaseService):
49 __provider__ = 'service_a'
51 class ServiceB(BaseService):
52 __provider__ = 'service_b'
54 assert issubclass(ServiceA, BaseService)
55 assert issubclass(ServiceB, BaseService)
57 assert 'service_a' in BaseService.providers
58 assert 'service_b' in BaseService.providers
62 class BaseService(ClassProvider):
63 __provider_type__ = 'service'
68 class ServiceA(BaseService):
69 __provider__ = 'service_a'
71 provided = BaseService.provide('service_a')
73 assert isinstance(provided, ServiceA)
76 def test_provide_with_args():
77 class BaseService(ClassProvider):
78 __provider_type__ = 'service'
80 def __init__(self, bar):
83 class ServiceA(BaseService):
84 __provider__ = 'service_a'
86 provided = BaseService.provide('service_a', bar=42)
88 assert isinstance(provided, ServiceA)
89 assert provided.bar == 42