From be2c0bdb80fc0b3429cd46dcfef444ff48bd4a0c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Dec 2023 15:33:21 -1000 Subject: [PATCH] chore: add basic cover --- tests/test_init.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_main.py | 33 ---------------- 2 files changed, 97 insertions(+), 33 deletions(-) create mode 100644 tests/test_init.py delete mode 100644 tests/test_main.py diff --git a/tests/test_init.py b/tests/test_init.py new file mode 100644 index 0000000..8605691 --- /dev/null +++ b/tests/test_init.py @@ -0,0 +1,97 @@ +from typing import Any +from unittest.mock import ANY + +from bleak.backends.device import BLEDevice +from bleak.backends.scanner import AdvertisementData + +from habluetooth import BaseHaRemoteScanner, BaseHaScanner, HaBluetoothConnector + + +class MockBleakClient: + pass + + +def test_create_scanner(): + connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) + + class MockScanner(BaseHaScanner): + pass + + def discovered_devices_and_advertisement_data(self): + return [] + + def discovered_devices(self): + return [] + + scanner = MockScanner("any", "any", connector) + assert isinstance(scanner, BaseHaScanner) + + +def test_create_remote_scanner(): + connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) + + def callback(data: Any) -> None: + pass + + scanner = BaseHaRemoteScanner("any", "any", callback, connector, True) + assert isinstance(scanner, BaseHaRemoteScanner) + + +def test__async_on_advertisement(): + connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) + + def callback(data: Any) -> None: + pass + + scanner = BaseHaRemoteScanner("any", "any", callback, connector, True) + details = scanner._details | {} + scanner._async_on_advertisement( + "AA:BB:CC:DD:EE:FF", + -88, + "name", + ["service_uuid"], + {"service_uuid": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, + {32: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, + -88, + details, + 1.0, + ) + scanner._async_on_advertisement( + "AA:BB:CC:DD:EE:FF", + -21, + "name", + ["service_uuid2"], + {"service_uuid2": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, + {21: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"}, + -88, + details, + 1.0, + ) + ble_device = BLEDevice( + "AA:BB:CC:DD:EE:FF", + "name", + details, + -21, + ) + first_device = scanner.discovered_devices[0] + assert first_device.address == ble_device.address + assert first_device.details == ble_device.details + assert first_device.name == ble_device.name + assert first_device.rssi == ble_device.rssi + assert "AA:BB:CC:DD:EE:FF" in scanner.discovered_devices_and_advertisement_data + adv = scanner.discovered_devices_and_advertisement_data["AA:BB:CC:DD:EE:FF"][1] + assert adv == AdvertisementData( + local_name="name", + manufacturer_data={ + 32: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b", + 21: b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b", + }, + service_data={ + "service_uuid": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b", + "service_uuid2": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b", + }, + service_uuids=ANY, + tx_power=-88, + rssi=-21, + platform_data=(), + ) diff --git a/tests/test_main.py b/tests/test_main.py deleted file mode 100644 index f353fe8..0000000 --- a/tests/test_main.py +++ /dev/null @@ -1,33 +0,0 @@ -from typing import Any - -from habluetooth import BaseHaRemoteScanner, BaseHaScanner, HaBluetoothConnector - - -class MockBleakClient: - pass - - -def test_create_scanner(): - connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) - - class MockScanner(BaseHaScanner): - pass - - def discovered_devices_and_advertisement_data(self): - return [] - - def discovered_devices(self): - return [] - - scanner = MockScanner("any", "any", connector) - assert isinstance(scanner, BaseHaScanner) - - -def test_create_remote_scanner(): - connector = HaBluetoothConnector(MockBleakClient, "any", lambda: True) - - def callback(data: Any) -> None: - pass - - scanner = BaseHaRemoteScanner("any", "any", callback, connector, True) - assert isinstance(scanner, BaseHaRemoteScanner)