Skip to content

Commit

Permalink
fix: ignore none value in add_change
Browse files Browse the repository at this point in the history
  • Loading branch information
Mips2648 committed Oct 14, 2024
1 parent 3af4d9a commit 7707efc
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 27 deletions.
14 changes: 11 additions & 3 deletions jeedomdaemon/aio_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ async def __aenter__(self):
async def __aexit__(self, *_):
await self._jeedom_session.close()

@property
def changes(self):
return self.__changes

def create_send_task(self):
""" Helper function to create the send task.
Expand Down Expand Up @@ -135,12 +139,16 @@ async def add_change(self, key: str, value):
Add a key/value pair to the payload of the next cycle, several levels can be provided at once by separating keys with `::`
If a key already exists the value will be replaced by the newest
"""
if value is None:
return

if key.find('::') != -1:

changes = value
for k in reversed(key.split('::')):
tmp_changes = {}
tmp_changes[k] = changes
tmp_changes = {
k: changes
}
changes = tmp_changes
await self.__merge_dict(self.__changes,changes)
else:
Expand All @@ -151,5 +159,5 @@ async def __merge_dict(self, dic1: dict, dic2: dict):
val1 = dic1.get(key) # returns None if v1 has no value for this key
if isinstance(val1, Mapping) and isinstance(val2, Mapping):
await self.__merge_dict(val1, val2)
elif not bool(val1) or bool(val2) :
else:
dic1[key] = val2
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# pylint: disable=missing-module-docstring

import pathlib
from setuptools import setup, find_packages

__version__ = "1.0.0"

HERE = pathlib.Path(__file__).parent
INSTALL_REQUIRES = (HERE / "requirements.txt").read_text().splitlines()

setup(
# Needed to silence warnings (and to be a worthwhile package)
name='jeedomdaemon',
Expand All @@ -13,7 +17,7 @@
# Needed to actually package something
packages=find_packages(),
# Needed for dependencies
install_requires=['aiohttp'],
install_requires=INSTALL_REQUIRES,
# *strongly* suggested for sharing
version=__version__,
# The license can be anything you like
Expand Down
92 changes: 92 additions & 0 deletions tests/aio_connector_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import re
import pytest
from aioresponses import aioresponses

from jeedomdaemon.aio_connector import Publisher

class TestPublisher():

@pytest.mark.asyncio
async def test_send_to_jeedom(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
with aioresponses() as mocked:
pattern = re.compile(r'^http://local/\?apikey=.*$')
mocked.get(pattern, status=200, body='test')
mocked.post(pattern, status=200, body='test')
resp = await pub.send_to_jeedom({})
assert resp == True

@pytest.mark.asyncio
async def test_send_to_jeedom_timeout(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
with aioresponses() as mocked:
pattern = re.compile(r'^http://local/\?apikey=.*$')
mocked.post(pattern, status=200, timeout=True)
resp = await pub.send_to_jeedom({})
assert resp == False

@pytest.mark.asyncio
async def test_add_change_basic(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
await pub.add_change('val', 51)
assert pub.changes == {'val': 51}

@pytest.mark.asyncio
async def test_add_change_None(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
await pub.add_change('val', None)
assert pub.changes == {}

@pytest.mark.asyncio
async def test_add_change_compose(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
await pub.add_change('val::51', 51)
await pub.add_change('val::5', 5)
assert pub.changes == {'val': {'5': 5, '51': 51}}

@pytest.mark.asyncio
async def test_merge_changes(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
await pub.add_change('val::51', 51)
await pub.add_change('val::5', 5)
assert pub.changes == {'val': {'5': 5, '51': 51}}

await pub.add_change('val::5', 7)
assert pub.changes == {'val': {'5': 7, '51': 51}}

@pytest.mark.asyncio
async def test_merge_changes_None(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
await pub.add_change('value_5', 5)
await pub.add_change('value_10', 10)
assert pub.changes == {'value_5': 5, 'value_10': 10}
await pub.add_change('value_10', None)
assert pub.changes == {'value_5': 5, 'value_10': 10}

@pytest.mark.asyncio
async def test_merge_changes_None_level(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
await pub.add_change('val::value_5', 5)
await pub.add_change('val::value_10', 10)
assert pub.changes == {'val': {'value_5': 5, 'value_10': 10}}
await pub.add_change('val::value_10', None)
assert pub.changes == {'val': {'value_5': 5, 'value_10': 10}}

@pytest.mark.asyncio
async def test_merge_changes_empty_string(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
await pub.add_change('value_int', 5)
await pub.add_change('value_string', "test")
assert pub.changes == {'value_int': 5, 'value_string': 'test'}
await pub.add_change('value_string', '')
assert pub.changes == {'value_int': 5, 'value_string': ''}

@pytest.mark.asyncio
async def test_merge_changes_with_0(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
await pub.add_change('val::51', 51)
await pub.add_change('val::5_or_0', 5)
assert pub.changes == {'val': {'5_or_0': 5, '51': 51}}

await pub.add_change('val::5_or_0', 0)
assert pub.changes == {'val': {'5_or_0': 0, '51': 51}}
23 changes: 0 additions & 23 deletions tests/base_daemon_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"""Test class for base config."""

import logging
import re
import sys
import os
from unittest import mock
import pytest
from aioresponses import aioresponses

sys.path.append(os.path.realpath(os.path.dirname(__file__) + '/..'))

Expand Down Expand Up @@ -76,24 +74,3 @@ async def test_base_daemon_stop(self, mock_open_method):
with mock.patch.object(self._test_daemon, 'stop', return_value=None) as mock_stop:
await self._test_daemon.stop()
mock_stop.assert_called_once()

class TestPublisher():

@pytest.mark.asyncio
async def test_send_to_jeedom(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
with aioresponses() as mocked:
pattern = re.compile(r'^http://local/\?apikey=.*$')
mocked.get(pattern, status=200, body='test')
mocked.post(pattern, status=200, body='test')
resp = await pub.send_to_jeedom({})
assert resp == True

@pytest.mark.asyncio
async def test_send_to_jeedom_timeout(self):
async with Publisher('http://local/', 'cnysltyql') as pub:
with aioresponses() as mocked:
pattern = re.compile(r'^http://local/\?apikey=.*$')
mocked.post(pattern, status=200, timeout=True)
resp = await pub.send_to_jeedom({})
assert resp == False

0 comments on commit 7707efc

Please sign in to comment.