Skip to content

Commit

Permalink
Merge pull request #25 from tekktrik/dev/fix-pytests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
tekktrik committed May 25, 2023
2 parents 557086a + 35efafd commit d39ad2b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
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

0 comments on commit d39ad2b

Please sign in to comment.