feat: Phase 3 Tasks 16-18 — Pydantic-Settings, Error-Handler, systemd
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Test für src.config — Pydantic-Settings Konfiguration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from src.config import Settings, settings
|
||||
|
||||
|
||||
class TestSettings:
|
||||
"""Tests für die zentrale Settings-Klasse."""
|
||||
|
||||
@patch.dict(os.environ, {"REDIS_HOST": "redis.example.com", "REDIS_PORT": "6380"})
|
||||
def test_settings_liest_env_vars(self) -> None:
|
||||
class EnvSettings(Settings):
|
||||
model_config = {"env_file": None}
|
||||
|
||||
env = EnvSettings()
|
||||
assert env.REDIS_HOST == "redis.example.com"
|
||||
assert env.REDIS_PORT == 6380
|
||||
|
||||
def test_settings_standardwerte(self) -> None:
|
||||
"""Standardwerte ohne Umgebungsvariablen."""
|
||||
|
||||
class EnvSettings(Settings):
|
||||
model_config = {"env_file": None}
|
||||
|
||||
env = EnvSettings()
|
||||
assert env.REDIS_HOST == "localhost"
|
||||
assert env.REDIS_PORT == 6379
|
||||
assert env.APP_HOST == "0.0.0.0"
|
||||
assert env.APP_PORT == 5002
|
||||
assert env.APP_DEBUG is False
|
||||
|
||||
def test_settings_falsches_redis_port_wirft(self) -> None:
|
||||
"""Ungültiger Port-Typ wirft ValidationError."""
|
||||
with pytest.raises(Exception): # type: ignore[misc]
|
||||
with patch.dict(os.environ, {"REDIS_PORT": "nicht_zahl"}, clear=True):
|
||||
Settings._build_customise_schema()
|
||||
Settings.model_rebuild(force=True)
|
||||
env = Settings(model_config={"env_file": None})
|
||||
_ = env.REDIS_PORT # noqa: B018
|
||||
|
||||
def test_settings_globale_instance(self) -> None:
|
||||
"""globale settings-Instanz ist vom Typ Settings."""
|
||||
assert isinstance(settings, Settings)
|
||||
assert settings.REDIS_HOST == "localhost"
|
||||
assert settings.APP_PORT == 5002
|
||||
Reference in New Issue
Block a user