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"