chore: Database enhancement (#329)

This commit is contained in:
d3vyce
2026-06-27 13:30:38 +02:00
committed by GitHub
parent 1e021005bc
commit 70e0b3b9d5
3 changed files with 65 additions and 10 deletions
+29 -1
View File
@@ -3,12 +3,13 @@
import asyncio
import uuid
from contextlib import asynccontextmanager
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi import Depends, FastAPI
from fastapi.responses import StreamingResponse
from httpx import ASGITransport, AsyncClient
from pydantic import PostgresDsn
from sqlalchemy import (
Column,
ForeignKey,
@@ -94,6 +95,33 @@ class TestDatabaseConstruction:
with pytest.raises(TypeError):
Database(engine=engine, pool_size=5)
@pytest.mark.anyio
async def test_connect_args_with_engine_raises(self, engine):
"""connect_args are rejected in engine= mode."""
with pytest.raises(TypeError):
Database(engine=engine, connect_args={"server_settings": {}})
@pytest.mark.anyio
async def test_accepts_postgres_dsn(self):
"""A Pydantic PostgresDsn is coerced to a string URL."""
dsn = PostgresDsn(DATABASE_URL)
db = Database(dsn)
try:
assert db._owns_engine is True
assert str(db.engine.url) == str(create_async_engine(DATABASE_URL).url)
finally:
await db.engine.dispose()
@pytest.mark.anyio
async def test_connect_args_forwarded_to_engine(self):
"""connect_args are forwarded to create_async_engine in URL mode."""
connect_args = {"server_settings": {"application_name": "ft_test"}}
with patch("fastapi_toolsets.db.core.create_async_engine") as mocked:
mocked.return_value = MagicMock()
Database(DATABASE_URL, connect_args=connect_args)
_, kwargs = mocked.call_args
assert kwargs["connect_args"] == connect_args
@pytest.mark.anyio
async def test_url_mode_owns_engine(self):
"""URL mode builds and owns the engine."""