|
| 1 | +"""Test blynclight.hid.HidDevice |
| 2 | + |
| 3 | +These tests do not depend on the existence of a particular |
| 4 | +USB device but it does require *some* USB devices to act |
| 5 | +upon. |
| 6 | + |
| 7 | +These tests focus on enumerating, opening, and closing |
| 8 | +devices. The read and write functions are not tested |
| 9 | +since that requires some specific knowledge about the |
| 10 | +device we are attempting to perform I/O on. |
| 11 | + |
| 12 | +""" |
| 13 | + |
| 14 | +import pytest |
| 15 | +from blynclight.hid import HidDevice |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope="session") |
| 19 | +def devices(): |
| 20 | + """List of dictionaries fixture returned from the class method |
| 21 | + blynclight.hid.HidDevice.enumerate(). The enumerate method makes |
| 22 | + no guarantees about device ordering between invocations, so we use |
| 23 | + session scope for this fixture to keep repeated uses consistent |
| 24 | + between tests. |
| 25 | + |
| 26 | + """ |
| 27 | + return HidDevice.enumerate() |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="session") |
| 31 | +def hid_device(devices): |
| 32 | + """A blynclight.hid.HidDevice fixture that represents the last device |
| 33 | + in the 'devices' fixture. Tests often use the first device in |
| 34 | + devices so using the last device avoids attempting to open a |
| 35 | + device that is already open while still making a HidDevice |
| 36 | + available. This fixture has session scope. |
| 37 | + |
| 38 | + """ |
| 39 | + device = HidDevice.from_dict(devices[-1]) |
| 40 | + yield device |
| 41 | + |
| 42 | + |
| 43 | +def test_classmethod_enumerate(): |
| 44 | + """Ensure that the output of blynclight.hid.HidDevice.enumerate |
| 45 | + returns a list of dictionaries and spot checks the dictionary |
| 46 | + contents for the keys 'vendor_id' and 'product_id'. |
| 47 | + |
| 48 | + """ |
| 49 | + devices = HidDevice.enumerate() |
| 50 | + assert devices |
| 51 | + assert isinstance(devices, list) |
| 52 | + |
| 53 | + for device in devices: |
| 54 | + assert device |
| 55 | + assert isinstance(device, dict) |
| 56 | + assert "vendor_id" in device.keys() |
| 57 | + assert "product_id" in device.keys() |
| 58 | + |
| 59 | + |
| 60 | +def test_classmethod_from_dict_good_data(devices): |
| 61 | + """:param devices: list of dictionaries fixture |
| 62 | + |
| 63 | + Tests the blynclight.hid.HidDevice.from_dict convenience method |
| 64 | + using presumably good device information dictionaries sourced from |
| 65 | + the enumerate class method. |
| 66 | + |
| 67 | + """ |
| 68 | + try: |
| 69 | + device = HidDevice.from_dict(devices[0]) |
| 70 | + assert isinstance(device, HidDevice) |
| 71 | + assert device.vendor_id == devices[0]["vendor_id"] |
| 72 | + assert device.product_id == devices[0]["product_id"] |
| 73 | + del (device) |
| 74 | + except KeyError: |
| 75 | + pass |
| 76 | + |
| 77 | + |
| 78 | +def test_classmethod_from_dict_bad_data(): |
| 79 | + """Tests the blynclight.hid.HidDevice.from_dict convenience method |
| 80 | + using synthetic bad data, specifically: |
| 81 | + |
| 82 | + - an empty dictionary |
| 83 | + - a dictionary with only a vendor_id key |
| 84 | + - a dictionary with only a product_id key |
| 85 | + |
| 86 | + """ |
| 87 | + |
| 88 | + with pytest.raises(LookupError): |
| 89 | + HidDevice.from_dict({}) |
| 90 | + |
| 91 | + with pytest.raises(LookupError): |
| 92 | + HidDevice.from_dict({"vendor_id": None}) |
| 93 | + |
| 94 | + with pytest.raises(LookupError): |
| 95 | + HidDevice.from_dict({"product_id": None}) |
| 96 | + |
| 97 | + |
| 98 | +def test_hiddevice_init(devices): |
| 99 | + """:param devices: list of dictionaries fixture |
| 100 | + |
| 101 | + Tests creating a blynclight.hid.HidDevice and ensures that the |
| 102 | + HidDevice is configured with the specified vendor_id and |
| 103 | + product_id. |
| 104 | + |
| 105 | + """ |
| 106 | + |
| 107 | + device = HidDevice(devices[0]["vendor_id"], devices[0]["product_id"]) |
| 108 | + assert isinstance(device, HidDevice) |
| 109 | + assert device.vendor_id == devices[0]["vendor_id"] |
| 110 | + assert device.product_id == devices[0]["product_id"] |
| 111 | + |
| 112 | + |
| 113 | +def test_hiddevice_open_twice(devices): |
| 114 | + """:param devices: list of dictionaries fixture |
| 115 | + |
| 116 | + The hidapi library does not allow a USB device to be opened twice |
| 117 | + in the same process, so we test for that by opening the first |
| 118 | + device twice. |
| 119 | + |
| 120 | + """ |
| 121 | + |
| 122 | + device = HidDevice.from_dict(devices[0]) |
| 123 | + |
| 124 | + with pytest.raises(ValueError): |
| 125 | + HidDevice(device.vendor_id, device.product_id) |
0 commit comments