amqtt/tests/plugins/test_manager.py

102 wiersze
3.6 KiB
Python
Czysty Zwykły widok Historia

2015-08-16 19:37:18 +00:00
# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.
import unittest
import logging
import asyncio
2021-03-06 15:20:48 +00:00
import pytest
from hbmqtt.plugins.manager import PluginManager
2015-08-16 19:37:18 +00:00
2015-08-20 19:43:18 +00:00
formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
2015-08-17 19:52:26 +00:00
logging.basicConfig(level=logging.INFO, format=formatter)
2015-08-16 19:37:18 +00:00
class TestPlugin:
2015-08-20 19:43:18 +00:00
def __init__(self, context):
self.context = context
2015-08-16 19:37:18 +00:00
class EventTestPlugin:
2015-08-20 19:43:18 +00:00
def __init__(self, context):
self.context = context
2015-08-16 19:37:18 +00:00
self.test_flag = False
self.coro_flag = False
2020-12-31 00:16:45 +00:00
async def on_test(self, *args, **kwargs):
2015-08-16 19:37:18 +00:00
self.test_flag = True
2015-08-20 19:43:18 +00:00
self.context.logger.info("on_test")
2015-08-16 19:37:18 +00:00
2020-12-31 00:16:45 +00:00
async def test_coro(self, *args, **kwargs):
2015-08-16 19:37:18 +00:00
self.coro_flag = True
2020-12-31 00:16:45 +00:00
async def ret_coro(self, *args, **kwargs):
return "TEST"
2015-08-16 19:37:18 +00:00
class TestPluginManager(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
2021-03-06 15:20:48 +00:00
@pytest.mark.xfail(reason="see https://github.com/Yakifo/aio-hbmqtt/issues/15", strict=False)
2015-08-16 19:37:18 +00:00
def test_load_plugin(self):
manager = PluginManager("hbmqtt.test.plugins", context=None)
2021-03-06 17:37:23 +00:00
assert len(manager._plugins) > 0
2015-08-16 19:37:18 +00:00
2021-03-06 15:20:48 +00:00
@pytest.mark.xfail(reason="see https://github.com/Yakifo/aio-hbmqtt/issues/15", strict=False)
2015-08-16 19:37:18 +00:00
def test_fire_event(self):
2020-12-31 00:16:45 +00:00
async def fire_event():
await manager.fire_event("test")
await asyncio.sleep(1, loop=self.loop)
await manager.close()
2015-08-19 11:54:09 +00:00
manager = PluginManager("hbmqtt.test.plugins", context=None, loop=self.loop)
self.loop.run_until_complete(fire_event())
plugin = manager.get_plugin("event_plugin")
2021-03-06 17:37:23 +00:00
assert plugin.object.test_flag
2015-08-19 11:54:09 +00:00
2021-03-06 15:20:48 +00:00
@pytest.mark.xfail(reason="see https://github.com/Yakifo/aio-hbmqtt/issues/15", strict=False)
2015-08-19 11:54:09 +00:00
def test_fire_event_wait(self):
2020-12-31 00:16:45 +00:00
async def fire_event():
await manager.fire_event("test", wait=True)
await manager.close()
2015-08-16 19:37:18 +00:00
manager = PluginManager("hbmqtt.test.plugins", context=None, loop=self.loop)
self.loop.run_until_complete(fire_event())
plugin = manager.get_plugin("event_plugin")
2021-03-06 17:37:23 +00:00
assert plugin.object.test_flag
2015-08-16 19:37:18 +00:00
2021-03-06 15:20:48 +00:00
@pytest.mark.xfail(reason="see https://github.com/Yakifo/aio-hbmqtt/issues/15", strict=False)
2015-08-16 19:37:18 +00:00
def test_map_coro(self):
2020-12-31 00:16:45 +00:00
async def call_coro():
await manager.map_plugin_coro('test_coro')
2015-08-16 19:37:18 +00:00
manager = PluginManager("hbmqtt.test.plugins", context=None, loop=self.loop)
self.loop.run_until_complete(call_coro())
plugin = manager.get_plugin("event_plugin")
2021-03-06 17:37:23 +00:00
assert plugin.object.test_coro
2021-03-06 15:20:48 +00:00
@pytest.mark.xfail(reason="see https://github.com/Yakifo/aio-hbmqtt/issues/15", strict=False)
def test_map_coro_return(self):
2020-12-31 00:16:45 +00:00
async def call_coro():
return (await manager.map_plugin_coro('ret_coro'))
manager = PluginManager("hbmqtt.test.plugins", context=None, loop=self.loop)
ret = self.loop.run_until_complete(call_coro())
plugin = manager.get_plugin("event_plugin")
self.assertEqual(ret[plugin], "TEST")
2021-03-06 15:20:48 +00:00
@pytest.mark.xfail(reason="see https://github.com/Yakifo/aio-hbmqtt/issues/15", strict=False)
def test_map_coro_filter(self):
"""
Run plugin coro but expect no return as an empty filter is given
:return:
"""
2020-12-31 00:16:45 +00:00
async def call_coro():
return (await manager.map_plugin_coro('ret_coro', filter_plugins=[]))
manager = PluginManager("hbmqtt.test.plugins", context=None, loop=self.loop)
ret = self.loop.run_until_complete(call_coro())
2021-03-06 17:37:23 +00:00
assert len(ret) == 0