File: test_backward_compatibility_11.py

package info (click to toggle)
liquidctl 1.16.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,452 kB
  • sloc: python: 15,304; sh: 712; xml: 84; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,610 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Test backward compatibility with liquidctl 1.1.x."""

import pytest

import usb

from liquidctl.driver.kraken2 import Kraken2
from liquidctl.driver.usb import hid, HidapiDevice


class _MockPyUsbHandle(usb.core.Device):
    def __init__(self, serial_number):
        self.idVendor = 0x1e71
        self.idProduct = 0x170e
        self._serial_number = serial_number

        class MockResourceManager():
            def dispose(self, *args, **kwargs):
                pass

        self._ctx = MockResourceManager()


def _mock_enumerate(vendor_id=0, product_id=0):
    return [
        {'vendor_id': vendor_id, 'product_id': product_id, 'serial_number': '_21', 'path': b'/_21'},
        {'vendor_id': vendor_id, 'product_id': product_id, 'serial_number': '_89', 'path': b'/_89'}
    ]


def test_construct_with_raw_pyusb_handle(monkeypatch):
    monkeypatch.setattr(hid, 'enumerate', _mock_enumerate)
    pyusb_handle = _MockPyUsbHandle(serial_number='_89')
    liquidctl_device = Kraken2(pyusb_handle, 'Some device')
    assert liquidctl_device.device.vendor_id == pyusb_handle.idVendor, \
        '<driver instance>.device points to incorrect physical device'
    assert liquidctl_device.device.product_id == pyusb_handle.idProduct, \
        '<driver instance>.device points to incorrect physical device'
    assert liquidctl_device.device.serial_number == pyusb_handle.serial_number, \
        '<driver instance>.device points to different physical unit'
    assert isinstance(liquidctl_device.device, HidapiDevice), \
        '<driver instance>.device not properly converted to HidapiDevice instance'