Skip to content

Commit faa9d6e

Browse files
committed
Improve tests
Signed-off-by: Nicolai Buchwitz <nb@tipi-net.de>
1 parent 35aec6c commit faa9d6e

4 files changed

Lines changed: 73 additions & 12 deletions

File tree

tests/helper.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
parentdir = os.path.dirname(currentdir)
99
sys.path.append(parentdir)
1010

11-
from uhubctl import *
11+
import uhubctl
1212

13-
class MockHub(Hub):
13+
class MockHub(uhubctl.Hub):
1414
def __init__(self, path: str, num_ports: int = 5) -> None:
1515
self.path = path
1616
self.num_ports = num_ports
@@ -30,7 +30,8 @@ def __stdout(self, prefix: str = "Current", port_filter: int = None):
3030
if port_filter is not None and idx+1 != int(port_filter):
3131
continue
3232

33-
stdout.append(f" Port {idx+1}: 0100 {self.__power_status(idx)}".encode())
33+
stdout.append(
34+
f" Port {idx+1}: 0100 {self.__power_status(idx)}".encode())
3435

3536
return stdout
3637

@@ -53,7 +54,7 @@ def cmd(self, fp: pytest_subprocess.FakeProcess, port_number: int = None, new_st
5354
stdout = self.__stdout(port_filter=port_number)
5455

5556
if port_number is not None:
56-
cmd += [ "-p", str(port_number)]
57+
cmd += ["-p", str(port_number)]
5758

5859
if new_status is not None:
5960
self.status[port_number] = new_status
@@ -66,10 +67,11 @@ def cmd(self, fp: pytest_subprocess.FakeProcess, port_number: int = None, new_st
6667

6768
fp.register(cmd, stdout=stdout)
6869

70+
6971
@pytest.fixture
7072
def mock_hub():
7173
path = "1-2"
7274
num_ports = 5
7375
hub = MockHub(path, num_ports)
7476

75-
return hub
77+
return hub

tests/test_hub.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@
33

44
import helper
55
from helper import MockHub, mock_hub
6+
7+
import uhubctl
68
from uhubctl import Hub
79

10+
811
def test_empty_hub():
912
hub_path = "1-2"
1013
hub = Hub(hub_path, enumerate_ports=False)
1114

1215
assert hub.path == hub_path
1316
assert hub.ports == []
1417

18+
19+
def test_wrong_or_missing_parameter():
20+
with pytest.raises(TypeError):
21+
Hub()
22+
23+
with pytest.raises(TypeError):
24+
Hub(1)
25+
26+
1527
def test_multiple_manual_ports():
1628
hub_path = "1-2"
1729
hub = Hub(hub_path, enumerate_ports=False)
@@ -20,14 +32,37 @@ def test_multiple_manual_ports():
2032

2133
assert len(hub.ports) == 1
2234

23-
hub.add_ports(2,10)
35+
hub.add_ports(2, 10)
2436

2537
assert len(hub.ports) == 10
2638

2739

40+
def test_str():
41+
hub_path = "1-2"
42+
hub = Hub(hub_path, enumerate_ports=False)
43+
44+
assert str(hub) == f"USB Hub {hub_path}"
45+
2846

2947
def test_port_enumeration(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess):
3048
mock_hub.cmd(fp)
3149
mock_hub.discover_ports()
3250

3351
assert len(mock_hub.ports) == 5
52+
53+
54+
def test_no_devices(fp: pytest_subprocess.FakeProcess):
55+
fp.register(["uhubctl", "-N"],
56+
stdout=["No compatible devices detected!".encode()])
57+
58+
hubs = uhubctl.discover_hubs()
59+
60+
assert hubs == []
61+
62+
63+
def test_find_port(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess):
64+
mock_hub.cmd(fp)
65+
mock_hub.discover_ports()
66+
67+
assert mock_hub.find_port(1) is not None
68+
assert mock_hub.find_port(10) is None

tests/test_port.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from random import randint
12
import pytest
23
import pytest_subprocess
34

@@ -6,16 +7,39 @@
67
from uhubctl import Hub, Port
78

89

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)
1213

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)
1525

1626
assert port.port_number == port_number
1727

1828

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+
1943
def test_port_status(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess):
2044
for port in mock_hub.ports:
2145
mock_hub.cmd(fp, port.port_number)

tests/test_uhubctl_binary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ def test_uhubctl_binary_echo():
1515
import uhubctl
1616
uhubctl.utils.UHUBCTL_BINARY = "/bin/echo"
1717

18-
assert uhubctl.discover_hubs() == []
18+
assert uhubctl.discover_hubs() == []

0 commit comments

Comments
 (0)