|
| 1 | +from random import randint |
1 | 2 | import pytest |
2 | 3 | import pytest_subprocess |
3 | 4 |
|
|
6 | 7 | from uhubctl import Hub, Port |
7 | 8 |
|
8 | 9 |
|
9 | | -def test_manual_port_creation(): |
10 | | - port_number = 1 |
11 | | - hub_path = "1" |
| 10 | +@pytest.fixture |
| 11 | +def demo_hub(): |
| 12 | + return Hub("1", enumerate_ports=False) |
12 | 13 |
|
13 | | - hub = Hub(hub_path, enumerate_ports=False) |
14 | | - port = Port(hub, port_number) |
| 14 | + |
| 15 | +@pytest.mark.parametrize("port_number", [randint(1, 20)]) |
| 16 | +def test_str(demo_hub: Hub, port_number: int): |
| 17 | + port = Port(demo_hub, port_number) |
| 18 | + |
| 19 | + assert str(port) == f"USB Port {demo_hub.path}.{port_number}" |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("port_number", [randint(1, 20)]) |
| 23 | +def test_manual_port_creation(demo_hub: Hub, port_number: int): |
| 24 | + port = Port(demo_hub, port_number) |
15 | 25 |
|
16 | 26 | assert port.port_number == port_number |
17 | 27 |
|
18 | 28 |
|
| 29 | +def test_wrong_or_missing_parameter(demo_hub: Hub): |
| 30 | + with pytest.raises(TypeError): |
| 31 | + Port() |
| 32 | + |
| 33 | + with pytest.raises(TypeError): |
| 34 | + Port(demo_hub) |
| 35 | + |
| 36 | + port = Port(demo_hub, "1") |
| 37 | + assert port.port_number == 1 |
| 38 | + |
| 39 | + with pytest.raises(ValueError): |
| 40 | + Port(demo_hub, "a") |
| 41 | + |
| 42 | + |
19 | 43 | def test_port_status(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess): |
20 | 44 | for port in mock_hub.ports: |
21 | 45 | mock_hub.cmd(fp, port.port_number) |
|
0 commit comments