|
51 | 51 | select_jlink, |
52 | 52 | select_device_by_serial, |
53 | 53 | select_device, |
| 54 | + Comms, |
54 | 55 | ) |
55 | 56 |
|
56 | 57 | Port = namedtuple("Port", ["hwid", "device"]) |
@@ -236,6 +237,11 @@ def ports_linux_jlink_multi_com(): |
236 | 237 | m.comports.return_value = list_ports_linux_jlink_multi_com |
237 | 238 | yield m |
238 | 239 |
|
| 240 | +@pytest.fixture |
| 241 | +def mock_serial(): |
| 242 | + """Mock the serial.Serial class.""" |
| 243 | + with patch("nrfcredstore.comms.serial.Serial", autospec=True) as mock_serial: |
| 244 | + yield mock_serial |
239 | 245 |
|
240 | 246 | # Tests for get_connected_nordic_boards |
241 | 247 |
|
@@ -441,3 +447,44 @@ def test_select_device_no_list_all_more_devices( |
441 | 447 | ) |
442 | 448 | assert port.device == port_to_select.device |
443 | 449 | assert serial_number == "THINGY91X_F39CC1B120C" |
| 450 | + |
| 451 | +# tests for expect_response |
| 452 | + |
| 453 | +def test_expect_response_ok(mock_serial): |
| 454 | + with patch("nrfcredstore.comms.select_device", return_value=(Mock(), "123456789")) as mock_select: |
| 455 | + comms = Comms() |
| 456 | + comms.read_line = Mock(return_value="OK") |
| 457 | + result, output = comms.expect_response("OK", "ERROR") |
| 458 | + mock_select.assert_called_once() |
| 459 | + assert result is True |
| 460 | + assert output == '' |
| 461 | + |
| 462 | +def test_expect_response_error(mock_serial): |
| 463 | + with patch("nrfcredstore.comms.select_device", return_value=(Mock(), "123456789")) as mock_select: |
| 464 | + comms = Comms() |
| 465 | + comms.read_line = Mock(return_value="ERROR") |
| 466 | + result, output = comms.expect_response("OK", "ERROR") |
| 467 | + mock_select.assert_called_once() |
| 468 | + assert result is False |
| 469 | + assert output == '' |
| 470 | + |
| 471 | +def test_expect_response_error_not_allowed(mock_serial): |
| 472 | + with patch("nrfcredstore.comms.select_device", return_value=(Mock(), "123456789")) as mock_select: |
| 473 | + with patch("nrfcredstore.comms.logging.error") as mock_error: |
| 474 | + comms = Comms() |
| 475 | + comms.read_line = Mock(return_value="+CME ERROR: 514") |
| 476 | + result, output = comms.expect_response("OK", "ERROR") |
| 477 | + mock_select.assert_called_once() |
| 478 | + assert result is False |
| 479 | + assert output == '' |
| 480 | + mock_error.assert_called_once_with("AT command error: Not allowed") |
| 481 | + |
| 482 | +def test_expect_response_timeout(mock_serial): |
| 483 | + with patch("nrfcredstore.comms.select_device", return_value=(Mock(), "123456789")) as mock_select: |
| 484 | + comms = Comms() |
| 485 | + comms.read_line = Mock(return_value="") |
| 486 | + result, output = comms.expect_response("OK", "ERROR", timeout=1) |
| 487 | + mock_select.assert_called_once() |
| 488 | + assert result is False |
| 489 | + assert output == '' |
| 490 | + |
0 commit comments