Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / tests / test_dependency.py
1 """
2 Copyright (c) 2019 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16
17 from accuracy_checker.dependency import ClassProvider, get_opts
18
19
20 def test_get_opts_positional_and_kwargs():
21     opts = {'o': ((1,), {'a': 1})}
22     args, kwargs = get_opts(opts['o'])
23
24     assert args == (1,)
25     assert kwargs == {'a': 1}
26
27
28 def test_get_opts_kwargs_only():
29     opts = {'o': {'a': 1}}
30     args, kwargs = get_opts(opts['o'])
31
32     assert args == ()
33     assert kwargs == {'a': 1}
34
35
36 def test_get_opts_positional_only():
37     opts = {'o': (1, 2, 3)}
38     args, kwargs = get_opts(opts['o'])
39
40     assert args == (1, 2, 3)
41     assert kwargs == {}
42
43
44 def test_class_provider():
45     class BaseService(ClassProvider):
46         __provider_type__ = 'Service'
47
48     class ServiceA(BaseService):
49         __provider__ = 'service_a'
50
51     class ServiceB(BaseService):
52         __provider__ = 'service_b'
53
54     assert issubclass(ServiceA, BaseService)
55     assert issubclass(ServiceB, BaseService)
56
57     assert 'service_a' in BaseService.providers
58     assert 'service_b' in BaseService.providers
59
60
61 def test_provide():
62     class BaseService(ClassProvider):
63         __provider_type__ = 'service'
64
65         def __init__(self):
66             pass
67
68     class ServiceA(BaseService):
69         __provider__ = 'service_a'
70
71     provided = BaseService.provide('service_a')
72
73     assert isinstance(provided, ServiceA)
74
75
76 def test_provide_with_args():
77     class BaseService(ClassProvider):
78         __provider_type__ = 'service'
79
80         def __init__(self, bar):
81             self.bar = bar
82
83     class ServiceA(BaseService):
84         __provider__ = 'service_a'
85
86     provided = BaseService.provide('service_a', bar=42)
87
88     assert isinstance(provided, ServiceA)
89     assert provided.bar == 42