test: add pytest-asyncio, tests for client._handle_tx

This commit is contained in:
Wesley Ellis 2024-09-14 08:56:09 -04:00
parent b4d8fb1a70
commit d74d238e07
3 changed files with 69 additions and 6 deletions

20
poetry.lock generated
View File

@ -515,6 +515,24 @@ pluggy = ">=1.5,<2"
[package.extras]
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
[[package]]
name = "pytest-asyncio"
version = "0.24.0"
description = "Pytest support for asyncio"
optional = false
python-versions = ">=3.8"
files = [
{file = "pytest_asyncio-0.24.0-py3-none-any.whl", hash = "sha256:a811296ed596b69bf0b6f3dc40f83bcaf341b155a269052d82efa2b25ac7037b"},
{file = "pytest_asyncio-0.24.0.tar.gz", hash = "sha256:d081d828e576d85f875399194281e92bf8a68d60d72d1a2faf2feddb6c46b276"},
]
[package.dependencies]
pytest = ">=8.2,<9"
[package.extras]
docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1.0)"]
testing = ["coverage (>=6.2)", "hypothesis (>=5.7.1)"]
[[package]]
name = "python-dateutil"
version = "2.9.0.post0"
@ -821,4 +839,4 @@ all = ["winrt-Windows.Foundation.Collections[all] (==2.1.0)", "winrt-Windows.Fou
[metadata]
lock-version = "2.0"
python-versions = ">=3.11,<3.13"
content-hash = "64f162bccb27953b3e3d30aaa341967071c76ffea8fc78c5e971c6a04a580d6b"
content-hash = "a1a7ae97e6c5b47dc497be6b096d5fcefb15165c7dc940532dd54d4798782822"

View File

@ -21,6 +21,7 @@ ruff = "^0.6.0"
mypy = "^1.11.1"
freezegun = "^1.5.1"
hypothesis = "^6.112.0"
pytest-asyncio = "^0.24.0"
[tool.poetry.group.doc.dependencies]
@ -39,6 +40,10 @@ warn_redundant_casts = true
strict_equality = true
disallow_incomplete_defs = true
[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
[tool.ruff]
line-length = 125

View File

@ -1,22 +1,62 @@
import logging
from unittest.mock import Mock
import pytest
from bleak.backends.characteristic import BleakGATTCharacteristic
from hypothesis import given
import hypothesis.strategies as st
import pytest
from colmi_r02_client.client import Client
from colmi_r02_client import battery
MOCK_CHAR = Mock(spec=BleakGATTCharacteristic)
def test_handle_tx_short_packet():
@given(st.binary().filter(lambda x: len(x) != 16))
def test_handle_tx_short_packet(raw):
client = Client("unused")
with pytest.raises(AssertionError, match="Packet is the wrong length"):
client._handle_tx(MOCK_CHAR, bytearray())
client._handle_tx(MOCK_CHAR, bytearray(raw))
def test_handle_tx_error_bit():
@given(st.binary(min_size=16, max_size=16).filter(lambda x: x[0] >= 127))
def test_handle_tx_error_bit(raw):
client = Client("unused")
with pytest.raises(AssertionError, match="Packet has error bit"):
client._handle_tx(MOCK_CHAR, bytearray(b"\xf1" * 16))
client._handle_tx(MOCK_CHAR, bytearray(raw))
async def test_handle_tx_real_packet():
client = Client("unused")
packet = bytearray(b"\x03@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00C")
expected = battery.BatteryInfo(64, False)
client._handle_tx(MOCK_CHAR, packet)
result = await client.queues[battery.CMD_BATTERY].get()
assert result == expected
def test_handle_tx_none_parse(caplog):
caplog.set_level(logging.DEBUG)
client = Client("unused")
# set time packet response is ignored
packet = bytearray(b'\x01\x00\x01\x00"\x00\x00\x00\x00\x01\x000\x01\x00\x10f')
client._handle_tx(MOCK_CHAR, packet)
assert "No result returned from parser for 1" in caplog.text
def test_handle_tx_unexpected_packet(caplog):
client = Client("unused")
# 125 is currently unused
packet = bytearray(b"}\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00}")
client._handle_tx(MOCK_CHAR, packet)
assert "Did not expect this packet:" in caplog.text