forked from mopidy/mopidy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ext.py
More file actions
273 lines (188 loc) · 9.17 KB
/
test_ext.py
File metadata and controls
273 lines (188 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from __future__ import absolute_import, unicode_literals
import os
import mock
import pkg_resources
import pytest
from mopidy import config, exceptions, ext
from tests import IsA, any_unicode
class DummyExtension(ext.Extension):
dist_name = 'Mopidy-Foobar'
ext_name = 'foobar'
version = '1.2.3'
def get_default_config(self):
return '[foobar]\nenabled = true'
any_testextension = IsA(DummyExtension)
class TestExtension(object):
@pytest.fixture
def extension(self):
return ext.Extension()
def test_dist_name_is_none(self, extension):
assert extension.dist_name is None
def test_ext_name_is_none(self, extension):
assert extension.ext_name is None
def test_version_is_none(self, extension):
assert extension.version is None
def test_get_default_config_raises_not_implemented(self, extension):
with pytest.raises(NotImplementedError):
extension.get_default_config()
def test_get_config_schema_returns_extension_schema(self, extension):
schema = extension.get_config_schema()
assert isinstance(schema['enabled'], config.Boolean)
def test_validate_environment_does_nothing_by_default(self, extension):
assert extension.validate_environment() is None
def test_setup_raises_not_implemented(self, extension):
with pytest.raises(NotImplementedError):
extension.setup(None)
def test_get_cache_dir_raises_assertion_error(self, extension):
config = {'core': {'cache_dir': '/tmp'}}
with pytest.raises(AssertionError): # ext_name not set
ext.Extension.get_cache_dir(config)
def test_get_config_dir_raises_assertion_error(self, extension):
config = {'core': {'config_dir': '/tmp'}}
with pytest.raises(AssertionError): # ext_name not set
ext.Extension.get_config_dir(config)
def test_get_data_dir_raises_assertion_error(self, extension):
config = {'core': {'data_dir': '/tmp'}}
with pytest.raises(AssertionError): # ext_name not set
ext.Extension.get_data_dir(config)
class TestLoadExtensions(object):
@pytest.yield_fixture
def iter_entry_points_mock(self, request):
patcher = mock.patch('pkg_resources.iter_entry_points')
iter_entry_points = patcher.start()
iter_entry_points.return_value = []
yield iter_entry_points
patcher.stop()
def test_no_extensions(self, iter_entry_points_mock):
iter_entry_points_mock.return_value = []
assert ext.load_extensions() == []
def test_load_extensions(self, iter_entry_points_mock):
mock_entry_point = mock.Mock()
mock_entry_point.load.return_value = DummyExtension
iter_entry_points_mock.return_value = [mock_entry_point]
expected = ext.ExtensionData(
any_testextension, mock_entry_point, IsA(config.ConfigSchema),
any_unicode, None)
assert ext.load_extensions() == [expected]
def test_gets_wrong_class(self, iter_entry_points_mock):
class WrongClass(object):
pass
mock_entry_point = mock.Mock()
mock_entry_point.load.return_value = WrongClass
iter_entry_points_mock.return_value = [mock_entry_point]
assert ext.load_extensions() == []
def test_gets_instance(self, iter_entry_points_mock):
mock_entry_point = mock.Mock()
mock_entry_point.load.return_value = DummyExtension()
iter_entry_points_mock.return_value = [mock_entry_point]
assert ext.load_extensions() == []
def test_creating_instance_fails(self, iter_entry_points_mock):
mock_extension = mock.Mock(spec=ext.Extension)
mock_extension.side_effect = Exception
mock_entry_point = mock.Mock()
mock_entry_point.load.return_value = mock_extension
iter_entry_points_mock.return_value = [mock_entry_point]
assert ext.load_extensions() == []
def test_get_config_schema_fails(self, iter_entry_points_mock):
mock_entry_point = mock.Mock()
mock_entry_point.load.return_value = DummyExtension
iter_entry_points_mock.return_value = [mock_entry_point]
with mock.patch.object(DummyExtension, 'get_config_schema') as get:
get.side_effect = Exception
assert ext.load_extensions() == []
get.assert_called_once_with()
def test_get_default_config_fails(self, iter_entry_points_mock):
mock_entry_point = mock.Mock()
mock_entry_point.load.return_value = DummyExtension
iter_entry_points_mock.return_value = [mock_entry_point]
with mock.patch.object(DummyExtension, 'get_default_config') as get:
get.side_effect = Exception
assert ext.load_extensions() == []
get.assert_called_once_with()
def test_get_command_fails(self, iter_entry_points_mock):
mock_entry_point = mock.Mock()
mock_entry_point.load.return_value = DummyExtension
iter_entry_points_mock.return_value = [mock_entry_point]
with mock.patch.object(DummyExtension, 'get_command') as get:
get.side_effect = Exception
assert ext.load_extensions() == []
get.assert_called_once_with()
class TestValidateExtensionData(object):
@pytest.fixture
def ext_data(self):
extension = DummyExtension()
entry_point = mock.Mock()
entry_point.name = extension.ext_name
schema = extension.get_config_schema()
defaults = extension.get_default_config()
command = extension.get_command()
return ext.ExtensionData(
extension, entry_point, schema, defaults, command)
def test_name_mismatch(self, ext_data):
ext_data.entry_point.name = 'barfoo'
assert not ext.validate_extension_data(ext_data)
def test_distribution_not_found(self, ext_data):
error = pkg_resources.DistributionNotFound
ext_data.entry_point.require.side_effect = error
assert not ext.validate_extension_data(ext_data)
def test_version_conflict(self, ext_data):
error = pkg_resources.VersionConflict
ext_data.entry_point.require.side_effect = error
assert not ext.validate_extension_data(ext_data)
def test_entry_point_require_exception(self, ext_data):
ext_data.entry_point.require.side_effect = Exception
# Hope that entry points are well behaved, so exception will bubble.
with pytest.raises(Exception):
assert not ext.validate_extension_data(ext_data)
def test_extenions_validate_environment_error(self, ext_data):
extension = ext_data.extension
with mock.patch.object(extension, 'validate_environment') as validate:
validate.side_effect = exceptions.ExtensionError('error')
assert not ext.validate_extension_data(ext_data)
validate.assert_called_once_with()
def test_extenions_validate_environment_exception(self, ext_data):
extension = ext_data.extension
with mock.patch.object(extension, 'validate_environment') as validate:
validate.side_effect = Exception
assert not ext.validate_extension_data(ext_data)
validate.assert_called_once_with()
def test_missing_schema(self, ext_data):
ext_data = ext_data._replace(config_schema=None)
assert not ext.validate_extension_data(ext_data)
def test_schema_that_is_missing_enabled(self, ext_data):
del ext_data.config_schema['enabled']
ext_data.config_schema['baz'] = config.String()
assert not ext.validate_extension_data(ext_data)
def test_schema_with_wrong_types(self, ext_data):
ext_data.config_schema['enabled'] = 123
assert not ext.validate_extension_data(ext_data)
def test_schema_with_invalid_type(self, ext_data):
ext_data.config_schema['baz'] = 123
assert not ext.validate_extension_data(ext_data)
def test_no_default_config(self, ext_data):
ext_data = ext_data._replace(config_defaults=None)
assert not ext.validate_extension_data(ext_data)
def test_get_cache_dir(self, ext_data):
core_cache_dir = '/tmp'
config = {'core': {'cache_dir': core_cache_dir}}
extension = ext_data.extension
with mock.patch.object(ext.path, 'get_or_create_dir'):
cache_dir = extension.get_cache_dir(config)
expected = os.path.join(core_cache_dir, extension.ext_name)
assert cache_dir == expected
def test_get_config_dir(self, ext_data):
core_config_dir = '/tmp'
config = {'core': {'config_dir': core_config_dir}}
extension = ext_data.extension
with mock.patch.object(ext.path, 'get_or_create_dir'):
config_dir = extension.get_config_dir(config)
expected = os.path.join(core_config_dir, extension.ext_name)
assert config_dir == expected
def test_get_data_dir(self, ext_data):
core_data_dir = '/tmp'
config = {'core': {'data_dir': core_data_dir}}
extension = ext_data.extension
with mock.patch.object(ext.path, 'get_or_create_dir'):
data_dir = extension.get_data_dir(config)
expected = os.path.join(core_data_dir, extension.ext_name)
assert data_dir == expected