Skip to content

Commit

Permalink
test: starting testing client._handle_tx
Browse files Browse the repository at this point in the history
  • Loading branch information
tahnok committed Sep 12, 2024
1 parent b23a3d4 commit db27fde
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions colmi_r02_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ async def connect(self):
assert rx_char
self.rx_char = rx_char

await self.bleak_client.start_notify(UART_TX_CHAR_UUID, self.handle_tx)
await self.bleak_client.start_notify(UART_TX_CHAR_UUID, self._handle_tx)

async def disconnect(self):
await self.bleak_client.disconnect()

def handle_tx(self, _: BleakGATTCharacteristic, packet: bytearray) -> None:
def _handle_tx(self, _: BleakGATTCharacteristic, packet: bytearray) -> None:
"""Bleak callback that handles new packets from the ring."""

logger.info(f"Received packet {packet}")

assert len(packet) == 16, f"Packet is the wrong length {packet}"
packet_type = packet[0]
assert packet_type < 127, f"Packet has error bit set {packet}"

Expand Down
22 changes: 22 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from unittest.mock import Mock
import pytest

from bleak.backends.characteristic import BleakGATTCharacteristic

from colmi_r02_client.client import Client

MOCK_CHAR = Mock(spec=BleakGATTCharacteristic)


def test_handle_tx_short_packet():
client = Client("unused")

with pytest.raises(AssertionError, match="Packet is the wrong length"):
client._handle_tx(MOCK_CHAR, bytearray())


def test_handle_tx_error_bit():
client = Client("unused")

with pytest.raises(AssertionError, match="Packet has error bit"):
client._handle_tx(MOCK_CHAR, bytearray(b"\xf1" * 16))

0 comments on commit db27fde

Please sign in to comment.