add main unit test for --ble and --host; add test for --sendtext with --dest

This commit is contained in:
Mike Kinney
2021-12-15 21:59:41 -08:00
parent 6239585bf2
commit 34789899d8
2 changed files with 63 additions and 4 deletions

View File

@@ -13,8 +13,6 @@ import pyqrcode
import pkg_resources
import meshtastic.util
import meshtastic.test
from .tcp_interface import TCPInterface
from .ble_interface import BLEInterface
from . import remote_hardware
from . import portnums_pb2, channel_pb2, radioconfig_pb2
from .globals import Globals
@@ -575,9 +573,9 @@ def common():
subscribe()
if args.ble:
client = BLEInterface(args.ble, debugOut=logfile, noProto=args.noproto)
client = meshtastic.ble_interface.BLEInterface(args.ble, debugOut=logfile, noProto=args.noproto)
elif args.host:
client = TCPInterface(
client = meshtastic.tcp_interface.TCPInterface(
args.host, debugOut=logfile, noProto=args.noproto)
else:
client = meshtastic.serial_interface.SerialInterface(

View File

@@ -10,6 +10,8 @@ import pytest
from meshtastic.__main__ import initParser, main, Globals, onReceive, onConnection
import meshtastic.radioconfig_pb2
from ..serial_interface import SerialInterface
from ..tcp_interface import TCPInterface
from ..ble_interface import BLEInterface
from ..node import Node
from ..channel_pb2 import Channel
@@ -184,6 +186,44 @@ def test_main_info(capsys, reset_globals):
mo.assert_called()
@pytest.mark.unit
def test_main_info_with_tcp_interface(capsys, reset_globals):
"""Test --info"""
sys.argv = ['', '--info', '--host', 'meshtastic.local']
Globals.getInstance().set_args(sys.argv)
iface = MagicMock(autospec=TCPInterface)
def mock_showInfo():
print('inside mocked showInfo')
iface.showInfo.side_effect = mock_showInfo
with patch('meshtastic.tcp_interface.TCPInterface', return_value=iface) as mo:
main()
out, err = capsys.readouterr()
assert re.search(r'Connected to radio', out, re.MULTILINE)
assert re.search(r'inside mocked showInfo', out, re.MULTILINE)
assert err == ''
mo.assert_called()
@pytest.mark.unit
def test_main_info_with_ble_interface(capsys, reset_globals):
"""Test --info"""
sys.argv = ['', '--info', '--ble', 'foo']
Globals.getInstance().set_args(sys.argv)
iface = MagicMock(autospec=BLEInterface)
def mock_showInfo():
print('inside mocked showInfo')
iface.showInfo.side_effect = mock_showInfo
with patch('meshtastic.ble_interface.BLEInterface', return_value=iface) as mo:
main()
out, err = capsys.readouterr()
assert re.search(r'Connected to radio', out, re.MULTILINE)
assert re.search(r'inside mocked showInfo', out, re.MULTILINE)
assert err == ''
mo.assert_called()
@pytest.mark.unit
def test_main_no_proto(capsys, reset_globals):
"""Test --noproto (using --info for output)"""
@@ -377,6 +417,27 @@ def test_main_sendtext(capsys, reset_globals):
mo.assert_called()
@pytest.mark.unit
def test_main_sendtext_with_dest(capsys, reset_globals):
"""Test --sendtext with --dest"""
sys.argv = ['', '--sendtext', 'hello', '--dest', 'foo']
Globals.getInstance().set_args(sys.argv)
iface = MagicMock(autospec=SerialInterface)
def mock_sendText(text, dest, wantAck):
print('inside mocked sendText')
iface.sendText.side_effect = mock_sendText
with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo:
main()
out, err = capsys.readouterr()
assert re.search(r'Connected to radio', out, re.MULTILINE)
assert re.search(r'Sending text message', out, re.MULTILINE)
assert re.search(r'inside mocked sendText', out, re.MULTILINE)
assert err == ''
mo.assert_called()
@pytest.mark.unit
def test_main_sendping(capsys, reset_globals):
"""Test --sendping"""