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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import unittest
  20. from lodel.plugin.plugins import Plugin, PluginError, MetaPlugType,\
  21. VIRTUAL_PACKAGE_NAME, PLUGINS_PATH
  22. from lodel.plugin.datasource_plugin import DatasourcePlugin
  23. from lodel.plugin.sessionhandler import SessionHandlerPlugin
  24. from lodel.plugin.interface import InterfacePlugin
  25. from lodel.plugin.extensions import Extension
  26. from lodel.settings.settings import Settings
  27. import tests.loader_utils
  28. from unittest.mock import patch
  29. ##@todo write tests about discovering
  30. ##@todo finish tests for plugin_path
  31. ##@todo finish tests for check_deps
  32. ##@todo write tests for loader_module
  33. ##@todo write tests for load_all (ran upon problems as "dummy_datasource"
  34. ## already is "pre-loaded" (I guess), but cannot be found in
  35. ## _plugin_instances (without doing some other work before, I guess)
  36. class PluginTestCase(unittest.TestCase):
  37. def setUp(self):
  38. Plugin.clear()
  39. self.working_plugins_list = ['dummy', 'dummy_datasource']
  40. def test_check_deps_returns_empty_list_if_no_dependencies(self):
  41. self.assertEqual(list(), Plugin('dummy').check_deps())
  42. def test_loader_module_if_plugin_not_yet_loaded_throws_RuntimeError(self):
  43. self.assertRaises(RuntimeError, Plugin('dummy').loader_module)
  44. def test_start_calls_register_for_each_plugins_from_array(self):
  45. plugin_class = Plugin
  46. with patch.object(Plugin, 'register', wraps=plugin_class.register) as register_wrap:
  47. Plugin.start(self.working_plugins_list)
  48. self.assertEqual(len(self.working_plugins_list), register_wrap.call_count)
  49. def test_clear_effectively_allow_fresh_new_plugin_reloading(self):
  50. Plugin.start(self.working_plugins_list)
  51. Plugin.clear()
  52. Plugin.start(self.working_plugins_list)
  53. def test_register_if_plugin_already_registered_throws_PluginError(self):
  54. Plugin.register('dummy')
  55. self.assertRaises(PluginError, Plugin.register, 'dummy')
  56. def test_register_if_plugin_name_not_in_cache_throws_PluginError(self):
  57. self.assertRaises(PluginError, Plugin.register, 'azerty')
  58. def test_register_if_ptype_not_known_throws_PluginError(self):
  59. with patch.object(MetaPlugType, 'all_ptype_names', return_value=[]) as mock_method:
  60. self.assertRaises(PluginError, Plugin.register, 'dummy')
  61. def test_register_returns_Plugin_child_object(self):
  62. self.assertTrue(issubclass(Plugin.register('dummy_datasource').__class__, Plugin))
  63. def test_get_if_no_plugin_found_throws_KeyError(self):
  64. self.assertRaises(PluginError, Plugin.get, 'foo')
  65. def test_get_returns_proper_plugin_instance(self):
  66. Plugin.register('dummy')
  67. self.assertTrue(Plugin.get('dummy').__class__, Plugin)
  68. def test_plugin_path_if_no_plugin_name_found_throws_PluginError(self):
  69. self.assertRaises(PluginError, Plugin.plugin_path, 'foo')
  70. def test_plugin_module_name_correctly_returns_module_name_string_from_plugin_name(self):
  71. self.assertEqual(Plugin.plugin_module_name('foo'), "%s.%s" % (VIRTUAL_PACKAGE_NAME, 'foo'))
  72. class PluginStartedTestCase(unittest.TestCase):
  73. """ Test case grouping all tests on a started Plugin class """
  74. @classmethod
  75. def setUpClass(cls):
  76. Plugin.clear()
  77. Plugin.start(['dummy', 'dummy_datasource', 'webui', 'ram_sessions'])
  78. @classmethod
  79. def tearDownClass(cls):
  80. Plugin.clear()
  81. def test_construct(self):
  82. """ Testing plugin instanciation """
  83. pname_type = {
  84. 'dummy': Extension,
  85. 'dummy_datasource': DatasourcePlugin,
  86. #'webui': InterfacePlugin, #singleton, cannot reinstanciate
  87. #'ram_session': SessionHandlerPlugin, #singleton, cannot resintanciate
  88. }
  89. for pname, ptype in pname_type.items():
  90. pinstance = Plugin.get(pname)
  91. self.assertIsInstance(pinstance, ptype, "Expected plugin '%s' \
  92. to be in an %s instance but found an %s instance" % (
  93. pname, ptype, pinstance.__class__))
  94. def test_construct_invalid(self):
  95. """ Testing plugin instanciation with a non existing name """
  96. with self.assertRaises(PluginError):
  97. Plugin.get("fljkhsfh")