Skip to content

Commit aea18d4

Browse files
committed
Added fixtures and tests for webapi
1 parent 62de461 commit aea18d4

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

tests/api/test_routes.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
"""
3+
4+
from typing import Dict, Union
5+
6+
import pytest
7+
8+
9+
@pytest.fixture(scope="session")
10+
def route_variables() -> Dict[str, Union[int, str]]:
11+
return {
12+
"light_id": 0,
13+
"color": "purple",
14+
"speed": "fast",
15+
}
16+
17+
18+
def test_route_root(busylight_client) -> None:
19+
20+
with busylight_client as client:
21+
response = client.get("/")
22+
23+
assert response.status_code == 200
24+
25+
routes = response.json()
26+
27+
assert isinstance(routes, list)
28+
29+
for route in routes:
30+
assert isinstance(route, dict)
31+
assert "path" in route.keys()
32+
assert all(isinstance(value, str) for value in route.values())
33+
34+
35+
@pytest.mark.parametrize(
36+
"route, expected_status_codes",
37+
[
38+
["/light/{light_id}/status", [404]],
39+
["/lights/status", [200]],
40+
["/light/{light_id}/on", [404]],
41+
["/light/{light_id}/on/{color}", [404]],
42+
["/lights/on", [200]],
43+
["/lights/on/{color}", [200]],
44+
["/light/{light_id}/off", [200, 404]],
45+
["/lights/off", [200]],
46+
["/light/{light_id}/blink", [200, 404]],
47+
["/light/{light_id}/blink/{color}", [200, 404]],
48+
["/light/{light_id}/blink/{color}/{speed}", [200, 404]],
49+
["/lights/blink", [200]],
50+
["/lights/blink/{color}", [200]],
51+
["/lights/blink/{color}/{speed}", [200]],
52+
["/light/{light_id}/rainbow", [200, 404]],
53+
["/lights/rainbow", [200]],
54+
["/light/{light_id}/fli", [200, 404]],
55+
["/lights/fli", [200]],
56+
["/light/{light_id}/pulse", [200, 404]],
57+
["/light/{light_id}/pulse/{color}", [200, 404]],
58+
["/lights/pulse", [200]],
59+
["/lights/pulse/{color}", [200]],
60+
],
61+
)
62+
def test_all_routes(
63+
route,
64+
expected_status_codes,
65+
busylight_client,
66+
route_variables,
67+
) -> None:
68+
69+
with busylight_client as client:
70+
response = client.get(route.format_map(route_variables))
71+
assert response.status_code in expected_status_codes
72+
73+
74+
@pytest.mark.parametrize(
75+
"color, expected_status_code",
76+
[
77+
("red", 200),
78+
("#ff0000", 200),
79+
("#f00", 200),
80+
("0xff0000", 200),
81+
("0xf00", 200),
82+
("reddish", 404),
83+
],
84+
)
85+
def test_route_lights_on_color_validation(
86+
color,
87+
expected_status_code,
88+
busylight_client,
89+
) -> None:
90+
91+
with busylight_client as client:
92+
for value in [color.lower(), color.upper(), color.title()]:
93+
response = client.get(f"/lights/on/{value}")
94+
assert response.status_code == expected_status_code
95+
96+
97+
@pytest.mark.parametrize(
98+
"speed, expected_status_code",
99+
[
100+
("slow", 200),
101+
("medium", 200),
102+
("fast", 200),
103+
("slowish", 422),
104+
],
105+
)
106+
def test_route_lights_blink_speed_validataion(
107+
speed, expected_status_code, busylight_client
108+
) -> None:
109+
110+
with busylight_client as client:
111+
response = client.get(f"/lights/blink/orange/{speed}")
112+
assert response.status_code == expected_status_code

tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
"""
22
"""
33

4+
from typing import List
5+
46
import pytest
57

68
from unittest import mock
79

10+
from fastapi.testclient import TestClient
11+
812
from busylight.lights import USBLight
13+
from busylight.api import busylightapi
914

1015

1116
@pytest.fixture(scope="session")
@@ -52,3 +57,17 @@ def send_feature_report(self, data):
5257
"release_number": 0x200,
5358
}
5459
return lights
60+
61+
62+
@pytest.fixture(scope="session")
63+
def busylight_client() -> TestClient:
64+
return TestClient(busylightapi)
65+
66+
67+
@pytest.fixture(scope="session")
68+
def busylight_api_routes(busylight_client) -> List[str]:
69+
70+
with busylight_client as client:
71+
response = client.get("/")
72+
73+
return [r["path"] for r in response.json()]

tests/unit/test_usblight.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def test_supported_lights():
2323
supported_lights = USBLight.supported_lights()
2424

2525
assert isinstance(supported_lights, list)
26+
2627
for light in supported_lights:
2728
assert issubclass(light, USBLight)
2829

@@ -31,6 +32,10 @@ def test_usblight_first_light():
3132
"""The first_light() classmethod returns a configured
3233
light or raises USBLightNotFound when no more lights
3334
can be located.
35+
36+
For this test, we keep references to each light in a
37+
list to keep them from being released and re-found by
38+
the `first_light` method.
3439
"""
3540
with pytest.raises(USBLightNotFound):
3641
lights = []

0 commit comments

Comments
 (0)