add main unit tests for --set-team

This commit is contained in:
Mike Kinney
2021-12-12 14:37:41 -08:00
parent f3490f80fa
commit 59e14d3741
2 changed files with 72 additions and 5 deletions

View File

@@ -16,7 +16,7 @@ import meshtastic.test
from .tcp_interface import TCPInterface
from .ble_interface import BLEInterface
from . import remote_hardware
from . import portnums_pb2, channel_pb2, mesh_pb2, radioconfig_pb2
from . import portnums_pb2, channel_pb2, radioconfig_pb2
from .globals import Globals
"""We only import the tunnel code if we are on a platform that can run it"""
@@ -219,14 +219,14 @@ def onConnected(interface):
if args.set_team:
closeNow = True
try:
v_team = mesh_pb2.Team.Value(args.set_team.upper())
v_team = meshtastic.mesh_pb2.Team.Value(args.set_team.upper())
except ValueError:
v_team = 0
print(f"ERROR: Team \'{args.set_team}\' not found.")
print("Try a team name from the list below, or CLEAR for unaffiliated:")
print(mesh_pb2.Team.keys())
print("Try a team name from the sorted list below, or use 'CLEAR' for unaffiliated:")
print(sorted(meshtastic.mesh_pb2.Team.keys()))
else:
print(f"Setting team to {mesh_pb2.Team.Name(v_team)}")
print(f"Setting team to {meshtastic.mesh_pb2.Team.Name(v_team)}")
getNode().setOwner(team=v_team)
if args.set_ham:

View File

@@ -537,3 +537,70 @@ def test_main_setalt(capsys):
# TODO: Why does this not work? assert re.search(r'inside mocked writeConfig', out, re.MULTILINE)
assert err == ''
mo.assert_called()
@pytest.mark.unit
def test_main_set_team_valid(capsys):
"""Test --set-team"""
sys.argv = ['', '--set-team', 'CYAN']
args = sys.argv
parser = None
parser = argparse.ArgumentParser()
our_globals = Globals.getInstance()
our_globals.set_parser(parser)
our_globals.set_args(args)
our_globals.set_target_node(None)
mocked_node = MagicMock(autospec=Node)
def mock_setOwner(team):
print('inside mocked setOwner')
mocked_node.setOwner.side_effect = mock_setOwner
iface = MagicMock(autospec=SerialInterface)
iface.localNode.return_value = mocked_node
with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo:
with patch('meshtastic.mesh_pb2.Team') as mm:
mm.Name.return_value = 'FAKENAME'
mm.Value.return_value = 'FAKEVAL'
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'Setting team to', out, re.MULTILINE)
assert err == ''
mo.assert_called()
mm.Name.assert_called()
mm.Value.assert_called()
@pytest.mark.unit
def test_main_set_team_invalid(capsys):
"""Test --set-team using an invalid team name"""
sys.argv = ['', '--set-team', 'NOTCYAN']
args = sys.argv
parser = None
parser = argparse.ArgumentParser()
our_globals = Globals.getInstance()
our_globals.set_parser(parser)
our_globals.set_args(args)
our_globals.set_target_node(None)
iface = MagicMock(autospec=SerialInterface)
def throw_an_exception(exc):
raise ValueError("Fake exception.")
with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo:
with patch('meshtastic.mesh_pb2.Team') as mm:
mm.Value.side_effect = throw_an_exception
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'ERROR: Team', out, re.MULTILINE)
assert err == ''
mo.assert_called()
mm.Value.assert_called()