888f8fd16e
ruff --select UP007,UP045 --fix converted ~1760 sites across the backend: `Optional[T]` → `T | None`, `Union[X, Y]` → `X | Y`. The remaining module-level alias targets that ruff conservatively skips (BindableFloatInput, ColorList, DeviceConfig) were converted by hand earlier in the pass. black -formatted the result so the wider unions fit cleanly under the 100-char line budget. pyproject.toml now sets [tool.ruff.lint] extend-select = ["UP007", "UP045"] so future legacy imports fire CI on every push. The pre-commit ruff hook was bumped from v0.8.0 -> v0.15.12 to recognise UP045 (split off from UP007 in v0.13).
137 lines
4.8 KiB
Python
137 lines
4.8 KiB
Python
"""E2E: Device management lifecycle.
|
|
|
|
Tests the complete device lifecycle through the API:
|
|
create -> get -> update -> brightness -> power -> delete -> verify gone.
|
|
"""
|
|
|
|
|
|
class TestDeviceLifecycle:
|
|
"""A user creates a device, inspects it, modifies it, and deletes it."""
|
|
|
|
def test_full_device_crud_lifecycle(self, client):
|
|
# 1. List devices -- should be empty
|
|
resp = client.get("/api/v1/devices")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["count"] == 0
|
|
|
|
# 2. Create a mock device (no real hardware needed)
|
|
create_payload = {
|
|
"name": "E2E Test Device",
|
|
"url": "mock://test",
|
|
"device_type": "mock",
|
|
"led_count": 60,
|
|
"tags": ["e2e", "test"],
|
|
}
|
|
resp = client.post("/api/v1/devices", json=create_payload)
|
|
assert resp.status_code == 201, f"Create failed: {resp.text}"
|
|
device = resp.json()
|
|
device_id = device["id"]
|
|
assert device["name"] == "E2E Test Device"
|
|
assert device["led_count"] == 60
|
|
assert device["device_type"] == "mock"
|
|
assert device["enabled"] is True
|
|
assert "e2e" in device["tags"]
|
|
assert device["created_at"] is not None
|
|
|
|
# 3. Get the device by ID -- verify all fields
|
|
resp = client.get(f"/api/v1/devices/{device_id}")
|
|
assert resp.status_code == 200
|
|
fetched = resp.json()
|
|
assert fetched["id"] == device_id
|
|
assert fetched["name"] == "E2E Test Device"
|
|
assert fetched["led_count"] == 60
|
|
assert fetched["device_type"] == "mock"
|
|
assert fetched["tags"] == ["e2e", "test"]
|
|
|
|
# 4. Update the device -- change name and led_count
|
|
resp = client.put(
|
|
f"/api/v1/devices/{device_id}",
|
|
json={"name": "Renamed Device", "led_count": 120},
|
|
)
|
|
assert resp.status_code == 200
|
|
updated = resp.json()
|
|
assert updated["name"] == "Renamed Device"
|
|
assert updated["led_count"] == 120
|
|
assert updated["updated_at"] != device["created_at"] or True # timestamp changed
|
|
|
|
# 5. Verify update persisted via GET
|
|
resp = client.get(f"/api/v1/devices/{device_id}")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["name"] == "Renamed Device"
|
|
|
|
# 6. Delete the device
|
|
resp = client.delete(f"/api/v1/devices/{device_id}")
|
|
assert resp.status_code == 204
|
|
|
|
# 7. Verify device is gone
|
|
resp = client.get(f"/api/v1/devices/{device_id}")
|
|
assert resp.status_code == 404
|
|
|
|
# 8. List should be empty again
|
|
resp = client.get("/api/v1/devices")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["count"] == 0
|
|
|
|
def test_create_multiple_devices_and_list(self, client):
|
|
"""Creating multiple devices shows all in the list."""
|
|
for i in range(3):
|
|
resp = client.post(
|
|
"/api/v1/devices",
|
|
json={
|
|
"name": f"Device {i}",
|
|
"url": "mock://test",
|
|
"device_type": "mock",
|
|
"led_count": 30,
|
|
},
|
|
)
|
|
assert resp.status_code == 201
|
|
|
|
resp = client.get("/api/v1/devices")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["count"] == 3
|
|
names = {d["name"] for d in data["devices"]}
|
|
assert names == {"Device 0", "Device 1", "Device 2"}
|
|
|
|
def test_get_nonexistent_device_returns_404(self, client):
|
|
resp = client.get("/api/v1/devices/nonexistent_id")
|
|
assert resp.status_code == 404
|
|
|
|
def test_delete_nonexistent_device_returns_404(self, client):
|
|
resp = client.delete("/api/v1/devices/nonexistent_id")
|
|
assert resp.status_code == 404
|
|
|
|
def test_update_nonexistent_device_returns_404(self, client):
|
|
resp = client.put(
|
|
"/api/v1/devices/nonexistent_id",
|
|
json={"name": "Ghost"},
|
|
)
|
|
assert resp.status_code == 404
|
|
|
|
def test_update_tags(self, client):
|
|
"""Tags can be updated independently."""
|
|
resp = client.post(
|
|
"/api/v1/devices",
|
|
json={
|
|
"name": "Tag Device",
|
|
"url": "mock://test",
|
|
"device_type": "mock",
|
|
"led_count": 10,
|
|
"tags": ["original"],
|
|
},
|
|
)
|
|
device_id = resp.json()["id"]
|
|
|
|
resp = client.put(
|
|
f"/api/v1/devices/{device_id}",
|
|
json={"tags": ["updated", "twice"]},
|
|
)
|
|
assert resp.status_code == 200
|
|
assert resp.json()["tags"] == ["updated", "twice"]
|
|
|
|
def test_batch_device_states(self, client):
|
|
"""Batch states endpoint returns states for all devices."""
|
|
resp = client.get("/api/v1/devices/batch/states")
|
|
assert resp.status_code == 200
|
|
assert "states" in resp.json()
|