kopia lustrzana https://github.com/Yakifo/amqtt
improve has_attr plugin tests
rodzic
0c993565b5
commit
5181289d2b
|
@ -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)
|
|
@ -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__)
|
Ładowanie…
Reference in New Issue