Skip to content

Commit 9c66447

Browse files
authored
Merge pull request #201 from mkinney/more_testing
added tests for _getOrCreateByNum(), nodeNumToId(), and _fixupPositio…
2 parents 9d0bc09 + 65960fb commit 9c66447

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

meshtastic/mesh_interface.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ def _handleFromRadio(self, fromRadioBytes):
507507
elif fromRadio.HasField("node_info"):
508508
node = asDict["nodeInfo"]
509509
try:
510-
self._fixupPosition(node["position"])
510+
newpos = self._fixupPosition(node["position"])
511+
node["position"] = newpos
511512
except:
512513
logging.debug("Node without position")
513514

@@ -539,12 +540,14 @@ def _fixupPosition(self, position):
539540
"""Convert integer lat/lon into floats
540541
541542
Arguments:
542-
position {Position dictionary} -- object ot fix up
543+
position {Position dictionary} -- object to fix up
544+
Returns the position with the updated keys
543545
"""
544546
if "latitudeI" in position:
545547
position["latitude"] = position["latitudeI"] * 1e-7
546548
if "longitudeI" in position:
547549
position["longitude"] = position["longitudeI"] * 1e-7
550+
return position
548551

549552
def _nodeNumToId(self, num):
550553
"""Map a node node number to a node ID

meshtastic/tests/test_mesh_interface.py

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def test_MeshInterface(capsys, reset_globals):
5757
def test_getMyUser(reset_globals, iface_with_nodes):
5858
"""Test getMyUser()"""
5959
iface = iface_with_nodes
60-
6160
iface.myInfo.my_node_num = 2475227164
6261
myuser = iface.getMyUser()
6362
assert myuser is not None
@@ -459,3 +458,88 @@ def test_generatePacketId(capsys, reset_globals):
459458
assert re.search(r'Not connected yet, can not generate packet', out, re.MULTILINE)
460459
assert err == ''
461460
assert pytest_wrapped_e.type == Exception
461+
462+
463+
@pytest.mark.unit
464+
def test_fixupPosition_empty_pos(capsys, reset_globals):
465+
"""Test _fixupPosition()"""
466+
iface = MeshInterface(noProto=True)
467+
pos = {}
468+
newpos = iface._fixupPosition(pos)
469+
assert newpos == pos
470+
471+
472+
@pytest.mark.unit
473+
def test_fixupPosition_no_changes_needed(capsys, reset_globals):
474+
"""Test _fixupPosition()"""
475+
iface = MeshInterface(noProto=True)
476+
pos = {"latitude": 101, "longitude": 102}
477+
newpos = iface._fixupPosition(pos)
478+
assert newpos == pos
479+
480+
481+
@pytest.mark.unit
482+
def test_fixupPosition(capsys, reset_globals):
483+
"""Test _fixupPosition()"""
484+
iface = MeshInterface(noProto=True)
485+
pos = {"latitudeI": 1010000000, "longitudeI": 1020000000}
486+
newpos = iface._fixupPosition(pos)
487+
assert newpos == {"latitude": 101.0,
488+
"latitudeI": 1010000000,
489+
"longitude": 102.0,
490+
"longitudeI": 1020000000}
491+
492+
493+
@pytest.mark.unit
494+
def test_nodeNumToId(capsys, reset_globals, iface_with_nodes):
495+
"""Test _nodeNumToId()"""
496+
iface = iface_with_nodes
497+
iface.myInfo.my_node_num = 2475227164
498+
someid = iface._nodeNumToId(2475227164)
499+
assert someid == '!9388f81c'
500+
501+
502+
@pytest.mark.unit
503+
def test_nodeNumToId_not_found(capsys, reset_globals, iface_with_nodes):
504+
"""Test _nodeNumToId()"""
505+
iface = iface_with_nodes
506+
iface.myInfo.my_node_num = 2475227164
507+
someid = iface._nodeNumToId(123)
508+
assert someid is None
509+
510+
511+
@pytest.mark.unit
512+
def test_nodeNumToId_to_all(capsys, reset_globals, iface_with_nodes):
513+
"""Test _nodeNumToId()"""
514+
iface = iface_with_nodes
515+
iface.myInfo.my_node_num = 2475227164
516+
someid = iface._nodeNumToId(0xffffffff)
517+
assert someid == '^all'
518+
519+
520+
@pytest.mark.unit
521+
def test_getOrCreateByNum_minimal(capsys, reset_globals, iface_with_nodes):
522+
"""Test _getOrCreateByNum()"""
523+
iface = iface_with_nodes
524+
iface.myInfo.my_node_num = 2475227164
525+
tmp = iface._getOrCreateByNum(123)
526+
assert tmp == {'num': 123}
527+
528+
529+
@pytest.mark.unit
530+
def test_getOrCreateByNum_not_found(capsys, reset_globals, iface_with_nodes):
531+
"""Test _getOrCreateByNum()"""
532+
iface = iface_with_nodes
533+
iface.myInfo.my_node_num = 2475227164
534+
with pytest.raises(Exception) as pytest_wrapped_e:
535+
iface._getOrCreateByNum(0xffffffff)
536+
assert pytest_wrapped_e.type == Exception
537+
538+
539+
@pytest.mark.unit
540+
def test_getOrCreateByNum(capsys, reset_globals, iface_with_nodes):
541+
"""Test _getOrCreateByNum()"""
542+
iface = iface_with_nodes
543+
iface.myInfo.my_node_num = 2475227164
544+
tmp = iface._getOrCreateByNum(2475227164)
545+
assert tmp['num'] == 2475227164

0 commit comments

Comments
 (0)