feat: add configuration module with pydantic-settings
Made-with: Cursor
This commit is contained in:
14
src/ntr_fetcher/config.py
Normal file
14
src/ntr_fetcher/config.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = {"env_prefix": "NTR_"}
|
||||
|
||||
port: int = 8000
|
||||
host: str = "127.0.0.1"
|
||||
db_path: str = "./ntr_fetcher.db"
|
||||
poll_interval_seconds: int = 3600
|
||||
admin_token: str
|
||||
soundcloud_user: str = "nicktherat"
|
||||
show_day: int = 2
|
||||
show_hour: int = 22
|
||||
30
tests/test_config.py
Normal file
30
tests/test_config.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from ntr_fetcher.config import Settings
|
||||
|
||||
|
||||
def test_settings_defaults():
|
||||
settings = Settings(admin_token="test-secret")
|
||||
assert settings.port == 8000
|
||||
assert settings.host == "127.0.0.1"
|
||||
assert settings.db_path == "./ntr_fetcher.db"
|
||||
assert settings.poll_interval_seconds == 3600
|
||||
assert settings.soundcloud_user == "nicktherat"
|
||||
assert settings.show_day == 2
|
||||
assert settings.show_hour == 22
|
||||
|
||||
|
||||
def test_settings_from_env(monkeypatch):
|
||||
monkeypatch.setenv("NTR_PORT", "9090")
|
||||
monkeypatch.setenv("NTR_HOST", "0.0.0.0")
|
||||
monkeypatch.setenv("NTR_ADMIN_TOKEN", "my-secret")
|
||||
monkeypatch.setenv("NTR_SOUNDCLOUD_USER", "someoneelse")
|
||||
settings = Settings()
|
||||
assert settings.port == 9090
|
||||
assert settings.host == "0.0.0.0"
|
||||
assert settings.admin_token == "my-secret"
|
||||
assert settings.soundcloud_user == "someoneelse"
|
||||
|
||||
|
||||
def test_settings_admin_token_required():
|
||||
import pytest
|
||||
with pytest.raises(Exception):
|
||||
Settings()
|
||||
Reference in New Issue
Block a user