feat: pydantic-settings config mit allen env-vars
This commit is contained in:
33
app/config.py
Normal file
33
app/config.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
from pydantic import Field
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
|
||||||
|
|
||||||
|
nextcloud_webdav_url: str
|
||||||
|
nextcloud_user: str
|
||||||
|
nextcloud_app_password: str
|
||||||
|
|
||||||
|
ollama_url: str
|
||||||
|
ollama_embed_model: str
|
||||||
|
|
||||||
|
qdrant_url: str
|
||||||
|
qdrant_collection: str
|
||||||
|
|
||||||
|
webhook_secret: str
|
||||||
|
|
||||||
|
ingest_root: str = "Documents/THB/Studium"
|
||||||
|
chunk_size_words: int = 500
|
||||||
|
chunk_overlap_words: int = 50
|
||||||
|
log_level: str = "INFO"
|
||||||
|
|
||||||
|
|
||||||
|
_settings: Settings | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def get_settings() -> Settings:
|
||||||
|
global _settings
|
||||||
|
if _settings is None:
|
||||||
|
_settings = Settings()
|
||||||
|
return _settings
|
||||||
52
tests/test_config.py
Normal file
52
tests/test_config.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import os
|
||||||
|
import pytest
|
||||||
|
from app.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def test_settings_loads_all_required_fields(monkeypatch):
|
||||||
|
monkeypatch.setenv("NEXTCLOUD_WEBDAV_URL", "https://nc/remote.php/dav/files/u")
|
||||||
|
monkeypatch.setenv("NEXTCLOUD_USER", "u")
|
||||||
|
monkeypatch.setenv("NEXTCLOUD_APP_PASSWORD", "pw")
|
||||||
|
monkeypatch.setenv("OLLAMA_URL", "http://ollama:11434")
|
||||||
|
monkeypatch.setenv("OLLAMA_EMBED_MODEL", "qwen3-embedding:0.6b")
|
||||||
|
monkeypatch.setenv("QDRANT_URL", "http://qdrant:6333")
|
||||||
|
monkeypatch.setenv("QDRANT_COLLECTION", "rag_test")
|
||||||
|
monkeypatch.setenv("WEBHOOK_SECRET", "secret")
|
||||||
|
|
||||||
|
s = Settings()
|
||||||
|
|
||||||
|
assert s.nextcloud_user == "u"
|
||||||
|
assert s.qdrant_collection == "rag_test"
|
||||||
|
assert s.ingest_root == "Documents/THB/Studium" # default
|
||||||
|
assert s.chunk_size_words == 500
|
||||||
|
assert s.chunk_overlap_words == 50
|
||||||
|
assert s.log_level == "INFO"
|
||||||
|
|
||||||
|
|
||||||
|
def test_settings_overrides_defaults(monkeypatch):
|
||||||
|
for k, v in {
|
||||||
|
"NEXTCLOUD_WEBDAV_URL": "x", "NEXTCLOUD_USER": "x",
|
||||||
|
"NEXTCLOUD_APP_PASSWORD": "x", "OLLAMA_URL": "x",
|
||||||
|
"OLLAMA_EMBED_MODEL": "x", "QDRANT_URL": "x",
|
||||||
|
"QDRANT_COLLECTION": "x", "WEBHOOK_SECRET": "x",
|
||||||
|
"INGEST_ROOT": "Other/Path",
|
||||||
|
"CHUNK_SIZE_WORDS": "300",
|
||||||
|
"CHUNK_OVERLAP_WORDS": "30",
|
||||||
|
}.items():
|
||||||
|
monkeypatch.setenv(k, v)
|
||||||
|
|
||||||
|
s = Settings()
|
||||||
|
|
||||||
|
assert s.ingest_root == "Other/Path"
|
||||||
|
assert s.chunk_size_words == 300
|
||||||
|
assert s.chunk_overlap_words == 30
|
||||||
|
|
||||||
|
|
||||||
|
def test_settings_missing_required_raises(monkeypatch):
|
||||||
|
for k in ["NEXTCLOUD_WEBDAV_URL", "NEXTCLOUD_USER", "NEXTCLOUD_APP_PASSWORD",
|
||||||
|
"OLLAMA_URL", "OLLAMA_EMBED_MODEL", "QDRANT_URL",
|
||||||
|
"QDRANT_COLLECTION", "WEBHOOK_SECRET"]:
|
||||||
|
monkeypatch.delenv(k, raising=False)
|
||||||
|
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
Settings()
|
||||||
Reference in New Issue
Block a user