From 10e97ee88cd0651d396e2346892d90f3985065e8 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Fri, 10 Dec 2021 09:24:57 -0800 Subject: [PATCH] added --test2 unit test; wip on --info --- meshtastic/__main__.py | 1 + meshtastic/tests/test_main.py | 53 +++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index a8738c8..fdf0de5 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -13,6 +13,7 @@ import pyqrcode import pkg_resources import meshtastic.util import meshtastic.test +from meshtastic.serial_interface import SerialInterface from .serial_interface import SerialInterface from .tcp_interface import TCPInterface from .ble_interface import BLEInterface diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 58e48b3..a7b55f3 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -4,10 +4,11 @@ import sys import argparse import re -from unittest.mock import patch +from unittest.mock import patch, MagicMock import pytest from meshtastic.__main__ import initParser, main, Globals +#from meshtastic.serial_interface import SerialInterface @pytest.mark.unit @@ -165,7 +166,7 @@ def test_main_test_one_port(patched_find_ports): @patch('meshtastic.test.testAll', return_value=True) @patch('meshtastic.util.findPorts', return_value=['/dev/ttyFake1', '/dev/ttyFake2']) def test_main_test_two_ports_success(patched_find_ports, patched_test_all): - """Test --test two fake ports""" + """Test --test two fake ports and testAll() is a simulated success""" sys.argv = ['', '--test'] args = sys.argv parser = None @@ -179,3 +180,51 @@ def test_main_test_two_ports_success(patched_find_ports, patched_test_all): assert pytest_wrapped_e.value.code == 0 # TODO: why does this fail? patched_find_ports.assert_called() patched_test_all.assert_called() + + +@pytest.mark.unit +@patch('meshtastic.test.testAll', return_value=False) +@patch('meshtastic.util.findPorts', return_value=['/dev/ttyFake1', '/dev/ttyFake2']) +def test_main_test_two_ports_fails(patched_find_ports, patched_test_all): + """Test --test two fake ports and testAll() is a simulated failure""" + sys.argv = ['', '--test'] + args = sys.argv + parser = None + parser = argparse.ArgumentParser() + our_globals = Globals.getInstance() + our_globals.set_parser(parser) + our_globals.set_args(args) + with pytest.raises(SystemExit) as pytest_wrapped_e: + main() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + # TODO: why does this fail? patched_find_ports.assert_called() + patched_test_all.assert_called() +# +# +#@pytest.mark.unit +#@patch('meshtastic.stream_interface.StreamInterface.__init__') +#@patch('serial.Serial') +#@patch('meshtastic.serial_interface.SerialInterface') +#@patch('meshtastic.util.findPorts', return_value=['/dev/ttyFake1']) +#def test_main_info_one_port(patched_find_ports, patched_serial_interface, +# patched_serial_serial, patched_stream_interface_constructor): +# """Test --info one fake port""" +# iface = MagicMock() +# patched_serial_interface.return_value = iface +# astream = MagicMock() +# patched_serial_serial = astream +# siface = MagicMock() +# patched_stream_interface_constructor = siface +# sys.argv = ['', '--info'] +# args = sys.argv +# parser = None +# parser = argparse.ArgumentParser() +# our_globals = Globals.getInstance() +# our_globals.set_parser(parser) +# our_globals.set_args(args) +# main() +# patched_find_ports.assert_called() +# patched_serial_interface.assert_called() +# patched_serial_serial.assert_called() +# patched_stream_interface_constructor