mirror of
https://github.com/meshtastic/python.git
synced 2026-01-23 15:18:06 -05:00
refactor and write unit test
This commit is contained in:
@@ -338,40 +338,7 @@ def onConnected(interface):
|
||||
if args.configure_dump:
|
||||
# dump out the configuration (the opposite of '--configure')
|
||||
closeNow = True
|
||||
owner = interface.getLongName()
|
||||
channel_url = interface.localNode.getURL()
|
||||
myinfo = interface.getMyNodeInfo()
|
||||
pos = myinfo.get('position')
|
||||
lat = None
|
||||
lon = None
|
||||
alt = None
|
||||
if pos:
|
||||
lat = pos.get('latitude')
|
||||
lon = pos.get('longitude')
|
||||
alt = pos.get('altitude')
|
||||
|
||||
config = "# start of Meshtastic configure yaml\n"
|
||||
if owner:
|
||||
config += f"owner: {owner}\n\n"
|
||||
if channel_url:
|
||||
config += f"channel_url: {channel_url}\n\n"
|
||||
if lat or lon or alt:
|
||||
config += "location:\n"
|
||||
if lat:
|
||||
config += f" lat: {lat}\n"
|
||||
if lon:
|
||||
config += f" lon: {lon}\n"
|
||||
if alt:
|
||||
config += f" alt: {alt}\n"
|
||||
config += "\n"
|
||||
preferences = f'{interface.localNode.radioConfig.preferences}'
|
||||
prefs = preferences.splitlines()
|
||||
if prefs:
|
||||
config += "user_prefs:\n"
|
||||
for pref in prefs:
|
||||
config += f" {meshtastic.util.quoteBooleans(pref)}\n"
|
||||
|
||||
print(config)
|
||||
configure_dump(interface)
|
||||
|
||||
if args.seturl:
|
||||
closeNow = True
|
||||
@@ -553,6 +520,44 @@ def subscribe():
|
||||
# pub.subscribe(onNode, "meshtastic.node")
|
||||
|
||||
|
||||
def configure_dump(interface):
|
||||
"""Get info used in --configuration-dump"""
|
||||
owner = interface.getLongName()
|
||||
channel_url = interface.localNode.getURL()
|
||||
myinfo = interface.getMyNodeInfo()
|
||||
pos = myinfo.get('position')
|
||||
lat = None
|
||||
lon = None
|
||||
alt = None
|
||||
if pos:
|
||||
lat = pos.get('latitude')
|
||||
lon = pos.get('longitude')
|
||||
alt = pos.get('altitude')
|
||||
|
||||
config = "# start of Meshtastic configure yaml\n"
|
||||
if owner:
|
||||
config += f"owner: {owner}\n\n"
|
||||
if channel_url:
|
||||
config += f"channel_url: {channel_url}\n\n"
|
||||
if lat or lon or alt:
|
||||
config += "location:\n"
|
||||
if lat:
|
||||
config += f" lat: {lat}\n"
|
||||
if lon:
|
||||
config += f" lon: {lon}\n"
|
||||
if alt:
|
||||
config += f" alt: {alt}\n"
|
||||
config += "\n"
|
||||
preferences = f'{interface.localNode.radioConfig.preferences}'
|
||||
prefs = preferences.splitlines()
|
||||
if prefs:
|
||||
config += "user_prefs:\n"
|
||||
for pref in prefs:
|
||||
config += f" {meshtastic.util.quoteBooleans(pref)}\n"
|
||||
print(config)
|
||||
return config
|
||||
|
||||
|
||||
def common():
|
||||
"""Shared code for all of our command line wrappers"""
|
||||
our_globals = Globals.getInstance()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Meshtastic unit tests for __main__.py"""
|
||||
# pylint: disable=C0302
|
||||
|
||||
import sys
|
||||
import os
|
||||
@@ -7,7 +8,7 @@ import re
|
||||
from unittest.mock import patch, MagicMock
|
||||
import pytest
|
||||
|
||||
from meshtastic.__main__ import initParser, main, Globals, onReceive, onConnection
|
||||
from meshtastic.__main__ import initParser, main, Globals, onReceive, onConnection, configure_dump
|
||||
import meshtastic.radioconfig_pb2
|
||||
from ..serial_interface import SerialInterface
|
||||
from ..tcp_interface import TCPInterface
|
||||
@@ -1196,3 +1197,35 @@ def test_main_onConnection(reset_globals, capsys):
|
||||
out, err = capsys.readouterr()
|
||||
assert re.search(r'Connection changed: foo', out, re.MULTILINE)
|
||||
assert err == ''
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_main_configure_dump(reset_globals, capsys):
|
||||
"""Test configure_dump"""
|
||||
iface = MagicMock(autospec=SerialInterface)
|
||||
with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo:
|
||||
mo.getLongName.return_value = 'foo'
|
||||
mo.localNode.getURL.return_value = 'bar'
|
||||
mo.getMyNodeInfo().get.return_value = { 'latitudeI': 1100000000, 'longitudeI': 1200000000,
|
||||
'altitude': 100, 'batteryLevel': 34, 'latitude': 110.0,
|
||||
'longitude': 120.0}
|
||||
mo.localNode.radioConfig.preferences = """phone_timeout_secs: 900
|
||||
ls_secs: 300
|
||||
position_broadcast_smart: true
|
||||
fixed_position: true
|
||||
position_flags: 35"""
|
||||
configure_dump(mo)
|
||||
out, err = capsys.readouterr()
|
||||
assert re.search(r'owner: foo', out, re.MULTILINE)
|
||||
assert re.search(r'channel_url: bar', out, re.MULTILINE)
|
||||
assert re.search(r'location:', out, re.MULTILINE)
|
||||
assert re.search(r'lat: 110.0', out, re.MULTILINE)
|
||||
assert re.search(r'lon: 120.0', out, re.MULTILINE)
|
||||
assert re.search(r'alt: 100', out, re.MULTILINE)
|
||||
assert re.search(r'user_prefs:', out, re.MULTILINE)
|
||||
assert re.search(r'phone_timeout_secs: 900', out, re.MULTILINE)
|
||||
assert re.search(r'ls_secs: 300', out, re.MULTILINE)
|
||||
assert re.search(r"position_broadcast_smart: 'true'", out, re.MULTILINE)
|
||||
assert re.search(r"fixed_position: 'true'", out, re.MULTILINE)
|
||||
assert re.search(r"position_flags: 35", out, re.MULTILINE)
|
||||
assert err == ''
|
||||
|
||||
Reference in New Issue
Block a user