sort possible options/tests, figured out how to mock so --info can be tested

This commit is contained in:
Mike Kinney
2021-12-12 00:22:17 -08:00
parent c21d64be64
commit 0943ecab5b
3 changed files with 65 additions and 39 deletions

View File

@@ -13,7 +13,6 @@ 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
@@ -68,10 +67,13 @@ def getPref(attributes, name):
objDesc = attributes.DESCRIPTOR
field = objDesc.fields_by_name.get(name)
if not field:
print(f"{attributes.__class__.__name__} doesn't have an attribute called {name}, so you can not get it.")
print(f"Choices are:")
print(f"{attributes.__class__.__name__} does not have an attribute called {name}, so you can not get it.")
print(f"Choices in sorted order are:")
names = []
for f in objDesc.fields:
print(f" {f.name}")
names.append(f'{f.name}')
for temp_name in sorted(names):
print(f" {temp_name}")
return
# okay - try to read the value
@@ -94,10 +96,13 @@ def setPref(attributes, name, valStr):
objDesc = attributes.DESCRIPTOR
field = objDesc.fields_by_name.get(name)
if not field:
print(f"{attributes.__class__.__name__} doesn't have an attribute called {name}, so you can not set it.")
print(f"Choices are:")
print(f"{attributes.__class__.__name__} does not have an attribute called {name}, so you can not set it.")
print(f"Choices in sorted order are:")
names = []
for f in objDesc.fields:
print(f" {f.name}")
names.append(f'{f.name}')
for temp_name in sorted(names):
print(f" {temp_name}")
return
val = meshtastic.util.fromStr(valStr)
@@ -110,10 +115,13 @@ def setPref(attributes, name, valStr):
if e:
val = e.number
else:
print(f"{name} doesn't have an enum called {val}, so you can not set it.")
print(f"Choices are:")
print(f"{name} does not have an enum called {val}, so you can not set it.")
print(f"Choices in sorted order are:")
names = []
for f in enumType.values:
print(f" {f.name}")
names.append(f'{f.name}')
for temp_name in sorted(names):
print(f" {temp_name}")
return
# okay - try to read the value

View File

@@ -4,11 +4,13 @@ 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
from ..serial_interface import SerialInterface
@pytest.mark.unit
@@ -202,31 +204,23 @@ def test_main_test_two_ports_fails(patched_find_ports, patched_test_all):
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
@pytest.mark.unit
def test_main_info(capsys):
"""Test --info"""
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)
iface = MagicMock(autospec=SerialInterface)
with patch('meshtastic.serial_interface.SerialInterface', return_value=iface):
main()
out, err = capsys.readouterr()
print('out:', out)
print('err:', err)
assert re.search(r'Connected to radio', out, re.MULTILINE)
assert err == ''

View File

@@ -48,6 +48,30 @@ def test_smoke1_sendping():
assert return_value == 0
@pytest.mark.smoke1
def test_get_with_invalid_setting():
"""Test '--get a_bad_setting'."""
return_value, out = subprocess.getstatusoutput('meshtastic --get a_bad_setting')
assert re.search(r'Choices in sorted order', out)
assert return_value == 0
@pytest.mark.smoke1
def test_set_with_invalid_setting():
"""Test '--set a_bad_setting'."""
return_value, out = subprocess.getstatusoutput('meshtastic --set a_bad_setting foo')
assert re.search(r'Choices in sorted order', out)
assert return_value == 0
@pytest.mark.smoke1
def test_ch_set_with_invalid_settingpatch_find_ports():
"""Test '--ch-set with a_bad_setting'."""
return_value, out = subprocess.getstatusoutput('meshtastic --ch-set invalid_setting foo --ch-index 0')
assert re.search(r'Choices in sorted order', out)
assert return_value == 0
@pytest.mark.smoke1
def test_smoke1_pos_fields():
"""Test --pos-fields (with some values POS_ALTITUDE POS_ALT_MSL POS_BATTERY)"""