mirror of
https://github.com/meshtastic/python.git
synced 2025-12-31 03:47:55 -05:00
add more unit tests for onReceive in main
This commit is contained in:
@@ -27,7 +27,7 @@ def onReceive(packet, interface):
|
||||
args = our_globals.get_args()
|
||||
try:
|
||||
d = packet.get('decoded')
|
||||
logging.debug(f'd:{d}')
|
||||
logging.debug(f'in onReceive() d:{d}')
|
||||
|
||||
# Exit once we receive a reply
|
||||
if args and args.sendtext and packet["to"] == interface.myInfo.my_node_num and d["portnum"] == portnums_pb2.PortNum.TEXT_MESSAGE_APP:
|
||||
@@ -37,12 +37,10 @@ def onReceive(packet, interface):
|
||||
if args and args.reply:
|
||||
msg = d.get('text')
|
||||
if msg:
|
||||
#shortName = packet['decoded']['shortName']
|
||||
rxSnr = packet['rxSnr']
|
||||
hopLimit = packet['hopLimit']
|
||||
print(f"message: {msg}")
|
||||
reply = "got msg \'{}\' with rxSnr: {} and hopLimit: {}".format(
|
||||
msg, rxSnr, hopLimit)
|
||||
reply = "got msg \'{}\' with rxSnr: {} and hopLimit: {}".format(msg, rxSnr, hopLimit)
|
||||
print("Sending reply: ", reply)
|
||||
interface.sendText(reply)
|
||||
|
||||
|
||||
@@ -1234,14 +1234,113 @@ def test_main_setchan(capsys, reset_globals):
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_main_onReceive_empty(reset_globals):
|
||||
def test_main_onReceive_empty(caplog, reset_globals):
|
||||
"""Test onReceive"""
|
||||
sys.argv = ['']
|
||||
Globals.getInstance().set_args(sys.argv)
|
||||
iface = MagicMock(autospec=SerialInterface)
|
||||
packet = {'decoded': 'foo'}
|
||||
onReceive(packet, iface)
|
||||
# TODO: how do we know we actually called it?
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
onReceive(packet, iface)
|
||||
assert re.search(r'in onReceive', caplog.text, re.MULTILINE)
|
||||
|
||||
|
||||
# TODO: use this captured position app message (might want/need in the future)
|
||||
# packet = {
|
||||
# 'to': 4294967295,
|
||||
# 'decoded': {
|
||||
# 'portnum': 'POSITION_APP',
|
||||
# 'payload': "M69\306a"
|
||||
# },
|
||||
# 'id': 334776976,
|
||||
# 'hop_limit': 3
|
||||
# }
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_main_onReceive_with_sendtext(caplog, reset_globals):
|
||||
"""Test onReceive with sendtext
|
||||
The entire point of this test is to make sure the interface.close() call
|
||||
is made in onReceive().
|
||||
"""
|
||||
sys.argv = ['', '--sendtext', 'hello']
|
||||
Globals.getInstance().set_args(sys.argv)
|
||||
|
||||
# Note: 'TEXT_MESSAGE_APP' value is 1
|
||||
packet = {
|
||||
'to': 4294967295,
|
||||
'decoded': {
|
||||
'portnum': 1,
|
||||
'payload': "hello"
|
||||
},
|
||||
'id': 334776977,
|
||||
'hop_limit': 3,
|
||||
'want_ack': True
|
||||
}
|
||||
|
||||
iface = MagicMock(autospec=SerialInterface)
|
||||
iface.myInfo.my_node_num = 4294967295
|
||||
|
||||
with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo:
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
main()
|
||||
onReceive(packet, iface)
|
||||
assert re.search(r'in onReceive', caplog.text, re.MULTILINE)
|
||||
mo.assert_called()
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_main_onReceive_with_reply(caplog, capsys, reset_globals):
|
||||
"""Test onReceive with a reply
|
||||
To capture: on one device run '--sendtext aaa --reply' and on another
|
||||
device run '--sendtext bbb --reply', then back to the first device and
|
||||
run '--sendtext aaa2 --reply'. You should now see a "Sending reply" message.
|
||||
"""
|
||||
sys.argv = ['', '--sendtext', 'hello', '--reply']
|
||||
Globals.getInstance().set_args(sys.argv)
|
||||
|
||||
# Note: 'TEXT_MESSAGE_APP' value is 1
|
||||
|
||||
send_packet = {
|
||||
'to': 4294967295,
|
||||
'decoded': {
|
||||
'portnum': 1,
|
||||
'payload': "hello"
|
||||
},
|
||||
'id': 334776977,
|
||||
'hop_limit': 3,
|
||||
'want_ack': True
|
||||
}
|
||||
|
||||
reply_packet = {
|
||||
'from': 682968668,
|
||||
'to': 4294967295,
|
||||
'decoded': {
|
||||
'portnum': 'TEXT_MESSAGE_APP',
|
||||
'payload': b'bbb',
|
||||
'text': 'bbb'
|
||||
},
|
||||
'id': 1709936182,
|
||||
'rxTime': 1640381999,
|
||||
'rxSnr': 6.0,
|
||||
'hopLimit': 3,
|
||||
'raw': 'faked',
|
||||
'fromId': '!28b5465c',
|
||||
'toId': '^all'
|
||||
}
|
||||
|
||||
iface = MagicMock(autospec=SerialInterface)
|
||||
iface.myInfo.my_node_num = 4294967295
|
||||
|
||||
with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo:
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
main()
|
||||
onReceive(send_packet, iface)
|
||||
onReceive(reply_packet, iface)
|
||||
assert re.search(r'in onReceive', caplog.text, re.MULTILINE)
|
||||
out, err = capsys.readouterr()
|
||||
assert re.search(r'got msg ', out, re.MULTILINE)
|
||||
assert err == ''
|
||||
mo.assert_called()
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@@ -1371,6 +1470,7 @@ def test_main_gpio_rd(caplog, capsys, reset_globals):
|
||||
channel.settings.psk = b'\x01'
|
||||
|
||||
packet = {
|
||||
|
||||
'from': 682968668,
|
||||
'to': 682968612,
|
||||
'channel': 1,
|
||||
|
||||
Reference in New Issue
Block a user