Skip to content

Commit f0de977

Browse files
committed
Some more test improvements
1 parent c118334 commit f0de977

2 files changed

Lines changed: 105 additions & 31 deletions

File tree

meshtastic/tests/test_showNodes_favorite.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,6 @@ def test_showNodes_favorite_asterisk_display(capsys, _iface_with_favorite_nodes)
153153
assert err == ""
154154

155155

156-
@pytest.mark.unit
157-
def test_showNodes_favorite_field_formatting():
158-
"""Test the formatting logic for isFavorite field"""
159-
# Test favorite node
160-
raw_value = True
161-
formatted_value = "*" if raw_value else ""
162-
assert formatted_value == "*"
163-
164-
# Test non-favorite node
165-
raw_value = False
166-
formatted_value = "*" if raw_value else ""
167-
assert formatted_value == ""
168-
169-
# Test None/missing value
170-
raw_value = None
171-
formatted_value = "*" if raw_value else ""
172-
assert formatted_value == ""
173-
174-
175156
@pytest.mark.unit
176157
def test_showNodes_with_custom_fields_including_favorite(capsys, _iface_with_favorite_nodes):
177158
"""Test that isFavorite can be specified in custom showFields"""

meshtastic/tests/test_util.py

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import json
44
import logging
55
import re
6+
import time
7+
from types import SimpleNamespace
68
from unittest.mock import patch
79

810
import pytest
@@ -236,20 +238,111 @@ def test_remove_keys_from_dict_nested():
236238
assert remove_keys_from_dict(keys, adict) == exp
237239

238240

239-
@pytest.mark.unitslow
240-
def test_Timeout_not_found():
241-
"""Test Timeout()"""
242-
to = Timeout(0.2)
243-
attrs = "foo"
244-
to.waitForSet("bar", attrs)
241+
@pytest.mark.unit
242+
def test_Timeout_waitForSet_found():
243+
"""waitForSet returns True when all required attrs are truthy on target."""
244+
to = Timeout(0.01)
245+
target = SimpleNamespace(foo=1, bar="ok")
246+
assert to.waitForSet(target, ("foo", "bar")) is True
245247

246248

247-
@pytest.mark.unitslow
248-
def test_Timeout_found():
249-
"""Test Timeout()"""
250-
to = Timeout(0.2)
251-
attrs = ()
252-
to.waitForSet("bar", attrs)
249+
@pytest.mark.unit
250+
@patch("meshtastic.util.time.sleep")
251+
def test_Timeout_waitForSet_not_found(mock_sleep): # pylint: disable=unused-argument
252+
"""waitForSet returns False when attrs remain falsy until timeout."""
253+
to = Timeout(0.01)
254+
target = SimpleNamespace(foo=None, bar=0)
255+
assert to.waitForSet(target, ("foo", "bar")) is False
256+
257+
258+
@pytest.mark.unit
259+
def test_Timeout_waitForSet_empty_attrs():
260+
"""waitForSet returns True immediately when attrs is empty (vacuous truth)."""
261+
to = Timeout(0.01)
262+
assert to.waitForSet(object(), ()) is True
263+
264+
265+
@pytest.mark.unit
266+
def test_Timeout_reset():
267+
"""reset() sets expireTime to now + expireTimeout."""
268+
to = Timeout(maxSecs=5)
269+
before = time.time()
270+
to.reset()
271+
assert to.expireTime == pytest.approx(before + 5, abs=0.1)
272+
273+
274+
@pytest.mark.unit
275+
def test_Timeout_reset_custom():
276+
"""reset(expireTimeout) overrides the stored expireTimeout."""
277+
to = Timeout(maxSecs=5)
278+
before = time.time()
279+
to.reset(expireTimeout=10)
280+
assert to.expireTime == pytest.approx(before + 10, abs=0.1)
281+
282+
283+
@pytest.mark.unit
284+
def test_Timeout_waitForAckNak_found():
285+
"""waitForAckNak returns True when any acknowledgment attr is set, and clears it."""
286+
to = Timeout(0.01)
287+
ack = Acknowledgment()
288+
ack.receivedAck = True
289+
assert to.waitForAckNak(ack) is True
290+
assert ack.receivedAck is False # cleared after detection
291+
292+
293+
@pytest.mark.unit
294+
@patch("meshtastic.util.time.sleep")
295+
def test_Timeout_waitForAckNak_not_found(mock_sleep): # pylint: disable=unused-argument
296+
"""waitForAckNak returns False when no acknowledgment attr is set."""
297+
to = Timeout(0.01)
298+
ack = Acknowledgment()
299+
assert to.waitForAckNak(ack) is False
300+
301+
302+
@pytest.mark.unit
303+
def test_Timeout_waitForTraceRoute_found():
304+
"""waitForTraceRoute returns True on receivedTraceRoute, and clears it."""
305+
to = Timeout(0.01)
306+
ack = Acknowledgment()
307+
ack.receivedTraceRoute = True
308+
assert to.waitForTraceRoute(3.0, ack) is True
309+
assert ack.receivedTraceRoute is False
310+
311+
312+
@pytest.mark.unit
313+
@patch("meshtastic.util.time.sleep")
314+
def test_Timeout_waitForTraceRoute_not_found(mock_sleep): # pylint: disable=unused-argument
315+
"""waitForTraceRoute returns False on timeout."""
316+
to = Timeout(0.01)
317+
ack = Acknowledgment()
318+
assert to.waitForTraceRoute(3.0, ack) is False
319+
320+
321+
@pytest.mark.unit
322+
def test_Timeout_waitForTelemetry_found():
323+
"""waitForTelemetry returns True when receivedTelemetry is set."""
324+
to = Timeout(0.01)
325+
ack = Acknowledgment()
326+
ack.receivedTelemetry = True
327+
assert to.waitForTelemetry(ack) is True
328+
329+
330+
@pytest.mark.unit
331+
def test_Timeout_waitForPosition_found():
332+
"""waitForPosition returns True when receivedPosition is set."""
333+
to = Timeout(0.01)
334+
ack = Acknowledgment()
335+
ack.receivedPosition = True
336+
assert to.waitForPosition(ack) is True
337+
338+
339+
@pytest.mark.unit
340+
def test_Timeout_waitForWaypoint_found():
341+
"""waitForWaypoint returns True when receivedWaypoint is set."""
342+
to = Timeout(0.01)
343+
ack = Acknowledgment()
344+
ack.receivedWaypoint = True
345+
assert to.waitForWaypoint(ack) is True
253346

254347

255348
@pytest.mark.unitslow

0 commit comments

Comments
 (0)