<commit_before>"""Automated testing of GATT functionality using dbusmock."""

import subprocess
import sys
import unittest

import dbus
import dbusmock

from bluezero import GATT


class TestBluezeroService(dbusmock.DBusTestCase):
    """Test class to exercise (remote) GATT Service Features."""

    @classmethod
    def setUpClass(klass):
        """Initiate mock Bluez on a mock dbus."""
        klass.start_system_bus()
        klass.dbus_con = klass.get_dbus(True)
        (klass.p_mock, klass.obj_bluez) = klass.spawn_server_template(
            'bluez5', {}, stdout=subprocess.PIPE)

    def setUp(self):
        """Initialise the class for the tests."""
        self.obj_bluez.Reset()
        self.dbusmock = dbus.Interface(self.obj_bluez, dbusmock.MOCK_IFACE)
        self.dbusmock_bluez = dbus.Interface(self.obj_bluez, 'org.bluez.Mock')

    def test_service_uuid(self):
        """Test the service UUID."""
        adapter_name = 'hci0'
        address = '11:22:33:44:55:66'
        alias = 'Peripheral Device'

        # Initialise an adapter and ensure that it's on the right path
        path = self.dbusmock_bluez.AddAdapter(adapter_name, 'my-computer')
        self.assertEqual(path, '/org/bluez/' + adapter_name)

        # Add a mock remote device
        self.dbusmock_bluez.AddDevice(adapter_name, address, alias)

        # Initialise a mock remote GATT and ensure it's on the right path
        path = self.dbusmock_bluez.AddGATT(adapter_name, address, alias)
        srvc_dbus_path = '/org/bluez/hci0/dev_11_22_33_44_55_66/service0001'

        # Invoke the bluez GATT library to access the mock GATT service
        test_service = GATT.Service(srvc_dbus_path)

        # Test for the UUID
        found_name = test_service.UUID
        self.assertEqual(found_name, '180F')


if __name__ == '__main__':
    # avoid writing to stderr
    unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout,
                                                     verbosity=2))<commit_msg>Incorporate basic tests for all the Local GATT properties. (#93)<commit_after>"""Automated testing of GATT functionality using dbusmock."""

import subprocess
import sys
import unittest

import dbus
import dbusmock

from bluezero import GATT


class TestBluezeroService(dbusmock.DBusTestCase):
    """Test class to exercise (remote) GATT Service Features."""

    @classmethod
    def setUpClass(klass):
        """Initiate mock Bluez on a mock dbus."""
        klass.start_system_bus()
        klass.dbus_con = klass.get_dbus(True)
        (klass.p_mock, klass.obj_bluez) = klass.spawn_server_template(
            'bluez5', {}, stdout=subprocess.PIPE)

    def setUp(self):
        """Initialise the class for the tests."""
        self.obj_bluez.Reset()
        self.dbusmock = dbus.Interface(self.obj_bluez, dbusmock.MOCK_IFACE)
        self.dbusmock_bluez = dbus.Interface(self.obj_bluez, 'org.bluez.Mock')

        self.adapter_name = 'hci0'
        self.address = '11:22:33:44:55:66'
        self.alias = 'Peripheral Device'

        # Initialise an adapter
        self.ad_dpath = self.dbusmock_bluez.AddAdapter(self.adapter_name,
                                                       'my-computer')

        # Add a mock remote device
        self.dbusmock_bluez.AddDevice(self.adapter_name, self.address,
                                      self.alias)

        # Initialise a mock remote GATT device
        self.svc_dpath = self.dbusmock_bluez.AddGATT(self.adapter_name,
                                                     self.address, self.alias)

    def test_paths(self):
        """Test the adapter and service paths."""
        # Test for the adapter path
        self.assertEqual(self.ad_dpath, '/org/bluez/' + self.adapter_name)

        # Test for the service path
        srvc_dbus_path = '/org/bluez/hci0/dev_11_22_33_44_55_66/service0001'
        self.assertEqual(self.svc_dpath, srvc_dbus_path)

    def test_service_uuid(self):
        """Test the service UUID."""
        # Invoke the bluez GATT library to access the mock GATT service
        test_service = GATT.Service(self.svc_dpath)

        # Test for the UUID
        self.assertEqual(test_service.UUID, '180F')

    def test_service_device(self):
        """Test the service device path."""
        # Invoke the bluez GATT library to access the mock GATT service
        test_service = GATT.Service(self.svc_dpath)

        # Test for the device path
        dev_underscore = self.address.replace(':', '_').upper()
        dev_addr = '{0}/dev_{1}'.format(self.ad_dpath, dev_underscore)
        self.assertEqual(test_service.device, dev_addr)

    def test_service_primary(self):
        """Test the service primary flag."""
        # Invoke the bluez GATT library to access the mock GATT service
        test_service = GATT.Service(self.svc_dpath)

        # Test for the UUID
        self.assertEqual(test_service.primary, True)


if __name__ == '__main__':
    # avoid writing to stderr
    unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout,
                                                     verbosity=2))
{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.01444184231069477, 'mean_word_length': 2.536922015182885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4849339', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1548, 'n_words': 371}