feat: add FastAPI app factory and health check endpoint
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
28
tests/conftest.py
Normal file
28
tests/conftest.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""Shared test fixtures for the SneakySwole test suite."""
|
||||
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _set_test_env() -> None:
|
||||
"""Ensure required env vars are set for all tests."""
|
||||
with patch.dict(os.environ, {
|
||||
"ADMIN_USERNAME": "testadmin",
|
||||
"ADMIN_PASSWORD": "testpass123",
|
||||
"APP_ENV": "development",
|
||||
"DATABASE_URL": "sqlite:///data/test_sneakyswole.db",
|
||||
}, clear=False):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client() -> TestClient:
|
||||
"""Create a FastAPI TestClient for integration tests."""
|
||||
from app.main import create_app
|
||||
|
||||
app = create_app()
|
||||
return TestClient(app)
|
||||
12
tests/test_app.py
Normal file
12
tests/test_app.py
Normal file
@@ -0,0 +1,12 @@
|
||||
"""Tests for the FastAPI application factory."""
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
class TestCreateApp:
|
||||
"""Tests for the application factory and basic setup."""
|
||||
|
||||
def test_app_starts_without_error(self, client: TestClient) -> None:
|
||||
"""The app should initialize and serve requests."""
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
@@ -11,10 +11,14 @@ class TestSettings:
|
||||
|
||||
def test_settings_loads_defaults(self) -> None:
|
||||
"""Settings should have sensible defaults for all fields."""
|
||||
with patch.dict(os.environ, {
|
||||
env = {
|
||||
"ADMIN_USERNAME": "testadmin",
|
||||
"ADMIN_PASSWORD": "testpass123",
|
||||
}, clear=False):
|
||||
}
|
||||
# Remove DATABASE_URL so we test the actual default
|
||||
env_clean = {k: v for k, v in os.environ.items() if k != "DATABASE_URL"}
|
||||
env_clean.update(env)
|
||||
with patch.dict(os.environ, env_clean, clear=True):
|
||||
settings = Settings()
|
||||
assert settings.admin_username == "testadmin"
|
||||
assert settings.admin_password == "testpass123"
|
||||
|
||||
30
tests/test_health.py
Normal file
30
tests/test_health.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Tests for the health check endpoint."""
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
class TestHealthCheck:
|
||||
"""Tests for GET /health."""
|
||||
|
||||
def test_health_returns_200(self, client: TestClient) -> None:
|
||||
"""GET /health should return HTTP 200."""
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_health_returns_status_ok(self, client: TestClient) -> None:
|
||||
"""GET /health should return a JSON body with status 'ok'."""
|
||||
response = client.get("/health")
|
||||
data = response.json()
|
||||
assert data["status"] == "ok"
|
||||
|
||||
def test_health_includes_app_name(self, client: TestClient) -> None:
|
||||
"""GET /health should include the application name."""
|
||||
response = client.get("/health")
|
||||
data = response.json()
|
||||
assert data["app"] == "sneakyswole"
|
||||
|
||||
def test_health_includes_version(self, client: TestClient) -> None:
|
||||
"""GET /health should include the application version."""
|
||||
response = client.get("/health")
|
||||
data = response.json()
|
||||
assert "version" in data
|
||||
Reference in New Issue
Block a user