Skip to content

Commit

Permalink
new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mips2648 committed Aug 23, 2024
1 parent 4bf1274 commit 064ae54
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
6 changes: 2 additions & 4 deletions jeedomdaemon/aio_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion jeedomdaemon/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down
65 changes: 55 additions & 10 deletions tests/base_daemon_test.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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():

Expand Down

0 comments on commit 064ae54

Please sign in to comment.