From f3139a8aa0fd51c8282d60b3ba132aa439a3643c Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Wed, 12 Jan 2022 15:50:16 -0800 Subject: [PATCH] add more unit tests --- meshtastic/__init__.py | 33 ++++++++++++------- meshtastic/tests/test_init.py | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 meshtastic/tests/test_init.py diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 98bc9dc..19f669e 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -132,6 +132,7 @@ def _onTextReceive(iface, asDict): # # Usually btw this problem is caused by apps sending binary data but setting the payload type to # text. + logging.debug(f'in _onTextReceive() asDict:{asDict}') try: asBytes = asDict["decoded"]["payload"] asDict["decoded"]["text"] = asBytes.decode("utf-8") @@ -142,22 +143,30 @@ def _onTextReceive(iface, asDict): def _onPositionReceive(iface, asDict): """Special auto parsing for received messages""" - p = asDict["decoded"]["position"] - iface._fixupPosition(p) - # update node DB as needed - iface._getOrCreateByNum(asDict["from"])["position"] = p + logging.debug(f'in _onPositionReceive() asDict:{asDict}') + if 'decoded' in asDict: + if 'position' in asDict['decoded'] and 'from' in asDict: + p = asDict["decoded"]["position"] + logging.debug(f'p:{p}') + p = iface._fixupPosition(p) + logging.debug(f'after fixup p:{p}') + # update node DB as needed + iface._getOrCreateByNum(asDict["from"])["position"] = p def _onNodeInfoReceive(iface, asDict): """Special auto parsing for received messages""" - p = asDict["decoded"]["user"] - # decode user protobufs and update nodedb, provide decoded version as "position" in the published msg - # update node DB as needed - n = iface._getOrCreateByNum(asDict["from"]) - n["user"] = p - # We now have a node ID, make sure it is uptodate in that table - iface.nodes[p["id"]] = n - _receiveInfoUpdate(iface, asDict) + logging.debug(f'in _onNodeInfoReceive() asDict:{asDict}') + if 'decoded' in asDict: + if 'user' in asDict['decoded'] and 'from' in asDict: + p = asDict["decoded"]["user"] + # decode user protobufs and update nodedb, provide decoded version as "position" in the published msg + # update node DB as needed + n = iface._getOrCreateByNum(asDict["from"]) + n["user"] = p + # We now have a node ID, make sure it is uptodate in that table + iface.nodes[p["id"]] = n + _receiveInfoUpdate(iface, asDict) def _receiveInfoUpdate(iface, asDict): diff --git a/meshtastic/tests/test_init.py b/meshtastic/tests/test_init.py new file mode 100644 index 0000000..75c9609 --- /dev/null +++ b/meshtastic/tests/test_init.py @@ -0,0 +1,61 @@ +"""Meshtastic unit tests for __init__.py""" + +import re +import logging + +from unittest.mock import MagicMock +import pytest + +from meshtastic.__init__ import _onTextReceive, _onPositionReceive, _onNodeInfoReceive +from ..serial_interface import SerialInterface +from ..globals import Globals + + +@pytest.mark.unit +def test_init_onTextReceive_with_exception(caplog): + """Test _onTextReceive""" + args = MagicMock() + Globals.getInstance().set_args(args) + iface = MagicMock(autospec=SerialInterface) + packet = {} + with caplog.at_level(logging.DEBUG): + _onTextReceive(iface, packet) + assert re.search(r'in _onTextReceive', caplog.text, re.MULTILINE) + assert re.search(r'Malformatted', caplog.text, re.MULTILINE) + + +@pytest.mark.unit +def test_init_onPositionReceive(caplog): + """Test _onPositionReceive""" + args = MagicMock() + Globals.getInstance().set_args(args) + iface = MagicMock(autospec=SerialInterface) + packet = { + 'from': 'foo', + 'decoded': { + 'position': {} + } + } + with caplog.at_level(logging.DEBUG): + _onPositionReceive(iface, packet) + assert re.search(r'in _onPositionReceive', caplog.text, re.MULTILINE) + + +@pytest.mark.unit +def test_init_onNodeInfoReceive(caplog, iface_with_nodes): + """Test _onNodeInfoReceive""" + args = MagicMock() + Globals.getInstance().set_args(args) + iface = iface_with_nodes + iface.myInfo.my_node_num = 2475227164 + packet = { + 'from': 'foo', + 'decoded': { + 'user': { + 'id': 'bar', + }, + } + } + with caplog.at_level(logging.DEBUG): + _onNodeInfoReceive(iface, packet) + assert re.search(r'in _onNodeInfoReceive', caplog.text, re.MULTILINE)