Skip to content

Commit 66d53d7

Browse files
committed
more comms tests
1 parent 2af7088 commit 66d53d7

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tests/test_comms.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
select_jlink,
5252
select_device_by_serial,
5353
select_device,
54+
Comms,
5455
)
5556

5657
Port = namedtuple("Port", ["hwid", "device"])
@@ -236,6 +237,11 @@ def ports_linux_jlink_multi_com():
236237
m.comports.return_value = list_ports_linux_jlink_multi_com
237238
yield m
238239

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
239245

240246
# Tests for get_connected_nordic_boards
241247

@@ -441,3 +447,44 @@ def test_select_device_no_list_all_more_devices(
441447
)
442448
assert port.device == port_to_select.device
443449
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

Comments
 (0)