45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from pydantic_settings import SettingsConfigDict
|
|
|
|
|
|
class SettingsLocalEnvTests(unittest.TestCase):
|
|
def test_settings_composes_database_url_from_components(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
(root / ".env.local").write_text(
|
|
"DATABASE_HOST=192.168.3.90\n"
|
|
"DATABASE_PORT=5432\n"
|
|
"DATABASE_NAME=ai_ict\n"
|
|
"DATABASE_USER=yuqei\n"
|
|
"DATABASE_PASSWORD=abc123\n",
|
|
encoding="utf-8",
|
|
)
|
|
(root / ".env").write_text("", encoding="utf-8")
|
|
|
|
import src.config.settings as settings_module
|
|
|
|
with patch.dict(os.environ, {}, clear=True), patch.object(settings_module, "ROOT", root), patch.object(
|
|
settings_module.Settings,
|
|
"model_config",
|
|
SettingsConfigDict(
|
|
env_file=(str(root / ".env.local"), str(root / ".env")),
|
|
env_file_encoding="utf-8",
|
|
extra="ignore",
|
|
),
|
|
):
|
|
settings = settings_module.Settings()
|
|
|
|
self.assertEqual(
|
|
settings.database_url,
|
|
"postgresql+psycopg://yuqei:abc123@192.168.3.90:5432/ai_ict",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|