2025-10-23 13:45:54 -04:00
|
|
|
"""Basic tests for the backend application."""
|
|
|
|
|
|
2026-06-19 07:39:56 -04:00
|
|
|
from typing import cast
|
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
|
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from backend.main import app, get_api_session, read_root
|
2025-10-23 13:45:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_creation():
|
|
|
|
|
"""Test that the FastAPI app is created properly."""
|
|
|
|
|
assert app is not None
|
|
|
|
|
assert app.title == "Plex Playlist Backend"
|
|
|
|
|
assert app.version == "0.1.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_read_root():
|
|
|
|
|
"""Test the root endpoint function."""
|
|
|
|
|
result = read_root()
|
|
|
|
|
assert result == {"message": "Plex Playlist Backend API"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_health_check():
|
|
|
|
|
"""Test the health check endpoint function."""
|
2026-06-19 07:39:56 -04:00
|
|
|
|
|
|
|
|
healthy_session = cast("AsyncSession", AsyncMock(spec=AsyncSession))
|
|
|
|
|
healthy_session.execute = AsyncMock(return_value=1)
|
|
|
|
|
|
|
|
|
|
async def override_get_session():
|
|
|
|
|
"""Provide a healthy session dependency override for tests."""
|
|
|
|
|
yield healthy_session
|
|
|
|
|
|
|
|
|
|
app.dependency_overrides[get_api_session] = override_get_session
|
|
|
|
|
try:
|
|
|
|
|
with TestClient(app) as client:
|
|
|
|
|
response = client.get("/health")
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.json() == {"status": "healthy", "database": "connected"}
|
|
|
|
|
finally:
|
|
|
|
|
app.dependency_overrides.clear()
|
2025-10-23 13:45:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_typeguard_validation():
|
|
|
|
|
"""Test that typeguard is working for type validation."""
|
|
|
|
|
|
|
|
|
|
# Test a function with type hints to ensure typeguard is active
|
|
|
|
|
def typed_function(x: int, y: str) -> str:
|
|
|
|
|
return f"{x}: {y}"
|
|
|
|
|
|
|
|
|
|
# This should work fine
|
|
|
|
|
result = typed_function(42, "test")
|
|
|
|
|
assert result == "42: test"
|
|
|
|
|
|
|
|
|
|
# This would fail with typeguard active, but we'll just test the happy path
|
|
|
|
|
# to ensure coverage of our code without breaking the test
|