87 lines
2.8 KiB
Python
87 lines
2.8 KiB
Python
"""
|
|
Tests for configuration management
|
|
"""
|
|
|
|
import pytest
|
|
from unittest.mock import patch
|
|
import os
|
|
|
|
from app.core.config import Settings
|
|
|
|
|
|
def test_default_settings():
|
|
"""Test default configuration values"""
|
|
settings = Settings()
|
|
|
|
assert settings.HOST == "0.0.0.0"
|
|
assert settings.PORT == 8082
|
|
assert settings.METUBE_URL == "http://localhost:8081"
|
|
assert settings.DATABASE_URL.endswith("data/playlists.db") # Account for absolute path conversion
|
|
assert settings.DEFAULT_CHECK_INTERVAL == 60
|
|
assert settings.MAX_CONCURRENT_DOWNLOADS == 3
|
|
|
|
|
|
def test_metube_url_validation():
|
|
"""Test MeTube URL validation and normalization"""
|
|
settings = Settings(METUBE_URL="http://localhost:8081/")
|
|
assert settings.METUBE_URL == "http://localhost:8081"
|
|
|
|
settings = Settings(METUBE_URL="https://metube.example.com/")
|
|
assert settings.METUBE_URL == "https://metube.example.com"
|
|
|
|
|
|
def test_check_interval_validation():
|
|
"""Test check interval validation"""
|
|
with pytest.raises(ValueError):
|
|
Settings(DEFAULT_CHECK_INTERVAL=0)
|
|
|
|
with pytest.raises(ValueError):
|
|
Settings(DEFAULT_CHECK_INTERVAL=1500) # > 24 hours
|
|
|
|
# Valid values should work
|
|
settings = Settings(DEFAULT_CHECK_INTERVAL=30)
|
|
assert settings.DEFAULT_CHECK_INTERVAL == 30
|
|
|
|
|
|
def test_max_concurrent_validation():
|
|
"""Test max concurrent downloads validation"""
|
|
with pytest.raises(ValueError):
|
|
Settings(MAX_CONCURRENT_DOWNLOADS=0)
|
|
|
|
with pytest.raises(ValueError):
|
|
Settings(MAX_CONCURRENT_DOWNLOADS=15) # > 10
|
|
|
|
# Valid values should work
|
|
settings = Settings(MAX_CONCURRENT_DOWNLOADS=5)
|
|
assert settings.MAX_CONCURRENT_DOWNLOADS == 5
|
|
|
|
|
|
def test_database_url_validation():
|
|
"""Test database URL validation"""
|
|
settings = Settings(DATABASE_URL="sqlite:///data/playlists.db")
|
|
assert "sqlite:///" in settings.DATABASE_URL
|
|
|
|
# Test absolute path conversion for relative paths
|
|
settings = Settings(DATABASE_URL="sqlite:///data/db.sqlite")
|
|
assert os.path.isabs(settings.DATABASE_URL.replace("sqlite:///", ""))
|
|
|
|
|
|
def test_environment_variables():
|
|
"""Test loading from environment variables"""
|
|
env_vars = {
|
|
"HOST": "127.0.0.1",
|
|
"PORT": "9000",
|
|
"METUBE_URL": "http://metube:8081",
|
|
"DATABASE_URL": "sqlite:///test.db",
|
|
"DEFAULT_CHECK_INTERVAL": "30",
|
|
"LOG_LEVEL": "DEBUG"
|
|
}
|
|
|
|
with patch.dict(os.environ, env_vars):
|
|
settings = Settings()
|
|
assert settings.HOST == "127.0.0.1"
|
|
assert settings.PORT == 9000
|
|
assert settings.METUBE_URL == "http://metube:8081"
|
|
assert settings.DATABASE_URL.endswith("test.db")
|
|
assert settings.DEFAULT_CHECK_INTERVAL == 30
|
|
assert settings.LOG_LEVEL == "DEBUG" |