feat: add settings and async db engine
This commit is contained in:
13
backend/app/config.py
Normal file
13
backend/app/config.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
|
||||
|
||||
database_url: str
|
||||
ollama_base_url: str
|
||||
session_secret: str
|
||||
port: int = 7777
|
||||
|
||||
|
||||
settings = Settings()
|
||||
16
backend/app/db.py
Normal file
16
backend/app/db.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
from app.config import settings
|
||||
|
||||
engine = create_async_engine(settings.database_url, echo=False)
|
||||
async_session_maker = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
async def get_db() -> AsyncSession:
|
||||
async with async_session_maker() as session:
|
||||
yield session
|
||||
8
backend/tests/conftest.py
Normal file
8
backend/tests/conftest.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import os
|
||||
|
||||
|
||||
def pytest_configure(config):
|
||||
"""Set up minimal environment variables for tests before any imports."""
|
||||
os.environ.setdefault("DATABASE_URL", "postgresql+asyncpg://test:test@localhost/test")
|
||||
os.environ.setdefault("OLLAMA_BASE_URL", "http://localhost:11434")
|
||||
os.environ.setdefault("SESSION_SECRET", "test-secret-key")
|
||||
11
backend/tests/test_config.py
Normal file
11
backend/tests/test_config.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from app.config import Settings
|
||||
|
||||
|
||||
def test_settings_load_from_env(monkeypatch):
|
||||
monkeypatch.setenv("DATABASE_URL", "postgresql+asyncpg://u:p@host/db")
|
||||
monkeypatch.setenv("OLLAMA_BASE_URL", "http://10.30.20.107:11434")
|
||||
monkeypatch.setenv("SESSION_SECRET", "test-secret")
|
||||
settings = Settings(_env_file=None)
|
||||
assert settings.database_url == "postgresql+asyncpg://u:p@host/db"
|
||||
assert settings.ollama_base_url == "http://10.30.20.107:11434"
|
||||
assert settings.port == 7777
|
||||
Reference in New Issue
Block a user