|
| 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 |
0 commit comments