No Description
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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. from lodel.plugin.plugins import Plugin, PluginError, MetaPlugType,\
  4. VIRTUAL_PACKAGE_NAME, PLUGINS_PATH
  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. class PluginStartedTestCase(unittest.TestCase):
  56. """ Test case grouping all tests on a started Plugin class """
  57. @classmethod
  58. def setUpClass(cls):
  59. Plugin.clear()
  60. Plugin.start(['dummy', 'dummy_datasource', 'webui', 'ram_sessions'])
  61. @classmethod
  62. def tearDownClass(cls):
  63. Plugin.clear()
  64. def test_construct(self):
  65. """ Testing plugin instanciation """
  66. pname_type = {
  67. 'dummy': Extension,
  68. 'dummy_datasource': DatasourcePlugin,
  69. #'webui': InterfacePlugin, #singleton, cannot reinstanciate
  70. #'ram_session': SessionHandlerPlugin, #singleton, cannot resintanciate
  71. }
  72. for pname, ptype in pname_type.items():
  73. pinstance = Plugin.get(pname)
  74. self.assertIsInstance(pinstance, ptype, "Expected plugin '%s' \
  75. to be in an %s instance but found an %s instance" % (
  76. pname, ptype, pinstance.__class__))
  77. def test_construct_invalid(self):
  78. """ Testing plugin instanciation with a non existing name """
  79. with self.assertRaises(PluginError):
  80. Plugin.get("fljkhsfh")