Files
openrouter-watcher/tests/unit/test_configuration.py
Luca Sacchi Ricciardi aece120017 feat(setup): T04 setup configuration files
- Create config.py with Pydantic Settings (SettingsConfigDict v2)
- Add all required configuration fields with defaults
- Create .env.example template with all environment variables
- Implement get_settings() with @lru_cache for performance
- Add test_configuration.py with 13 unit tests
- All tests passing (13/13)

Refs: T04
2026-04-07 09:52:33 +02:00

117 lines
5.3 KiB
Python

"""Test for configuration setup (T04)."""
import os
import sys
import pytest
# Add src to path for importing
sys.path.insert(0, '/home/google/Sources/LucaSacchiNet/openrouter-watcher/src')
@pytest.mark.unit
class TestConfigurationSetup:
"""Test configuration files and settings."""
def test_config_py_exists(self):
"""Verify config.py file exists."""
config_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/src/openrouter_monitor/config.py"
assert os.path.isfile(config_path), f"File {config_path} does not exist"
def test_env_example_exists(self):
"""Verify .env.example file exists."""
env_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/.env.example"
assert os.path.isfile(env_path), f"File {env_path} does not exist"
def test_config_py_has_settings_class(self):
"""Verify config.py contains Settings class."""
config_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/src/openrouter_monitor/config.py"
with open(config_path, 'r') as f:
content = f.read()
assert 'class Settings' in content, "config.py should contain Settings class"
def test_config_py_has_database_url(self):
"""Verify Settings has database_url field."""
config_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/src/openrouter_monitor/config.py"
with open(config_path, 'r') as f:
content = f.read()
assert 'database_url' in content.lower(), "Settings should have database_url field"
def test_config_py_has_secret_key(self):
"""Verify Settings has secret_key field."""
config_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/src/openrouter_monitor/config.py"
with open(config_path, 'r') as f:
content = f.read()
assert 'secret_key' in content.lower(), "Settings should have secret_key field"
def test_config_py_has_encryption_key(self):
"""Verify Settings has encryption_key field."""
config_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/src/openrouter_monitor/config.py"
with open(config_path, 'r') as f:
content = f.read()
assert 'encryption_key' in content.lower(), "Settings should have encryption_key field"
def test_config_py_uses_pydantic_settings(self):
"""Verify config.py uses pydantic_settings."""
config_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/src/openrouter_monitor/config.py"
with open(config_path, 'r') as f:
content = f.read()
assert 'BaseSettings' in content or 'pydantic_settings' in content, \
"config.py should use pydantic_settings BaseSettings"
def test_env_example_has_database_url(self):
"""Verify .env.example contains DATABASE_URL."""
env_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/.env.example"
with open(env_path, 'r') as f:
content = f.read()
assert 'DATABASE_URL' in content, ".env.example should contain DATABASE_URL"
def test_env_example_has_secret_key(self):
"""Verify .env.example contains SECRET_KEY."""
env_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/.env.example"
with open(env_path, 'r') as f:
content = f.read()
assert 'SECRET_KEY' in content, ".env.example should contain SECRET_KEY"
def test_env_example_has_encryption_key(self):
"""Verify .env.example contains ENCRYPTION_KEY."""
env_path = "/home/google/Sources/LucaSacchiNet/openrouter-watcher/.env.example"
with open(env_path, 'r') as f:
content = f.read()
assert 'ENCRYPTION_KEY' in content, ".env.example should contain ENCRYPTION_KEY"
def test_config_can_be_imported(self):
"""Verify config module can be imported successfully."""
try:
from openrouter_monitor.config import Settings, get_settings
assert True
except Exception as e:
pytest.fail(f"Failed to import config module: {e}")
def test_settings_class_instantiates(self):
"""Verify Settings class can be instantiated with test values."""
try:
from openrouter_monitor.config import Settings
# Test with required fields (use snake_case field names)
settings = Settings(
secret_key="test-secret-key-min-32-chars-long",
encryption_key="test-32-byte-encryption-key!!"
)
assert settings is not None
assert hasattr(settings, 'database_url')
except Exception as e:
pytest.fail(f"Failed to instantiate Settings: {e}")
def test_settings_has_defaults(self):
"""Verify Settings has sensible defaults."""
try:
from openrouter_monitor.config import Settings
settings = Settings(
secret_key="test-secret-key-min-32-chars-long",
encryption_key="test-32-byte-encryption-key!!"
)
# Check default values
assert settings.database_url is not None
assert settings.debug is not None
assert settings.log_level is not None
except Exception as e:
pytest.fail(f"Settings missing defaults: {e}")