From 5181289d2bbf28abc20f6bb7540781e62d883564 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Sat, 5 Feb 2022 20:44:30 +0100 Subject: [PATCH] improve has_attr plugin tests --- tests/plugins/test_logging.py | 11 -------- tests/plugins/test_plugins.py | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) delete mode 100644 tests/plugins/test_logging.py create mode 100644 tests/plugins/test_plugins.py diff --git a/tests/plugins/test_logging.py b/tests/plugins/test_logging.py deleted file mode 100644 index 5fef6a8..0000000 --- a/tests/plugins/test_logging.py +++ /dev/null @@ -1,11 +0,0 @@ -import pytest - -import amqtt.plugins.logging - - -def test_EventLoggerPlugin_getattr(): - logger = amqtt.plugins.logging.EventLoggerPlugin(None) - with pytest.raises(AttributeError) as exc: - logger.foo - - assert "foo" in str(exc.value) diff --git a/tests/plugins/test_plugins.py b/tests/plugins/test_plugins.py new file mode 100644 index 0000000..bfacac3 --- /dev/null +++ b/tests/plugins/test_plugins.py @@ -0,0 +1,52 @@ +import inspect +from logging import getLogger +from os.path import join, dirname, isfile + +import pytest + +import amqtt.plugins +from glob import glob + +from amqtt.plugins.manager import BaseContext + +_INVALID_METHOD = "invalid_foo" +_PLUGIN = "Plugin" + + +class _TestContext(BaseContext): + def __init__(self): + super().__init__() + self.config = {"auth": {}} + self.logger = getLogger(__file__) + + +def _verify_module(module, plugin_module_name): + if not module.__name__.startswith(plugin_module_name): + return + + for name, clazz in inspect.getmembers(module, inspect.isclass): + if not name.endswith(_PLUGIN) or name == _PLUGIN: + continue + + obj = clazz(_TestContext()) + with pytest.raises(AttributeError, match=f"'{name}' object has no attribute '{_INVALID_METHOD}'"): + getattr(obj, _INVALID_METHOD) + assert hasattr(obj, _INVALID_METHOD) is False + + + for name, obj in inspect.getmembers(module, inspect.ismodule): + _verify_module(obj, plugin_module_name) + + +def test_plugins_correct_has_attr(): + module = amqtt.plugins + for file in glob(join(dirname(module.__file__), "**/*.py"), recursive=True): + if not isfile(file): + continue + + name = file.replace("/", ".") + name = name[name.find(module.__name__) : -3].removesuffix(".__init__") + + __import__(name) + + _verify_module(module, module.__name__)