mirror of
https://github.com/sortedcord/alemno-payments.git
synced 2026-07-22 04:02:49 +05:30
setup dependencies and fix test_job_repositories missing deps
This commit is contained in:
@@ -3,11 +3,15 @@ from typing import AsyncGenerator
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.dependencies import get_db
|
||||
from app.db.base import Base
|
||||
from app.main import app
|
||||
|
||||
# Use a test database suffix or a separate database for testing
|
||||
TEST_DATABASE_URL = settings.DATABASE_URL.replace(settings.POSTGRES_DB, f"test_{settings.POSTGRES_DB}")
|
||||
|
||||
# Create async engine for test db
|
||||
test_engine = create_async_engine(
|
||||
TEST_DATABASE_URL,
|
||||
@@ -58,3 +62,39 @@ def initialize_test_db():
|
||||
loop.run_until_complete(teardown_db())
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def db_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
"""Provide a transactional test database session."""
|
||||
if _db_offline:
|
||||
pytest.skip("PostgreSQL database is offline")
|
||||
return
|
||||
|
||||
async with TestAsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client(db_session: AsyncSession) -> AsyncGenerator[AsyncClient, None]:
|
||||
"""Provide an HTTPX AsyncClient for FastAPI endpoint testing with db overrides."""
|
||||
# Override get_db dependency to use the test session
|
||||
async def override_get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
yield db_session
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
async with AsyncClient(
|
||||
transport=ASGITransport(app=app),
|
||||
base_url="http://test",
|
||||
) as client:
|
||||
yield client
|
||||
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
Reference in New Issue
Block a user