Files
python/meshtastic/tests/test_init.py
Steve Holden 067cddd354 Refactor to avoid the use of a special global object.
The global object formerly used is now replaced by direct use
of the namespace opf the globals module. This eliminates the
redundant getters and setters and simplifies the code for
future maintainers.

Note that the globals module name conflicts (harmlessly at
present) with a Python built-in function. A future commit
should rename it `config` to remove this clash and better
represent its intended purpose.
2024-04-10 10:03:12 +01:00

57 lines
1.6 KiB
Python

"""Meshtastic unit tests for __init__.py"""
import logging
import re
from unittest.mock import MagicMock
import pytest
from meshtastic import _onNodeInfoReceive, _onPositionReceive, _onTextReceive, globals
from ..serial_interface import SerialInterface
@pytest.mark.unit
def test_init_onTextReceive_with_exception(caplog):
"""Test _onTextReceive"""
args = MagicMock()
globals.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.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.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)