From dff312534fe277590fd1e82538559897c6252fc7 Mon Sep 17 00:00:00 2001 From: MDE <59321583+mdevel1@users.noreply.github.com> Date: Wed, 29 Jan 2020 07:33:53 +0100 Subject: [PATCH] Added test and fix for device matching (#46) * Added test for device matching Mocking the scanning and testing the connect() of each backend would be better, but that requires more refactoring with prior agreement. Added unittest2 dependency for subTest support, other solutions are available. * Fixed matching for Move Hub If other BLE devices are around, an exception occurs on hub_mac.lower() if default hub_mac (None) is used. * fixup! Added test for device matching Removed unittest2 dependency and features --- pylgbst/comms/__init__.py | 13 ++++++++++--- tests/test_comms.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/test_comms.py diff --git a/pylgbst/comms/__init__.py b/pylgbst/comms/__init__.py index 6c1d4a9..8f88b88 100644 --- a/pylgbst/comms/__init__.py +++ b/pylgbst/comms/__init__.py @@ -48,11 +48,18 @@ class Connection(object): def _is_device_matched(self, address, name, hub_mac): log.debug("Checking device name: %s, MAC: %s", name, address) + matched = False if address != "00:00:00:00:00:00": - if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac.lower() == address.lower(): + if hub_mac: + if hub_mac.lower() == address.lower(): + matched = True + elif name == LEGO_MOVE_HUB: + matched = True + + if matched: log.info("Found %s at %s", name, address) - return True - return False + + return matched class DebugServer(object): diff --git a/tests/test_comms.py b/tests/test_comms.py new file mode 100644 index 0000000..7f2eeb1 --- /dev/null +++ b/tests/test_comms.py @@ -0,0 +1,38 @@ +import unittest + +from pylgbst.comms import * + +class ConnectionTestCase(unittest.TestCase): + def test_is_device_matched(self): + conn = Connection() + + hub_address = '1a:2A:3A:4A:5A:6A' + other_address = 'A1:a2:a3:a4:a5:a6' + zero_address = '00:00:00:00:00:00' + hub_name = LEGO_MOVE_HUB + other_name = 'HRM' + + test_matrix = [ + # address, name, hub_mac, expected + (hub_address, hub_name, hub_address, True), + (hub_address, hub_name, None, True), + (hub_address, None, hub_address, True), + (hub_address, None, None, False), + (hub_address, other_name, hub_address, True), + (hub_address, other_name, None, False), + (other_address, hub_name, hub_address, False), + (other_address, hub_name, None, True), + (other_address, None, hub_address, False), + (other_address, None, None, False), + (other_address, other_name, hub_address, False), + (other_address, other_name, None, False), + (zero_address, hub_name, hub_address, False), + (zero_address, hub_name, None, False), + (zero_address, None, hub_address, False), + (zero_address, None, None, False), + (zero_address, other_name, hub_address, False), + (zero_address, other_name, None, False), + ] + + for address, name, hub_mac, expected in test_matrix: + self.assertEqual(conn._is_device_matched(address=address, name=name, hub_mac=hub_mac), expected)