Debian Codegraph @

tests/test_enumerate.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010, 2011, 2012 Sebastian Wiesner <[email protected]>
3
4 # This library is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU Lesser General Public License as published by the
6 # Free Software Foundation; either version 2.1 of the License, or (at your
7 # option) any later version.
8
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 # for more details.
13
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18 # isort: THIRDPARTY
19 from hypothesis import given, settings, strategies
20
21 from ._constants import (
22 _ATTRIBUTE_STRATEGY,
23 _CONTEXT_STRATEGY,
24 _MATCH_PROPERTY_STRATEGY,
25 _SUBSYSTEM_STRATEGY,
26 _SYSNAME_STRATEGY,
27 _TAG_STRATEGY,
28 _UDEV_TEST,
29 device_strategy,
30 )
31 from .utils import failed_health_check_wrapper
32
33 try:
34 # isort: STDLIB
35 from unittest import mock
36 except ImportError:
37 # isort: THIRDPARTY
38 import mock
39
40
41 def _is_int(value):
42 try:
43 int(value)
44 return True
45 except (TypeError, ValueError):
46 return False
47
48
49 def _is_bool(value):
50 try:
51 return int(value) in (0, 1)
52 except (TypeError, ValueError):
53 return False
54
55
56 def _test_direct_and_complement(context, devices, func):
57 """
58 Test that results are correct and that complement holds.
59
60 :param Context context: the libudev context
61 :param devices: the devices that match
62 :type devices: frozenset of Device
63 :param func: the property to test
64 :type func: device -> bool
65 """
66 assert [device for device in devices if not func(device)] == []
67 complement = frozenset(context.list_devices()) - devices
68 assert [device for device in complement if func(device)] == []
69
70
71 def _test_intersection_and_union(context, matches, nomatches):
72 """
73 Test that intersection is empty and union is all of devices.
74
75 :param matches: the matching devices
76 :type matches: frozenset of Device
77 :param nomatches: the non-matching devices
78 :type nomatches: frozenset of Device
79 """
80 assert matches & nomatches == frozenset()
81 assert matches | nomatches == frozenset(context.list_devices())
82
83
84 class TestEnumerator:
85 """
86 Test the Enumerator class.
87 """
88
89 @failed_health_check_wrapper
90 @given(_CONTEXT_STRATEGY, _SUBSYSTEM_STRATEGY)
91 @settings(max_examples=10)
92 def test_match_subsystem(self, context, subsystem):
93 """
94 Subsystem match matches devices w/ correct subsystem.
95 """
96 _test_direct_and_complement(
97 context,
98 frozenset(context.list_devices().match_subsystem(subsystem)),
99 lambda d: d.subsystem == subsystem,
100 )
101
102 @failed_health_check_wrapper
103 @given(_CONTEXT_STRATEGY, _SUBSYSTEM_STRATEGY)
104 @settings(max_examples=5)
105 def test_match_subsystem_nomatch_unfulfillable(self, context, subsystem):
106 """
107 Combining match and no match should give an empty result.
108 """
109 devices = context.list_devices()
110 devices.match_subsystem(subsystem)
111 devices.match_subsystem(subsystem, nomatch=True)
112 assert not list(devices)
113
114 @failed_health_check_wrapper
115 @given(_CONTEXT_STRATEGY, _SUBSYSTEM_STRATEGY)
116 @settings(max_examples=1)
117 def test_match_subsystem_nomatch_complete(self, context, subsystem):
118 """
119 Test that w/ respect to the universe of devices returned by
120 list_devices() a match and its inverse are complements of each other.
121
122 Note that list_devices() omits devices that have no subsystem,
123 so with respect to the whole universe of devices, the two are
124 not complements of each other.
125 """
126 m_devices = frozenset(context.list_devices().match_subsystem(subsystem))
127 nm_devices = frozenset(
128 context.list_devices().match_subsystem(subsystem, nomatch=True)
129 )
130 _test_intersection_and_union(context, m_devices, nm_devices)
131
132 @failed_health_check_wrapper
133 @given(_CONTEXT_STRATEGY, _MATCH_PROPERTY_STRATEGY.filter(lambda x: _is_bool(x[1])))
134 @settings(max_examples=10)
135 def test_match_property_bool(self, context, pair):
136 """
137 Verify that a probably boolean property lookup works.
138 """
139 key, value = pair
140 bool_value = True if int(value) == 1 else False
141 devices = context.list_devices().match_property(key, bool_value)
142 assert all(
143 device.properties[key] == value
144 and device.properties.asbool(key) == bool_value
145 for device in devices
146 )
147
148 @failed_health_check_wrapper
149 @given(
150 _CONTEXT_STRATEGY, device_strategy(filter_func=lambda d: d.parent is not None)
151 )
152 @settings(max_examples=5)
153 def test_match_parent(self, context, device):
154 """
155 For a given device, verify that it is in its parent's children.
156
157 Verify that the parent is also among the device's children,
158 unless the parent lacks a subsystem
159
160 See: rhbz#1255191
161 """
162 parent = device.parent
163 children = list(context.list_devices().match_parent(parent))
164 assert device in children
165
166
167 class TestEnumeratorMatchCombinations:
168 """
169 Test combinations of matches.
170 """
171
172 @given(
173 _CONTEXT_STRATEGY,
174 _SUBSYSTEM_STRATEGY,
175 _SYSNAME_STRATEGY,
176 _MATCH_PROPERTY_STRATEGY,
177 )
178 @settings(max_examples=10)
179 def test_match(self, context, subsystem, sysname, ppair):
180 """
181 Test that matches from different categories are a conjunction.
182 """
183 prop_name, prop_value = ppair
184 kwargs = {prop_name: prop_value}
185 devices = frozenset(
186 context.list_devices().match(
187 subsystem=subsystem, sys_name=sysname, **kwargs
188 )
189 )
190 _test_direct_and_complement(
191 context,
192 devices,
193 lambda d: d.subsystem == subsystem
194 and d.sys_name == sysname
195 and d.properties.get(prop_name) == prop_value,
196 )
197
198
199 class TestEnumeratorMatchMethod:
200 """
201 Test the behavior of Enumerator.match.
202
203 Only methods that test behavior of this method by patching the Enumerator
204 object with the methods that match() should invoke belong here.
205 """
206
207 _ENUMERATOR_STRATEGY = _CONTEXT_STRATEGY.map(lambda x: x.list_devices())
208
209 @given(_ENUMERATOR_STRATEGY)
210 @settings(max_examples=1)
211 def test_match_passthrough_subsystem(self, enumerator):
212 """
213 Test that special keyword subsystem results in a match_subsystem call.
214 """
215 with mock.patch.object(
216 enumerator, "match_subsystem", autospec=True
217 ) as match_subsystem:
218 enumerator.match(subsystem=mock.sentinel.subsystem)
219 match_subsystem.assert_called_with(mock.sentinel.subsystem)
220
221 @given(_ENUMERATOR_STRATEGY)
222 @settings(max_examples=1)
223 def test_match_passthrough_sys_name(self, enumerator):
224 """
225 Test that special keyword sys_name results in a match_sys_name call.
226 """
227 with mock.patch.object(
228 enumerator, "match_sys_name", autospec=True
229 ) as match_sys_name:
230 enumerator.match(sys_name=mock.sentinel.sys_name)
231 match_sys_name.assert_called_with(mock.sentinel.sys_name)
232
233 @given(_ENUMERATOR_STRATEGY)
234 @settings(max_examples=1)
235 def test_match_passthrough_tag(self, enumerator):
236 """
237 Test that special keyword tag results in a match_tag call.
238 """
239 with mock.patch.object(enumerator, "match_tag", autospec=True) as match_tag:
240 enumerator.match(tag=mock.sentinel.tag)
241 match_tag.assert_called_with(mock.sentinel.tag)
242
243 @_UDEV_TEST(172, "test_match_passthrough_parent")
244 @given(_ENUMERATOR_STRATEGY)
245 @settings(max_examples=1)
246 def test_match_passthrough_parent(self, enumerator):
247 """
248 Test that special keyword 'parent' results in a match parent call.
249 """
250 with mock.patch.object(
251 enumerator, "match_parent", autospec=True
252 ) as match_parent:
253 enumerator.match(parent=mock.sentinel.parent)
254 match_parent.assert_called_with(mock.sentinel.parent)
255
256 @given(_ENUMERATOR_STRATEGY)
257 @settings(max_examples=1)
258 def test_match_passthrough_property(self, enumerator):
259 """
260 Test that non-special keyword args are treated as properties.
261 """
262 with mock.patch.object(
263 enumerator, "match_property", autospec=True
264 ) as match_property:
265 enumerator.match(eggs=mock.sentinel.eggs, spam=mock.sentinel.spam)
266 assert match_property.call_count == 2
267 posargs = [args for args, _ in match_property.call_args_list]
268 assert ("spam", mock.sentinel.spam) in posargs
269 assert ("eggs", mock.sentinel.eggs) in posargs