Merge pull request #151 from mkinney/work_on_unit_tests

Work on unit tests
This commit is contained in:
mkinney
2021-12-09 09:18:43 -08:00
committed by GitHub
9 changed files with 101 additions and 12 deletions

View File

@@ -1,2 +1,2 @@
[run]
omit = meshtastic/*_pb2.py,meshtastic/test.py
omit = meshtastic/*_pb2.py,meshtastic/test.py,meshtastic/test/*.py

View File

@@ -29,6 +29,6 @@ jobs:
which meshtastic
meshtastic --version
- name: Run pylint
run: pylint --exit-zero meshtastic
run: pylint meshtastic
- name: Run tests with pytest
run: pytest --cov=meshtastic

View File

@@ -6,8 +6,8 @@ import os
import stat
import serial
import meshtastic.util
from .stream_interface import StreamInterface
from .util import findPorts, our_exit
class SerialInterface(StreamInterface):
"""Interface class for meshtastic devices over a serial link"""
@@ -22,14 +22,14 @@ class SerialInterface(StreamInterface):
"""
if devPath is None:
ports = findPorts()
ports = meshtastic.util.findPorts()
logging.debug(f"ports:{ports}")
if len(ports) == 0:
our_exit("Warning: No Meshtastic devices detected.")
meshtastic.util.our_exit("Warning: No Meshtastic devices detected.")
elif len(ports) > 1:
message = "Warning: Multiple serial ports were detected so one serial port must be specified with the '--port'.\n"
message += f" Ports detected:{ports}"
our_exit(message)
meshtastic.util.our_exit(message)
else:
devPath = ports[0]

View File

@@ -1,15 +1,15 @@
"""Meshtastic unit tests for node.py"""
"""Meshtastic unit tests for mesh_interface.py"""
import re
import pytest
from meshtastic.mesh_interface import MeshInterface
from ..mesh_interface import MeshInterface
@pytest.mark.unit
def test_MeshInterface(capsys):
"""Test that we instantiate a MeshInterface"""
"""Test that we can instantiate a MeshInterface"""
iface = MeshInterface(noProto=True)
iface.showInfo()
iface.localNode.showInfo()

View File

@@ -0,0 +1,49 @@
"""Meshtastic unit tests for serial_interface.py"""
import re
from unittest.mock import patch
import pytest
from ..serial_interface import SerialInterface
@pytest.mark.unit
@patch('serial.Serial')
@patch('meshtastic.util.findPorts', return_value=['/dev/ttyUSBfake'])
def test_SerialInterface_single_port(mocked_findPorts, mocked_serial):
"""Test that we can instantiate a SerialInterface with a single port"""
iface = SerialInterface(noProto=True)
iface.showInfo()
iface.localNode.showInfo()
iface.close()
mocked_findPorts.assert_called()
mocked_serial.assert_called()
@pytest.mark.unit
@patch('meshtastic.util.findPorts', return_value=[])
def test_SerialInterface_no_ports(mocked_findPorts, capsys):
"""Test that we can instantiate a SerialInterface with no ports"""
with pytest.raises(SystemExit) as pytest_wrapped_e:
SerialInterface(noProto=True)
mocked_findPorts.assert_called()
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1
out, err = capsys.readouterr()
assert re.search(r'Warning: No Meshtastic devices detected', out, re.MULTILINE)
assert err == ''
@pytest.mark.unit
@patch('meshtastic.util.findPorts', return_value=['/dev/ttyUSBfake1', '/dev/ttyUSBfake2'])
def test_SerialInterface_multiple_ports(mocked_findPorts, capsys):
"""Test that we can instantiate a SerialInterface with two ports"""
with pytest.raises(SystemExit) as pytest_wrapped_e:
SerialInterface(noProto=True)
mocked_findPorts.assert_called()
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1
out, err = capsys.readouterr()
assert re.search(r'Warning: Multiple serial ports were detected', out, re.MULTILINE)
assert err == ''

View File

@@ -8,7 +8,7 @@ import os
# sense to pause for the radio at apprpriate times
import pytest
import meshtastic
from ..util import findPorts
# seconds to pause after running a meshtastic command
PAUSE_AFTER_COMMAND = 2
@@ -143,7 +143,7 @@ def test_smoke1_send_hello():
def test_smoke1_port():
"""Test --port"""
# first, get the ports
ports = meshtastic.util.findPorts()
ports = findPorts()
# hopefully there is just one
assert len(ports) == 1
port = ports[0]

View File

@@ -0,0 +1,14 @@
"""Meshtastic unit tests for stream_interface.py"""
import pytest
from ..stream_interface import StreamInterface
@pytest.mark.unit
def test_StreamInterface():
"""Test that we can instantiate a StreamInterface"""
with pytest.raises(Exception) as pytest_wrapped_e:
StreamInterface(noProto=True)
assert pytest_wrapped_e.type == Exception

View File

@@ -0,0 +1,26 @@
"""Meshtastic unit tests for tcp_interface.py"""
import re
from unittest.mock import patch
import pytest
from ..tcp_interface import TCPInterface
@pytest.mark.unit
def test_TCPInterface(capsys):
"""Test that we can instantiate a TCPInterface"""
with patch('socket.socket') as mock_socket:
iface = TCPInterface(hostname='localhost', noProto=True)
iface.showInfo()
iface.localNode.showInfo()
out, err = capsys.readouterr()
assert re.search(r'Owner: None \(None\)', out, re.MULTILINE)
assert re.search(r'Nodes', out, re.MULTILINE)
assert re.search(r'Preferences', out, re.MULTILINE)
assert re.search(r'Channels', out, re.MULTILINE)
assert re.search(r'Primary channel URL', out, re.MULTILINE)
assert err == ''
assert mock_socket.called
iface.close()

View File

@@ -1,4 +1,4 @@
"""Meshtastic unit tests for node.py"""
"""Meshtastic unit tests for util.py"""
import re