first round of review
This commit is contained in:
parent
7ebfb7a2dd
commit
da5a0d03eb
14 changed files with 362 additions and 244 deletions
|
|
@ -1,4 +1,9 @@
|
|||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
# Set required env vars before importing app
|
||||
os.environ.setdefault("SECRET_KEY", "test-secret-key-for-testing-only")
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
||||
|
|
@ -12,8 +17,38 @@ TEST_DATABASE_URL = os.getenv(
|
|||
)
|
||||
|
||||
|
||||
class ClientFactory:
|
||||
"""Factory for creating httpx clients with optional cookies."""
|
||||
|
||||
def __init__(self, transport, base_url):
|
||||
self._transport = transport
|
||||
self._base_url = base_url
|
||||
|
||||
@asynccontextmanager
|
||||
async def create(self, cookies: dict | None = None):
|
||||
"""Create a new client, optionally with cookies set."""
|
||||
async with AsyncClient(
|
||||
transport=self._transport,
|
||||
base_url=self._base_url,
|
||||
cookies=cookies or {},
|
||||
) as client:
|
||||
yield client
|
||||
|
||||
async def request(self, method: str, url: str, **kwargs):
|
||||
"""Make a one-off request without cookies."""
|
||||
async with self.create() as client:
|
||||
return await client.request(method, url, **kwargs)
|
||||
|
||||
async def get(self, url: str, **kwargs):
|
||||
return await self.request("GET", url, **kwargs)
|
||||
|
||||
async def post(self, url: str, **kwargs):
|
||||
return await self.request("POST", url, **kwargs)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
async def client():
|
||||
async def client_factory():
|
||||
"""Fixture that provides a factory for creating clients."""
|
||||
engine = create_async_engine(TEST_DATABASE_URL)
|
||||
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
|
|
@ -28,8 +63,17 @@ async def client():
|
|||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
|
||||
yield c
|
||||
transport = ASGITransport(app=app)
|
||||
factory = ClientFactory(transport, "http://test")
|
||||
|
||||
yield factory
|
||||
|
||||
app.dependency_overrides.clear()
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
async def client(client_factory):
|
||||
"""Fixture for a simple client without cookies (backwards compatible)."""
|
||||
async with client_factory.create() as c:
|
||||
yield c
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue