test: upgrade backend test suite, implement unit tests, and modernize CRUD patterns
Build Backend / build (push) Successful in 6m39s

This commit is contained in:
2026-06-08 16:17:11 +02:00
parent 4d53887fcc
commit a9e1e96622
13 changed files with 759 additions and 25 deletions
+37
View File
@@ -0,0 +1,37 @@
import pytest
from app.config import Settings
def test_settings_defaults():
"""Test that default settings are correctly loaded."""
# We instantiate a new Settings object to test the class directly,
# rather than using the already-initialized 'settings' instance in app.config.
s = Settings(_env_file=None)
assert s.DATABASE_URL == "sqlite+aiosqlite:///./data/sleep.db"
assert s.CORS_ORIGINS == "http://localhost:5173"
assert s.LOG_LEVEL == "info"
def test_settings_env_overrides(monkeypatch):
"""Test that environment variables correctly override defaults."""
# Set environment variables using monkeypatch
monkeypatch.setenv("DATABASE_URL", "postgresql+asyncpg://testuser:testpass@localhost/testdb")
monkeypatch.setenv("CORS_ORIGINS", "http://example.com")
monkeypatch.setenv("LOG_LEVEL", "debug")
# Re-instantiate Settings to pick up the new environment variables
s = Settings(_env_file=None)
assert s.DATABASE_URL == "postgresql+asyncpg://testuser:testpass@localhost/testdb"
assert s.CORS_ORIGINS == "http://example.com"
assert s.LOG_LEVEL == "debug"
def test_settings_partial_override(monkeypatch):
"""Test that overriding only some environment variables works correctly."""
monkeypatch.setenv("LOG_LEVEL", "warning")
s = Settings(_env_file=None)
# Overridden value
assert s.LOG_LEVEL == "warning"
# Default values should remain
assert s.DATABASE_URL == "sqlite+aiosqlite:///./data/sleep.db"
assert s.CORS_ORIGINS == "http://localhost:5173"