def test_write_handle_ok(self, time_mock, popen_mock): """Test writing to a handle successfully.""" _configure_popenmock(popen_mock, 'Characteristic value was written successfully') be = GatttoolBackend() be.connect(TEST_MAC) self.assertTrue(be.write_handle(0xFF, b'\X00\X10\XFF'))
def test_read_handle_empty_output(self, time_mock, popen_mock): """Test reading handle where no result is returned.""" _configure_popenmock(popen_mock, '') be = GatttoolBackend() be.connect(TEST_MAC) result = be.read_handle(0xFF) self.assertIsNone(result)
def test_read_handle_ok(self, popen_mock): """Test reading handle successfully.""" gattoutput = [0x00, 0x11, 0xAA, 0xFF] _configure_popenmock(popen_mock, 'Characteristic value/descriptor: 00 11 AA FF') be = GatttoolBackend() be.connect(TEST_MAC) result = be.read_handle(0xFF) self.assertEqual(gattoutput, result)
def test_run_connect_disconnect(self): """Just run connect and disconnect""" be = GatttoolBackend() be.connect(TEST_MAC) self.assertEqual(TEST_MAC, be._mac) be.disconnect() self.assertEqual(None, be._mac)
def test_write_handle_wrong_handle(self, time_mock, popen_mock): """Test writing to a non-writable handle.""" _configure_popenmock( popen_mock, "Characteristic Write Request failed: Attribute can't be written") be = GatttoolBackend() be.connect(TEST_MAC) with self.assertRaises(ValueError): be.write_handle(0xFF, b'\X00\X10\XFF')
def test_read_handle_wrong_handle(self, popen_mock): """Test reading invalid handle.""" _configure_popenmock( popen_mock, 'Characteristic value/descriptor read failed: Invalid handle') be = GatttoolBackend() be.connect(TEST_MAC) with self.assertRaises(ValueError): be.read_handle(0xFF)
def setUp(self): self.backend = GatttoolBackend(retries=0, timeout=20)
class TestGatttoolBackend(unittest.TestCase): def setUp(self): self.backend = GatttoolBackend(retries=0, timeout=20) @pytest.mark.usefixtures("mac") def test_read(self): self.backend.connect(self.mac) result = self.backend.read_handle(0) self.assertIsNotNone(result) self.backend.disconnect() @pytest.mark.usefixtures("mac") def test_write(self): self.backend.connect(self.mac) result = self.backend.write_handle(HANDLE_WRITE_MODE_CHANGE, DATA_MODE_CHANGE) self.assertIsNotNone(result) self.backend.disconnect() def test_read_not_connected(self): try: self.backend.read_handle(0) self.fail('should have thrown an exception') except ValueError: pass except BluetoothBackendException: pass def test_check_backend(self): self.backend.check_backend() def test_parse_bytes(self): result = """ value: 01 02 03 04 value: 05 06 07 08 value: 09 """ bytes = self.backend.parseBytes(result) self.assertEqual(bytes[0], 5)
def test_check_backend_ok(self, call_mock): """Test check_backend successfully.""" be = GatttoolBackend() be.check_backend()
def test_write_handle_no_answer(self, time_mock, popen_mock): """Test writing to a handle when no result is returned.""" _configure_popenmock(popen_mock, '') be = GatttoolBackend() be.connect(TEST_MAC) self.assertFalse(be.write_handle(0xFF, b'\X00\X10\XFF'))
def test_write_not_connected(self): """Test writing data when not connected.""" be = GatttoolBackend() with self.assertRaises(ValueError): be.write_handle(0xFF, [0x00])
def test_read_not_connected(self): """Test reading data when not connected.""" be = GatttoolBackend() with self.assertRaises(ValueError): be.read_handle(0xFF)
def test_check_backend_fail(self, call_mock): """Test check_backend with IOError being risen.""" be = GatttoolBackend() with self.assertRaises(BluetoothBackendException): be.check_backend()
def test_byte_to_handle(self): """Test conversion of handles.""" self.assertEqual('0x0B', GatttoolBackend.byte_to_handle(0x0B)) self.assertEqual('0xAF', GatttoolBackend.byte_to_handle(0xAF)) self.assertEqual('0xAABB', GatttoolBackend.byte_to_handle(0xAABB))
def test_bytes_to_string(self): """Test conversion of byte arrays.""" self.assertEqual('0A0B', GatttoolBackend.bytes_to_string(bytes([0x0A, 0x0B]))) self.assertEqual('0x0C0D', GatttoolBackend.bytes_to_string(bytes([0x0C, 0x0D]), True))