Six test files each defined the same profile() fixture. It now lives once in conftest.py, with test_profiles.py's 201 check on create. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
"""Black-box HTTP tests against a running controller.
|
|
|
|
Ported from the v1 Python app's tests (tests/test_endpoints_pytest.py, tests/test_endpoints.py).
|
|
Point them at a board with --host <ip> or LED_HOST; without one every test is skipped.
|
|
The board's stored collections are snapshotted first and written back afterwards.
|
|
"""
|
|
|
|
import os
|
|
|
|
import pytest
|
|
import requests
|
|
|
|
from helpers import new_id, unique
|
|
|
|
# Store collections (main/store.c NAMES), all reachable through GET/PUT /db/<name>
|
|
COLLECTIONS = ["device", "zone", "preset", "profile", "palette", "pattern",
|
|
"sequence", "scene", "group", "output", "settings"]
|
|
|
|
TIMEOUT = 10
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
parser.addoption("--host", default=os.environ.get("LED_HOST"),
|
|
help="controller address (or set LED_HOST)")
|
|
|
|
|
|
def pytest_collection_modifyitems(config, items):
|
|
if config.getoption("--host"):
|
|
return
|
|
skip = pytest.mark.skip(reason="no controller: pass --host or set LED_HOST")
|
|
for item in items:
|
|
item.add_marker(skip)
|
|
|
|
|
|
class Client(requests.Session):
|
|
"""requests.Session that takes paths relative to the controller."""
|
|
|
|
def __init__(self, base_url):
|
|
super().__init__()
|
|
self.base_url = base_url
|
|
self.headers.update({"User-Agent": "pytest/requests", "Accept": "application/json"})
|
|
|
|
def request(self, method, url, *args, **kwargs):
|
|
kwargs.setdefault("timeout", TIMEOUT)
|
|
return super().request(method, self.base_url + url, *args, **kwargs)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def base_url(request):
|
|
host = request.config.getoption("--host")
|
|
return host if host.startswith("http") else f"http://{host}"
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def c(base_url):
|
|
with Client(base_url) as client:
|
|
yield client
|
|
|
|
|
|
@pytest.fixture
|
|
def profile(c):
|
|
"""A new profile, made current for the test and deleted afterwards."""
|
|
resp = c.post("/profiles", json={"name": unique("pytest-profile")})
|
|
assert resp.status_code == 201
|
|
pid = new_id(resp)
|
|
assert c.post(f"/profiles/{pid}/apply").status_code == 200
|
|
yield pid
|
|
c.delete(f"/profiles/{pid}")
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def restore_db(request):
|
|
"""Snapshot every collection before the run and write it back afterwards."""
|
|
if not request.config.getoption("--host"):
|
|
yield
|
|
return
|
|
client = request.getfixturevalue("c")
|
|
snapshot = {}
|
|
for name in COLLECTIONS:
|
|
resp = client.get(f"/db/{name}")
|
|
resp.raise_for_status()
|
|
snapshot[name] = resp.json()
|
|
yield snapshot
|
|
failed = []
|
|
for name, data in snapshot.items():
|
|
resp = client.put(f"/db/{name}", json=data)
|
|
if resp.status_code != 200:
|
|
failed.append(f"{name}: {resp.status_code}")
|
|
assert not failed, f"could not restore the controller's data: {failed}"
|