Skip to content

Commit

Permalink
Safer test_mapper_plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Oct 31, 2019
1 parent ab72ca2 commit 8e56b50
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
11 changes: 7 additions & 4 deletions ckan/tests/legacy/ckantestplugins.py
Expand Up @@ -12,17 +12,20 @@ class MapperPlugin(p.SingletonPlugin):
def __init__(self, *args, **kw):
self.calls = []

def _get_instance_name(self, instance):
return getattr(instance, "name", None)

def before_insert(self, mapper, conn, instance):
self.calls.append(("before_insert", instance.name))
self.calls.append(("before_insert", self._get_instance_name(instance)))

def after_insert(self, mapper, conn, instance):
self.calls.append(("after_insert", instance.name))
self.calls.append(("after_insert", self._get_instance_name(instance)))

def before_delete(self, mapper, conn, instance):
self.calls.append(("before_delete", instance.name))
self.calls.append(("before_delete", self._get_instance_name(instance)))

def after_delete(self, mapper, conn, instance):
self.calls.append(("after_delete", instance.name))
self.calls.append(("after_delete", self._get_instance_name(instance)))


class MapperPlugin2(MapperPlugin):
Expand Down
48 changes: 24 additions & 24 deletions ckan/tests/legacy/test_plugins.py
Expand Up @@ -157,7 +157,31 @@ def test_mapper_plugin_fired_on_delete():
]


def test_action_plugin_override():
status_show_original = logic.get_action("status_show")(None, {})
with plugins.use_plugin("action_plugin"):
assert (
logic.get_action("status_show")(None, {}) != status_show_original
)
assert logic.get_action("status_show")(None, {}) == status_show_original


def test_auth_plugin_override():
package_list_original = authz.is_authorized("package_list", {})
with plugins.use_plugin("auth_plugin"):
assert authz.is_authorized("package_list", {}) != package_list_original
assert authz.is_authorized("package_list", {}) == package_list_original


def test_inexistent_plugin_loading():
with pytest.raises(plugins.PluginNotFoundException):
plugins.load("inexistent-plugin")


class TestPlugins:
def teardown_method(self):
plugins.unload_all()

def test_plugin_loading_order(self):
"""
Check that plugins are loaded in the order specified in the config
Expand Down Expand Up @@ -202,27 +226,3 @@ def test_plugin_loading_order(self):
# cleanup
config["ckan.plugins"] = config_plugins
plugins.load_all()

def test_action_plugin_override(self):
status_show_original = logic.get_action("status_show")(None, {})
with plugins.use_plugin("action_plugin"):
assert (
logic.get_action("status_show")(None, {})
!= status_show_original
)
assert (
logic.get_action("status_show")(None, {}) == status_show_original
)

def test_auth_plugin_override(self):
package_list_original = authz.is_authorized("package_list", {})
with plugins.use_plugin("auth_plugin"):
assert (
authz.is_authorized("package_list", {})
!= package_list_original
)
assert authz.is_authorized("package_list", {}) == package_list_original

def test_inexistent_plugin_loading(self):
with pytest.raises(plugins.PluginNotFoundException):
plugins.load("inexistent-plugin")

0 comments on commit 8e56b50

Please sign in to comment.