|
3 | 3 |
|
4 | 4 | from asynctest import CoroutineMock, mock
|
5 | 5 | import pytest
|
| 6 | +import serial |
6 | 7 | import zigpy.exceptions
|
7 | 8 |
|
8 | 9 | from zigpy_xbee import api as xbee_api, types as t, uart
|
@@ -588,3 +589,80 @@ async def test_reconnect_multiple_attempts(monkeypatch, caplog):
|
588 | 589 |
|
589 | 590 | assert api._uart is mock.sentinel.uart_reconnect
|
590 | 591 | assert connect_mock.call_count == 3
|
| 592 | + |
| 593 | + |
| 594 | +@pytest.mark.asyncio |
| 595 | +@mock.patch.object(xbee_api.XBee, "_at_command", new_callable=CoroutineMock) |
| 596 | +@mock.patch.object(uart, "connect") |
| 597 | +async def test_probe_success(mock_connect, mock_at_cmd): |
| 598 | + """Test device probing.""" |
| 599 | + |
| 600 | + res = await xbee_api.XBee.probe(mock.sentinel.uart, mock.sentinel.baud) |
| 601 | + assert res is True |
| 602 | + assert mock_connect.call_count == 1 |
| 603 | + assert mock_connect.await_count == 1 |
| 604 | + assert mock_connect.call_args[0][0] is mock.sentinel.uart |
| 605 | + assert mock_at_cmd.call_count == 1 |
| 606 | + assert mock_connect.return_value.close.call_count == 1 |
| 607 | + |
| 608 | + |
| 609 | +@pytest.mark.asyncio |
| 610 | +@mock.patch.object(xbee_api.XBee, "init_api_mode", return_value=True) |
| 611 | +@mock.patch.object(xbee_api.XBee, "_at_command", side_effect=asyncio.TimeoutError) |
| 612 | +@mock.patch.object(uart, "connect") |
| 613 | +async def test_probe_success_api_mode(mock_connect, mock_at_cmd, mock_api_mode): |
| 614 | + """Test device probing.""" |
| 615 | + |
| 616 | + res = await xbee_api.XBee.probe(mock.sentinel.uart, mock.sentinel.baud) |
| 617 | + assert res is True |
| 618 | + assert mock_connect.call_count == 1 |
| 619 | + assert mock_connect.await_count == 1 |
| 620 | + assert mock_connect.call_args[0][0] is mock.sentinel.uart |
| 621 | + assert mock_at_cmd.call_count == 1 |
| 622 | + assert mock_api_mode.call_count == 1 |
| 623 | + assert mock_connect.return_value.close.call_count == 1 |
| 624 | + |
| 625 | + |
| 626 | +@pytest.mark.asyncio |
| 627 | +@mock.patch.object(xbee_api.XBee, "init_api_mode") |
| 628 | +@mock.patch.object(xbee_api.XBee, "_at_command", side_effect=asyncio.TimeoutError) |
| 629 | +@mock.patch.object(uart, "connect") |
| 630 | +@pytest.mark.parametrize( |
| 631 | + "exception", |
| 632 | + (asyncio.TimeoutError, serial.SerialException, zigpy.exceptions.APIException), |
| 633 | +) |
| 634 | +async def test_probe_fail(mock_connect, mock_at_cmd, mock_api_mode, exception): |
| 635 | + """Test device probing fails.""" |
| 636 | + |
| 637 | + mock_api_mode.side_effect = exception |
| 638 | + mock_api_mode.reset_mock() |
| 639 | + mock_at_cmd.reset_mock() |
| 640 | + mock_connect.reset_mock() |
| 641 | + res = await xbee_api.XBee.probe(mock.sentinel.uart, mock.sentinel.baud) |
| 642 | + assert res is False |
| 643 | + assert mock_connect.call_count == 1 |
| 644 | + assert mock_connect.await_count == 1 |
| 645 | + assert mock_connect.call_args[0][0] is mock.sentinel.uart |
| 646 | + assert mock_at_cmd.call_count == 1 |
| 647 | + assert mock_api_mode.call_count == 1 |
| 648 | + assert mock_connect.return_value.close.call_count == 1 |
| 649 | + |
| 650 | + |
| 651 | +@pytest.mark.asyncio |
| 652 | +@mock.patch.object(xbee_api.XBee, "init_api_mode", return_value=False) |
| 653 | +@mock.patch.object(xbee_api.XBee, "_at_command", side_effect=asyncio.TimeoutError) |
| 654 | +@mock.patch.object(uart, "connect") |
| 655 | +async def test_probe_fail_api_mode(mock_connect, mock_at_cmd, mock_api_mode): |
| 656 | + """Test device probing fails.""" |
| 657 | + |
| 658 | + mock_api_mode.reset_mock() |
| 659 | + mock_at_cmd.reset_mock() |
| 660 | + mock_connect.reset_mock() |
| 661 | + res = await xbee_api.XBee.probe(mock.sentinel.uart, mock.sentinel.baud) |
| 662 | + assert res is False |
| 663 | + assert mock_connect.call_count == 1 |
| 664 | + assert mock_connect.await_count == 1 |
| 665 | + assert mock_connect.call_args[0][0] is mock.sentinel.uart |
| 666 | + assert mock_at_cmd.call_count == 1 |
| 667 | + assert mock_api_mode.call_count == 1 |
| 668 | + assert mock_connect.return_value.close.call_count == 1 |
0 commit comments