-
Notifications
You must be signed in to change notification settings - Fork 7
/
tests.py
443 lines (347 loc) · 13.8 KB
/
tests.py
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# coding: utf-8
# Module: tests
# Created on: 27.01.2016
# Author: Roman Miroshnychenko aka Roman V.M. ([email protected])
from __future__ import print_function
import os
import sys
import unittest
import shutil
import time
from collections import defaultdict
import mock
cwd = os.path.dirname(os.path.abspath(__file__))
configdir = os.path.join(cwd, 'config')
# Fake test objects
def fake_log(msg, level=0):
if not isinstance(msg, str):
raise TypeError('log message must be of str type!')
def fake_getInfoLabel(label):
if label == 'System.BuildVersion':
return '18.3 Git:fake-commit'
return ''
class FakeAddon(object):
def __init__(self, id_='test.addon'):
self._id = id_
self._settings = {}
def getAddonInfo(self, info_id):
if info_id == 'path':
return cwd
elif info_id == 'profile':
return configdir
elif info_id == 'id':
return self._id
elif info_id == 'version':
return '0.0.1'
else:
return ''
def getSetting(self, setting_id):
return self._settings.get(setting_id, '')
def setSetting(self, setting_id, value):
self._settings[setting_id] = value
def getLocalizedString(self, id_):
return {32000: u'Привет, мир!', 32001: u'Я тебя люблю.'}[id_]
class FakeWindow(object):
def __init__(self, id_=-1):
self._contents = defaultdict(str)
def getProperty(self, key):
return self._contents[key]
def setProperty(self, key, value):
self._contents[key] = value
def clearProperty(self, key):
del self._contents[key]
# Mock Kodi Python API
mock_xbmcaddon = mock.MagicMock()
mock_xbmcaddon.Addon.side_effect = FakeAddon
mock_xbmc = mock.MagicMock()
mock_xbmc.LOGDEBUG = 0
mock_xbmc.LOGNOTICE = 2
mock_xbmc.translatePath.side_effect = lambda path: path
mock_xbmc.log = fake_log
mock_xbmc.getInfoLabel = fake_getInfoLabel
mock_xbmcgui = mock.MagicMock()
mock_xbmcgui.Window = FakeWindow
mock_xbmcvfs = mock.MagicMock()
sys.modules['xbmcaddon'] = mock_xbmcaddon
sys.modules['xbmc'] = mock_xbmc
sys.modules['xbmcgui'] = mock_xbmcgui
sys.modules['xbmcvfs'] = mock_xbmcvfs
# Import our module being tested
sys.path.append(os.path.join(cwd, 'script.module.simpleplugin3', 'libs'))
from simpleplugin import (Storage, Addon, Plugin, RoutedPlugin,
SimplePluginError, MemStorage, log_exception)
# Begin tests
class StorageTestCase(unittest.TestCase):
"""
Test Storage class
"""
def tearDown(self):
try:
os.remove(os.path.join(cwd, 'storage.pcl'))
except OSError:
pass
def test_storage_initialization(self):
with Storage(cwd) as storage:
storage['foo'] = 'bar'
self.assertTrue(os.path.exists(os.path.join(cwd, 'storage.pcl')))
def test_storing_value_in_storage_(self):
with Storage(cwd) as storage1:
storage1['foo'] = 'bar'
with Storage(cwd) as storage2:
self.assertEqual(storage2['foo'], 'bar')
def test_reading_storage_without_changes(self):
with Storage(cwd) as storage1:
storage1['foo'] = 'bar'
last_mod = os.path.getmtime('storage.pcl')
with Storage(cwd) as storage2:
bar = storage2['foo']
self.assertEqual(os.path.getmtime('storage.pcl'), last_mod)
def test_storage_iteration(self):
with Storage(cwd) as storage:
storage['foo'] = 'foo'
storage['bar'] = 'bar'
storage['baz'] = 'baz'
i = 0
for key, value in storage.items():
self.assertEqual(key, value)
i += 1
self.assertEqual(i, 3)
class MemStorageTestCase(unittest.TestCase):
def test_mem_storage(self):
storage = MemStorage('foo')
storage['ham'] = 'spam'
self.assertEqual(storage['ham'], 'spam')
self.assertTrue('ham' in storage)
print(storage) # Test __str__ method
del storage['ham']
self.assertFalse('ham' in storage)
self.assertRaises(KeyError, storage.__getitem__, 'bar')
self.assertRaises(KeyError, storage.__delitem__, 'bar')
self.assertRaises(TypeError, storage.__getitem__, 5)
self.assertRaises(TypeError, storage.__setitem__, 5, 5)
self.assertRaises(TypeError, storage.__delitem__, 5)
self.assertRaises(TypeError, storage.__contains__, 5)
class AddonTestCase(unittest.TestCase):
"""
Test Addon class
"""
def tearDown(self):
shutil.rmtree(configdir, True)
try:
os.remove('icon.png')
os.remove('fanart.jpg')
except OSError:
pass
def test_addon_instance_creation(self):
addon = Addon('test.addon')
self.assertEqual(addon.id, 'test.addon')
self.assertTrue(os.path.exists(os.path.join(cwd, 'config')))
def test_addon_get_setting(self):
"""
Test addon settings normalization
"""
addon = Addon()
addon.addon.setSetting('test', 'true')
self.assertEqual(addon.get_setting('test'), True)
addon.addon.setSetting('test', 'false')
self.assertEqual(addon.get_setting('test'), False)
addon.addon.setSetting('test', '10')
self.assertEqual(addon.get_setting('test'), 10)
addon.addon.setSetting('test', '1.0')
self.assertEqual(addon.get_setting('test'), 1.0)
addon.addon.setSetting('test', 'foo')
self.assertEqual(addon.get_setting('test'), 'foo')
def test_addon_set_setting(self):
"""
Test saving addon settings
"""
addon = Addon()
addon.set_setting('test', True)
self.assertEqual(addon.addon.getSetting('test'), 'true')
addon.set_setting('test', False)
self.assertEqual(addon.addon.getSetting('test'), 'false')
addon.set_setting('test', 10)
self.assertEqual(addon.addon.getSetting('test'), '10')
def test_cached_decorator(self):
addon = Addon()
@addon.cached()
def tester(*args):
return str(time.time())
test1 = tester('arg1')
time.sleep(0.5)
self.assertEqual(tester('arg1'), test1)
self.assertNotEqual(tester('arg2'), test1)
def test_mem_cached_decorator(self):
with mock.patch.object(Addon, 'get_mem_storage', return_value={}):
addon = Addon()
@addon.mem_cached()
def tester(*args):
return str(time.time())
test1 = tester('arg1')
time.sleep(0.5)
self.assertEqual(tester('arg1'), test1)
self.assertNotEqual(tester('arg2'), test1)
def test_icon_fanart_properties(self):
addon = Addon()
self.assertEqual(addon.icon, '')
self.assertEqual(addon.fanart, '')
open('icon.png', 'w').close()
open('fanart.jpg', 'w').close()
self.assertTrue('icon.png' in addon.icon)
self.assertTrue('fanart.jpg' in addon.fanart)
def test_gettext_not_initialized(self):
addon = Addon()
self.assertRaises(SimplePluginError, addon.gettext, 'Hello World!')
def test_gettext_initialized(self):
addon = Addon()
_ = addon.initialize_gettext()
self.assertEqual(_('Hello World!'), u'Привет, мир!')
self.assertEqual(_('I love you.'), u'Я тебя люблю.')
self.assertRaises(SimplePluginError, _, 'Foo Bar')
class PluginTestCase(unittest.TestCase):
"""
Test Plugin class
"""
def tearDown(self):
shutil.rmtree(configdir, True)
def test_get_params(self, *args):
params = Plugin.get_params('param1=value1¶m1=value2¶m3=value3')
self.assertEqual(params['param1'], ['value1', 'value2'])
self.assertEqual(params['param3'], 'value3')
def test_get_url(self, *args):
plugin = Plugin('test.plugin')
self.assertEqual(plugin.get_url(), 'plugin://test.plugin/')
self.assertEqual(plugin.get_url(action='test'), 'plugin://test.plugin/?action=test')
self.assertEqual(plugin.get_url('plugin://other.plugin/', param='value'), 'plugin://other.plugin/?param=value')
def test_run(self):
plugin = Plugin('test.plugin')
mock_actions = mock.MagicMock()
mock_actions.root.return_value = None
mock_actions.foo.return_value = None
plugin.actions['root'] = mock_actions.root
plugin.actions['foo'] = mock_actions.foo
# Test calling 'root' action
with mock.patch('simpleplugin.sys.argv', ['test.plugin', '1', '']):
plugin.run()
mock_actions.root.assert_called_with({})
# Test calling an action with parameters
with mock.patch('simpleplugin.sys.argv', ['test.plugin', '1', '?action=foo¶m=bar']):
plugin.run()
mock_actions.foo.assert_called_with({'action': 'foo', 'param': 'bar'})
def test_action_decorator(self):
plugin = Plugin('test.plugin')
@plugin.action()
def foo(params):
raise AssertionError('Test passed!')
try:
@plugin.action('foo')
def bar(params):
pass
except SimplePluginError:
pass
else:
self.fail('Duplicate action names test failed!')
with mock.patch('simpleplugin.sys.argv', ['test.plugin', '1', '?action=foo']):
self.assertRaises(AssertionError, plugin.run)
class RoutedPluginTestCase(unittest.TestCase):
def tearDown(self):
shutil.rmtree(configdir, True)
def test_simple_routing(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/foo')
def test_func():
raise AssertionError('Test passed!')
with mock.patch('simpleplugin.sys.argv', ['plugin://test.plugin/foo', '1', '']):
self.assertRaises(AssertionError, plugin.run)
def test_passing_arguments(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/foo/<param1>/<param2>')
def test_func(param1, param2):
self.assertEqual(param1, 'ham')
self.assertEqual(param2, u'спам')
self.assertEqual(plugin.params.param3, u'Привет, мир!')
with mock.patch('simpleplugin.sys.argv',
['plugin://test.plugin/foo/ham/%D1%81%D0%BF%D0%B0%D0%BC', '1', '?param3=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%2C+%D0%BC%D0%B8%D1%80%21']):
plugin.run()
def test_passing_int_and_float(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/foo/<int:param1>/<float:param2>')
def test_func(param1, param2):
self.assertEqual(param1, 28)
self.assertEqual(param2, 3.1416)
with mock.patch('simpleplugin.sys.argv', ['plugin://test.plugin/foo/28/3.1416', '1', '']):
plugin.run()
def test_multiple_routes(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/foo', name='foo_route')
@plugin.route('/bar/<param>')
def test_func(param='ham'):
self.assertEqual(param, 'ham')
with mock.patch('simpleplugin.sys.argv', ['plugin://test.plugin/foo', '1', '']):
plugin.run()
with mock.patch('simpleplugin.sys.argv', ['plugin://test.plugin/bar/spam', '1', '']):
self.assertRaises(AssertionError, plugin.run)
def test_routes_with_same_name(self):
plugin = RoutedPlugin('test.plugin')
try:
@plugin.route('/foo')
@plugin.route('/bar')
def test_func():
pass
except SimplePluginError:
pass
else:
self.fail('Added 2 routes with the same name!')
class PluginUrlForTestCase(unittest.TestCase):
def tearDown(self):
shutil.rmtree(configdir, True)
def test_building_simple_url(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/foo')
def test():
pass
self.assertEqual(plugin.url_for('test'), 'plugin://test.plugin/foo')
self.assertEqual(plugin.url_for(test), 'plugin://test.plugin/foo')
def test_building_url_args(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/<param1>/<param2>')
def test():
pass
url = plugin.url_for('test', 'foo', u'тест')
self.assertEqual(url, u'plugin://test.plugin/foo/%D1%82%D0%B5%D1%81%D1%82')
def test_building_url_kwargs(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/<param1>/<param2>')
def test():
pass
url = plugin.url_for('test', param1='foo', param2='bar')
self.assertEqual(url, 'plugin://test.plugin/foo/bar')
def test_building_url_args_kwargs(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/<param1>/<param2>/<param3>')
def test():
pass
url = plugin.url_for('test', 'foo', param2='bar', param3='spam')
self.assertEqual(url, 'plugin://test.plugin/foo/bar/spam')
def test_building_url_args_kwargs_query(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/<param1>/<param2>/<param3>')
def test():
pass
url = plugin.url_for('test', 'foo', param2='bar', param3='spam', param4='ham')
self.assertEqual(url, 'plugin://test.plugin/foo/bar/spam?param4=ham')
def test_building_url_int_float(self):
plugin = RoutedPlugin('test.plugin')
@plugin.route('/<int:param1>/<float:param2>')
def test():
pass
url = plugin.url_for('test', param1=1, param2=3.14)
self.assertEqual(url, 'plugin://test.plugin/1/3.14')
class DebugExceptionTestCase(unittest.TestCase):
def test_debug_exception(self):
def test_func():
with log_exception():
raise RuntimeError
self.assertRaises(RuntimeError, test_func)
if __name__ == '__main__':
unittest.main()