Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_plugin.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. from lodel.plugin.plugins import Plugin, PluginError, MetaPlugType,\
  4. VIRTUAL_PACKAGE_NAME, DEFAULT_PLUGINS_PATH_LIST
  5. from lodel.plugin.datasource_plugin import DatasourcePlugin
  6. from lodel.plugin.sessionhandler import SessionHandlerPlugin
  7. from lodel.plugin.interface import InterfacePlugin
  8. from lodel.plugin.extensions import Extension
  9. from lodel.settings.settings import Settings
  10. import tests.loader_utils
  11. from unittest.mock import patch
  12. ##@todo write tests about discovering
  13. ##@todo finish tests for plugin_path
  14. ##@todo finish tests for check_deps
  15. ##@todo write tests for loader_module
  16. ##@todo write tests for load_all (ran upon problems as "dummy_datasource"
  17. ## already is "pre-loaded" (I guess), but cannot be found in
  18. ## _plugin_instances (without doing some other work before, I guess)
  19. class PluginTestCase(unittest.TestCase):
  20. def setUp(self):
  21. Plugin.clear()
  22. self.working_plugins_list = ['dummy', 'dummy_datasource']
  23. def test_check_deps_returns_empty_list_if_no_dependencies(self):
  24. self.assertEqual(list(), Plugin('dummy').check_deps())
  25. def test_loader_module_if_plugin_not_yet_loaded_throws_RuntimeError(self):
  26. self.assertRaises(RuntimeError, Plugin('dummy').loader_module)
  27. def test_start_calls_register_for_each_plugins_from_array(self):
  28. plugin_class = Plugin
  29. with patch.object(Plugin, 'register', wraps=plugin_class.register) as register_wrap:
  30. Plugin.start(self.working_plugins_list)
  31. self.assertEqual(len(self.working_plugins_list), register_wrap.call_count)
  32. def test_clear_effectively_allow_fresh_new_plugin_reloading(self):
  33. Plugin.start(self.working_plugins_list)
  34. Plugin.clear()
  35. Plugin.start(self.working_plugins_list)
  36. def test_register_if_plugin_already_registered_throws_PluginError(self):
  37. Plugin.register('dummy')
  38. self.assertRaises(PluginError, Plugin.register, 'dummy')
  39. def test_register_if_plugin_name_not_in_cache_throws_PluginError(self):
  40. self.assertRaises(PluginError, Plugin.register, 'azerty')
  41. def test_register_if_ptype_not_known_throws_PluginError(self):
  42. with patch.object(MetaPlugType, 'all_ptype_names', return_value=[]) as mock_method:
  43. self.assertRaises(PluginError, Plugin.register, 'dummy')
  44. def test_register_returns_Plugin_child_object(self):
  45. self.assertTrue(issubclass(Plugin.register('dummy_datasource').__class__, Plugin))
  46. def test_get_if_no_plugin_found_throws_KeyError(self):
  47. self.assertRaises(PluginError, Plugin.get, 'foo')
  48. def test_get_returns_proper_plugin_instance(self):
  49. Plugin.register('dummy')
  50. self.assertTrue(Plugin.get('dummy').__class__, Plugin)
  51. def test_plugin_path_if_no_plugin_name_found_throws_PluginError(self):
  52. self.assertRaises(PluginError, Plugin.plugin_path, 'foo')
  53. def test_plugin_module_name_correctly_returns_module_name_string_from_plugin_name(self):
  54. self.assertEqual(Plugin.plugin_module_name('foo'), "%s.%s" % (VIRTUAL_PACKAGE_NAME, 'foo'))
  55. def test_discover_if_paths_is_none_default_is_used(self):
  56. with patch.object(Plugin, '_discover', wraps=Plugin._discover) as _discover_wrap:
  57. Plugin.discover(DEFAULT_PLUGINS_PATH_LIST)
  58. _discover_wrap.assert_called_with(DEFAULT_PLUGINS_PATH_LIST[-1])
  59. def test_discover_if_paths_is_set_properly_search_in(self):
  60. paths = ['.', '.']
  61. with patch.object(Plugin, '_discover', wraps=Plugin._discover) as _discover_wrap:
  62. Plugin.discover(paths)
  63. _discover_wrap.assert_called_with(paths[-1])
  64. def test_discover_if_no_plugins_found_still_returns_searched_paths(self):
  65. paths = ['/home/quentin/Pictures']
  66. dfoi = Plugin.discover(paths)
  67. self.assertEqual(dfoi['path_list'], paths)
  68. def test_discover_if_no_plugins_found_returns_empty_plugin_dict(self):
  69. dfoi = Plugin.discover(['/home/quentin/Pictures'])
  70. self.assertEqual(dfoi['plugins'], {})
  71. def test_discover_if_no_plugins_found_returns_empty_plugin(self):
  72. dfoi = Plugin.discover(['./plugins'])
  73. self.maxDiff = None
  74. self.assertEqual(dfoi['plugins'], {})
  75. class PluginStartedTestCase(unittest.TestCase):
  76. """ Test case grouping all tests on a started Plugin class """
  77. @classmethod
  78. def setUpClass(cls):
  79. Plugin.clear()
  80. Plugin.start(['dummy', 'dummy_datasource', 'webui', 'ram_sessions'])
  81. @classmethod
  82. def tearDownClass(cls):
  83. Plugin.clear()
  84. def test_construct(self):
  85. """ Testing plugin instanciation """
  86. pname_type = {
  87. 'dummy': Extension,
  88. 'dummy_datasource': DatasourcePlugin,
  89. #'webui': InterfacePlugin, #singleton, cannot reinstanciate
  90. #'ram_session': SessionHandlerPlugin, #singleton, cannot resintanciate
  91. }
  92. for pname, ptype in pname_type.items():
  93. pinstance = Plugin.get(pname)
  94. self.assertIsInstance(pinstance, ptype, "Expected plugin '%s' \
  95. to be in an %s instance but found an %s instance" % (
  96. pname, ptype, pinstance.__class__))
  97. def test_construct_invalid(self):
  98. """ Testing plugin instanciation with a non existing name """
  99. with self.assertRaises(PluginError):
  100. Plugin.get("fljkhsfh")