diff --git a/jeedomdaemon/aio_connector.py b/jeedomdaemon/aio_connector.py index ffc1985..dbc238e 100644 --- a/jeedomdaemon/aio_connector.py +++ b/jeedomdaemon/aio_connector.py @@ -45,8 +45,6 @@ async def __handle_read(self, reader: asyncio.StreamReader, writer: asyncio.Stre self._logger.debug("Received new message on socket") data = await reader.read() message = data.decode() - # addr = writer.get_extra_info('peername') - # self._logger.debug("Received %s from %s", message, addr) writer.close() self._logger.debug("Close connection") await writer.wait_closed() @@ -66,7 +64,7 @@ def __init__(self, callback_url: str, api_key: str, cycle: float = 0.5) -> None: async def __aenter__(self): return self - async def __aexit__(self, *excinfo): + async def __aexit__(self, *_): await self._jeedom_session.close() def create_send_task(self): @@ -77,7 +75,7 @@ def create_send_task(self): return asyncio.create_task(self.__send_task()) async def test_callback(self): - """test_callback will return true if the communication with Jeedom is sucessfull or false otherwise""" + """test_callback will return true if the communication with Jeedom is successful or false otherwise""" try: async with self._jeedom_session.get(self._callback_url + '?test=1&apikey=' + self._api_key) as resp: if resp.status != 200: diff --git a/jeedomdaemon/base_config.py b/jeedomdaemon/base_config.py index 7df33ea..6df2f47 100644 --- a/jeedomdaemon/base_config.py +++ b/jeedomdaemon/base_config.py @@ -40,7 +40,7 @@ def __init__(self): self.add_argument("--cycle", help="cycle", type=float, default=0.5) def add_argument(self, *args, **kwargs): - """Add an argurment to parse. + """Add an argument to parse. e.g. from your child class: ``` diff --git a/tests/base_daemon_test.py b/tests/base_daemon_test.py index 56f8b9e..25c434c 100644 --- a/tests/base_daemon_test.py +++ b/tests/base_daemon_test.py @@ -1,9 +1,10 @@ """Test class for base config.""" +import logging import re import sys import os -from unittest.mock import patch, mock_open +from unittest import mock import pytest from aioresponses import aioresponses @@ -14,22 +15,66 @@ from jeedomdaemon.aio_connector import Publisher class TestBaseDaemon(): - def _get_test_config(self): - config = BaseConfig() - config.parse(['--loglevel', 'info', '--socketport', '42000', '--callback', 'http://localhost/path', '--apikey', 'cnysltyql', '--pid', '/tmp/test_daemon']) - return config - @patch("builtins.open", new_callable=mock_open) - def test_base_daemon_creation(self, dummy_file): + # Arrange + @pytest.fixture(autouse=True) + def daemon_config(self): + self._config = BaseConfig() + self._config.parse(['--loglevel', 'info', '--socketport', '42000', '--callback', 'http://localhost/path', '--apikey', 'cnysltyql', '--pid', '/tmp/test_daemon']) + + # Arrange + @pytest.fixture(autouse=True) + def test_daemon(self): + self._test_daemon = BaseDaemon(self._config) + + async def _on_start_cb(self): + raise Exception("Test") + + @mock.patch("builtins.open", new_callable=mock.mock_open) + def test_base_daemon_creation(self, mock_open_method): """ Tests if it can create a basic daemon """ - config = self._get_test_config() - testdaemon = BaseDaemon(config) with pytest.raises(SystemExit) as pytest_wrapped_e: - testdaemon.run() + with mock.patch('jeedomdaemon.aio_connector.Publisher.test_callback') as mock_test_callback: + self._test_daemon.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 0 + mock_test_callback.assert_called_once() + + @mock.patch("builtins.open", new_callable=mock.mock_open) + def test_base_daemon_initialization(self, mock_open_method): + """ + Tests if the daemon initializes correctly + """ + assert self._test_daemon._config == self._config + + @mock.patch("builtins.open", new_callable=mock.mock_open) + def test_base_daemon_on_start_exception(self, mock_open_method): + """ + Tests on start callback exception + """ + testdaemon = BaseDaemon(self._config, on_start_cb=self._on_start_cb) + logger = logging.getLogger('jeedomdaemon.base_daemon') + with pytest.raises(SystemExit) as pytest_wrapped_e: + with mock.patch('jeedomdaemon.aio_connector.Publisher.test_callback') as mock_test_callback: + with mock.patch.object(logger, 'warning') as mock_warning: + testdaemon.run() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 0 + mock_test_callback.assert_called_once() + mock_warning.assert_called_once() + assert len(mock_warning.call_args) == 2 + assert str(mock_warning.call_args[0][1].args[0]) == 'Test' + + @mock.patch("builtins.open", new_callable=mock.mock_open) + def test_base_daemon_stop(self, mock_open_method): + """ + Tests if the daemon stops correctly + """ + with mock.patch.object(self._test_daemon, 'stop', return_value=None) as mock_stop: + self._test_daemon.stop() + mock_stop.assert_called_once() class TestPublisher():