Exemple #1
0
 def test_write_handle_timeout(self, time_mock, popen_mock, os_mock):
     """Test notification when timeout"""
     _configure_popenmock_timeout(popen_mock, "Characteristic")
     backend = GatttoolBackend()
     backend.connect(TEST_MAC)
     with self.assertRaises(BluetoothBackendException):
         backend.write_handle(0xFF, b"\x00\x10\xFF")
Exemple #2
0
 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, "")
     backend = GatttoolBackend()
     backend.connect(TEST_MAC)
     with self.assertRaises(BluetoothBackendException):
         backend.write_handle(0xFF, b"\x00\x10\xFF")
Exemple #3
0
 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")
     backend = GatttoolBackend()
     backend.connect(TEST_MAC)
     with self.assertRaises(BluetoothBackendException):
         backend.write_handle(0xFF, b'\x00\x10\xFF')
Exemple #4
0
 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')
     backend = GatttoolBackend()
     backend.connect(TEST_MAC)
     self.assertTrue(backend.write_handle(0xFF, b'\x00\x10\xFF'))
Exemple #5
0
class TestGatttoolBackend(unittest.TestCase):
    """Test GatttoolBackend with real sensor."""

    # pylint does not understand pytest fixtures, so we have to disable the warning
    # pylint: disable=no-member

    def setUp(self):
        """Setup of the test case."""
        self.backend = GatttoolBackend(retries=0, timeout=20)

    @pytest.mark.usefixtures("mac")
    def test_read(self):
        """Test reading a handle from the sensor."""
        self.backend.connect(self.mac)
        result = self.backend.read_handle(HANDLE_READ_NAME)
        self.assertIsNotNone(result)
        self.backend.disconnect()

    @pytest.mark.usefixtures("mac")
    def test_write(self):
        """Test writing data to handle of the sensor."""
        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):
        """Test error handling if not connected."""
        with self.assertRaises(BluetoothBackendException):
            self.backend.read_handle(HANDLE_READ_NAME)

    def test_check_backend(self):
        """Test check_backend function."""
        self.assertTrue(self.backend.check_backend())

    def test_invalid_mac_exception(self):
        """Test writing data to handle of the sensor."""
        with self.assertRaises(BluetoothBackendException):
            self.backend.connect(TEST_MAC)
            self.backend.read_handle(HANDLE_READ_NAME)
Exemple #6
0
 def test_write_not_connected(self):
     """Test writing data when not connected."""
     backend = GatttoolBackend()
     with self.assertRaises(BluetoothBackendException):
         backend.write_handle(0xFF, [0x00])