fix: isolate tests from production database

Tests that imported wled_controller.main at module level caused the real
production database (data/ledgrab.db) to be opened before test fixtures
could patch the config. This led to silent data loss.

Patch the global config singleton at conftest module level (before any
test imports main.py) to redirect all DB access to a temp directory.
This commit is contained in:
2026-04-01 19:01:56 +03:00
parent 6b0e4e5539
commit 992495e2e4
3 changed files with 79 additions and 24 deletions
+19 -11
View File
@@ -4,31 +4,39 @@ import os
import sys
import pytest
from fastapi.testclient import TestClient
from wled_controller.main import app
from wled_controller import __version__
from wled_controller.config import get_config
_has_display = bool(os.environ.get("DISPLAY") or sys.platform == "win32" or sys.platform == "darwin")
_has_display = bool(
os.environ.get("DISPLAY") or sys.platform == "win32" or sys.platform == "darwin"
)
requires_display = pytest.mark.skipif(not _has_display, reason="No display available (headless CI)")
client = TestClient(app)
# Build auth header from the first configured API key
_config = get_config()
_api_key = next(iter(_config.auth.api_keys.values()), "")
AUTH_HEADERS = {"Authorization": f"Bearer {_api_key}"} if _api_key else {}
def test_root_endpoint():
@pytest.fixture(scope="module")
def client():
"""Provide a TestClient backed by the isolated test database."""
from fastapi.testclient import TestClient
from wled_controller.main import app
with TestClient(app, raise_server_exceptions=False) as c:
yield c
def test_root_endpoint(client):
"""Test root endpoint returns the HTML dashboard."""
response = client.get("/")
assert response.status_code == 200
assert "text/html" in response.headers["content-type"]
def test_health_check():
def test_health_check(client):
"""Test health check endpoint."""
response = client.get("/health")
assert response.status_code == 200
@@ -38,7 +46,7 @@ def test_health_check():
assert "timestamp" in data
def test_version_endpoint():
def test_version_endpoint(client):
"""Test version endpoint."""
response = client.get("/api/v1/version")
assert response.status_code == 200
@@ -49,7 +57,7 @@ def test_version_endpoint():
@requires_display
def test_get_displays():
def test_get_displays(client):
"""Test get displays endpoint (requires auth and a real display)."""
response = client.get("/api/v1/config/displays", headers=AUTH_HEADERS)
assert response.status_code == 200
@@ -69,7 +77,7 @@ def test_get_displays():
assert "is_primary" in display
def test_openapi_docs():
def test_openapi_docs(client):
"""Test OpenAPI documentation is available."""
response = client.get("/openapi.json")
assert response.status_code == 200
@@ -77,7 +85,7 @@ def test_openapi_docs():
assert data["info"]["version"] == __version__
def test_swagger_ui():
def test_swagger_ui(client):
"""Test Swagger UI is available."""
response = client.get("/docs")
assert response.status_code == 200