Skip to content

Fix tests #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adafruit_ble_radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def send_bytes(self, message: bytes) -> None:
# Concatenate the bytes that make up the advertised message.
advertisement.msg = struct.pack("<BB", self._channel, self.uid) + message

self.uid = (self.uid + 1) % 255
self.uid = (self.uid + 1) % 256
# Advertise (block) for AD_DURATION period of time.
self.ble.start_advertising(advertisement)
time.sleep(AD_DURATION)
Expand Down
7 changes: 5 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
# should be mocked away. For instance, modules which are available in
# CircuitPython but not standard Python.
MOCK_MODULES = [
"adafruit_ble.BLERadio",
"adafruit_ble.advertising.adafruit.AdafruitRadio",
"adafruit_ble",
"adafruit_ble.advertising",
"adafruit_ble.advertising.standard",
"adafruit_ble.advertising.adafruit",
"_bleio",
]


Expand Down
12 changes: 7 additions & 5 deletions tests/test_adafruit_radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
import time
from unittest import mock
import pytest
from adafruit_ble.advertising import Advertisement
import adafruit_ble_radio


# pylint: disable=redefined-outer-name


@pytest.fixture
def radio():
def radio_obj():
"""
A fixture to recreate a new Radio instance for each test that needs it.
"""
Expand Down Expand Up @@ -104,9 +108,7 @@ def test_radio_send_bytes(radio_obj):
with mock.patch("adafruit_ble_radio.time.sleep") as mock_sleep:
radio_obj.send_bytes(msg)
mock_sleep.assert_called_once_with(adafruit_ble_radio.AD_DURATION)
spy_advertisement = (
adafruit_ble_radio._RadioAdvertisement() # pylint: disable=protected-access
) # pylint: disable=protected-access
spy_advertisement = Advertisement
chan = struct.pack("<B", radio_obj._channel) # pylint: disable=protected-access
uid = struct.pack("<B", 255)
assert spy_advertisement.msg == chan + uid + msg
Expand All @@ -122,7 +124,7 @@ def test_radio_receive_no_message(radio_obj):
"""
radio_obj.receive_full = mock.MagicMock(return_value=None)
assert radio_obj.receive() is None
radio_obj.receive_full.assert_called_once_with()
radio_obj.receive_full.assert_called_once_with(timeout=1.0)


def test_radio_receive(radio_obj):
Expand Down