From 34d31711ad44372999697e6527310f188aac7d87 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Sun, 12 Dec 2021 09:27:51 -0800 Subject: [PATCH] add --qr and --nodes unit test --- meshtastic/__main__.py | 3 +- meshtastic/tests/test_main.py | 58 +++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 9561ca1..a7c31d0 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -485,8 +485,7 @@ def onConnected(interface): # Handle the int/float/bool arguments for pref in args.get: - getPref( - prefs, pref[0]) + getPref(prefs, pref[0]) print("Completed getting preferences") diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 14cc6db..7002ee4 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -9,7 +9,6 @@ import pytest from meshtastic.__main__ import initParser, main, Globals -#from meshtastic.serial_interface import SerialInterface from ..serial_interface import SerialInterface @@ -217,10 +216,65 @@ def test_main_info(capsys): our_globals.set_parser(parser) our_globals.set_args(args) iface = MagicMock(autospec=SerialInterface) - with patch('meshtastic.serial_interface.SerialInterface', return_value=iface): + def mock_showInfo(): + print('inside mocked showInfo') + iface.showInfo.side_effect = mock_showInfo + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: main() out, err = capsys.readouterr() print('out:', out) print('err:', err) 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_qr(capsys): + """Test --qr""" + sys.argv = ['', '--qr'] + args = sys.argv + parser = None + parser = argparse.ArgumentParser() + our_globals = Globals.getInstance() + our_globals.set_parser(parser) + our_globals.set_args(args) + iface = MagicMock(autospec=SerialInterface) + # TODO: could mock/check url + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: + main() + out, err = capsys.readouterr() + print('out:', out) + print('err:', err) + assert re.search(r'Connected to radio', out, re.MULTILINE) + assert re.search(r'Primary channel URL', out, re.MULTILINE) + # if a qr code is generated it will have lots of these + assert re.search(r'\[7m', out, re.MULTILINE) + assert err == '' + mo.assert_called() + + +@pytest.mark.unit +def test_main_nodes(capsys): + """Test --nodes""" + sys.argv = ['', '--nodes'] + args = sys.argv + parser = None + parser = argparse.ArgumentParser() + our_globals = Globals.getInstance() + our_globals.set_parser(parser) + our_globals.set_args(args) + iface = MagicMock(autospec=SerialInterface) + def mock_showNodes(): + print('inside mocked showNodes') + iface.showNodes.side_effect = mock_showNodes + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: + main() + out, err = capsys.readouterr() + print('out:', out) + print('err:', err) + assert re.search(r'Connected to radio', out, re.MULTILINE) + assert re.search(r'inside mocked showNodes', out, re.MULTILINE) + assert err == '' + mo.assert_called()