From 549b482842e1c0d3cbb6baf0827fbc342b55ca31 Mon Sep 17 00:00:00 2001 From: germanhydrogen Date: Thu, 14 Dec 2023 15:40:18 +0100 Subject: [PATCH] Added unit tests to standalone and did minimal refactoring --- pyobs/modules/test/standalone.py | 8 ++++---- tests/modules/test/__init__.py | 0 tests/modules/test/standalone.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 tests/modules/test/__init__.py create mode 100644 tests/modules/test/standalone.py diff --git a/pyobs/modules/test/standalone.py b/pyobs/modules/test/standalone.py index 362fbe67..9283c6dc 100644 --- a/pyobs/modules/test/standalone.py +++ b/pyobs/modules/test/standalone.py @@ -33,11 +33,11 @@ async def _message_func(self): """Thread function for async processing.""" # loop until closing while True: - # log message - log.info(self._message) + await self._loop() - # sleep a little - await asyncio.sleep(self._interval) + async def _loop(self): + log.info(self._message) + await asyncio.sleep(self._interval) __all__ = ["StandAlone"] diff --git a/tests/modules/test/__init__.py b/tests/modules/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/modules/test/standalone.py b/tests/modules/test/standalone.py new file mode 100644 index 00000000..e4343154 --- /dev/null +++ b/tests/modules/test/standalone.py @@ -0,0 +1,30 @@ +import asyncio +import logging + +import pytest + +from pyobs.modules.test import StandAlone + + +def test_default(): + module = StandAlone() + assert module._message == "Hello world" + assert module._interval == 10 + + +@pytest.mark.asyncio +async def test_loop(mocker, caplog): + mocker.patch("asyncio.sleep", return_value=None) + module = StandAlone("Testmessage", 3) + + with caplog.at_level(logging.INFO): + await module._loop() + + assert caplog.messages[0] == "Testmessage" + asyncio.sleep.assert_called_once_with(3) + + +@pytest.mark.asyncio +async def test_background_task(): + module = StandAlone() + assert module._message_func in module._background_tasks