Skip to content

Commit 098b19c

Browse files
authored
Improve coverage (#157)
1 parent f1eeca8 commit 098b19c

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

tests/test_api.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ async def test_connect(monkeypatch):
3131

3232
def test_close(api):
3333
uart = api._uart
34+
conn_lost_task = mock.MagicMock()
35+
api._conn_lost_task = conn_lost_task
36+
3437
api.close()
38+
39+
assert api._conn_lost_task is None
40+
assert conn_lost_task.cancel.call_count == 1
3541
assert api._uart is None
3642
assert uart.close.call_count == 1
3743

@@ -564,7 +570,7 @@ async def test_reconnect_multiple_attempts(monkeypatch, caplog):
564570

565571

566572
@mock.patch.object(xbee_api.XBee, "_at_command", new_callable=mock.AsyncMock)
567-
@mock.patch.object(uart, "connect")
573+
@mock.patch.object(uart, "connect", return_value=mock.MagicMock())
568574
async def test_probe_success(mock_connect, mock_at_cmd):
569575
"""Test device probing."""
570576

@@ -579,7 +585,7 @@ async def test_probe_success(mock_connect, mock_at_cmd):
579585

580586
@mock.patch.object(xbee_api.XBee, "init_api_mode", return_value=True)
581587
@mock.patch.object(xbee_api.XBee, "_at_command", side_effect=asyncio.TimeoutError)
582-
@mock.patch.object(uart, "connect")
588+
@mock.patch.object(uart, "connect", return_value=mock.MagicMock())
583589
async def test_probe_success_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
584590
"""Test device probing."""
585591

@@ -595,7 +601,7 @@ async def test_probe_success_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
595601

596602
@mock.patch.object(xbee_api.XBee, "init_api_mode")
597603
@mock.patch.object(xbee_api.XBee, "_at_command", side_effect=asyncio.TimeoutError)
598-
@mock.patch.object(uart, "connect")
604+
@mock.patch.object(uart, "connect", return_value=mock.MagicMock())
599605
@pytest.mark.parametrize(
600606
"exception",
601607
(asyncio.TimeoutError, serial.SerialException, zigpy.exceptions.APIException),
@@ -619,7 +625,7 @@ async def test_probe_fail(mock_connect, mock_at_cmd, mock_api_mode, exception):
619625

620626
@mock.patch.object(xbee_api.XBee, "init_api_mode", return_value=False)
621627
@mock.patch.object(xbee_api.XBee, "_at_command", side_effect=asyncio.TimeoutError)
622-
@mock.patch.object(uart, "connect")
628+
@mock.patch.object(uart, "connect", return_value=mock.MagicMock())
623629
async def test_probe_fail_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
624630
"""Test device probing fails."""
625631

@@ -636,7 +642,7 @@ async def test_probe_fail_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
636642
assert mock_connect.return_value.close.call_count == 1
637643

638644

639-
@mock.patch.object(xbee_api.XBee, "connect")
645+
@mock.patch.object(xbee_api.XBee, "connect", return_value=mock.MagicMock())
640646
async def test_xbee_new(conn_mck):
641647
"""Test new class method."""
642648
api = await xbee_api.XBee.new(mock.sentinel.application, DEVICE_CONFIG)

tests/test_application.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,16 @@ async def test_get_association_state(app):
279279
assert ai is mock.sentinel.ai
280280

281281

282-
async def test_write_network_info(app, node_info, network_info):
283-
app._api._queued_at = mock.AsyncMock(spec=XBee._queued_at)
282+
@pytest.mark.parametrize("legacy_module", (False, True))
283+
async def test_write_network_info(app, node_info, network_info, legacy_module):
284+
def _mock_queued_at(name, *args):
285+
if legacy_module and name == "CE":
286+
raise RuntimeError("Legacy module")
287+
return "OK"
288+
289+
app._api._queued_at = mock.AsyncMock(
290+
spec=XBee._queued_at, side_effect=_mock_queued_at
291+
)
284292
app._api._at_command = mock.AsyncMock(spec=XBee._at_command)
285293
app._api._running = mock.AsyncMock(spec=app._api._running)
286294

@@ -308,7 +316,7 @@ async def _test_start_network(
308316
legacy_module=False,
309317
):
310318
ai_tries = 5
311-
app.state.node_info.nwk = mock.sentinel.nwk
319+
app.state.node_info = zigpy.state.NodeInfo()
312320

313321
def _at_command_mock(cmd, *args):
314322
nonlocal ai_tries
@@ -348,7 +356,6 @@ def init_api_mode_mock():
348356
await app.connect()
349357

350358
app.form_network = mock.AsyncMock()
351-
await app.load_network_info()
352359
await app.start_network()
353360
return app
354361

@@ -459,6 +466,25 @@ async def test_request_send_fail(app):
459466
await _test_request(app, send_success=False)
460467

461468

469+
async def test_request_unknown_device(app):
470+
dev = zigpy.device.Device(
471+
application=app, ieee=xbee_t.UNKNOWN_IEEE, nwk=xbee_t.UNKNOWN_NWK
472+
)
473+
with pytest.raises(
474+
zigpy.exceptions.DeliveryError,
475+
match="Cannot send a packet to a device without a known IEEE address",
476+
):
477+
await app.request(
478+
dev,
479+
0x0260,
480+
1,
481+
2,
482+
3,
483+
123,
484+
b"\xaa\x55\xbe\xef",
485+
)
486+
487+
462488
async def test_request_extended_timeout(app):
463489
r = await _test_request(app, True, True, extended_timeout=False)
464490
assert r[0] == xbee_t.TXStatus.SUCCESS
@@ -485,12 +511,14 @@ async def test_shutdown(app):
485511
assert mack_close.call_count == 1
486512

487513

488-
def test_remote_at_cmd(app, device):
514+
async def test_remote_at_cmd(app, device):
489515
dev = device()
490516
app.get_device = mock.MagicMock(return_value=dev)
491517
app._api = mock.MagicMock(spec=XBee)
492518
s = mock.sentinel
493-
app.remote_at_command(s.nwk, s.cmd, s.data, apply_changes=True, encryption=True)
519+
await app.remote_at_command(
520+
s.nwk, s.cmd, s.data, apply_changes=True, encryption=True
521+
)
494522
assert app._api._remote_at_command.call_count == 1
495523
assert app._api._remote_at_command.call_args[0][0] is dev.ieee
496524
assert app._api._remote_at_command.call_args[0][1] == s.nwk

0 commit comments

Comments
 (0)