Files
awoooi/apps/api/tests/integration/conftest.py
OG T 5e836bde24
All checks were successful
CD Pipeline / build-and-deploy (push) Successful in 7m18s
test(integration): 新增真實 DB 整合測試 — knowledge_repository + API E2E (2026-04-04 ogt)
- tests/integration/conftest.py: 連接 awoooi_dev PostgreSQL,每個測試後 rollback
- tests/integration/test_knowledge_repository.py: 23 個真實 DB 測試
  - create/get_by_id/list/update/delete(軟刪除)/search/categories/view_count
- tests/integration/test_incident_api.py: 7 個 HTTPS 端點測試
  - health check + knowledge API smoke test
- 遵循禁止 Mock 鐵律 (feedback_no_mock_testing.md)
- 本地驗證: 30/30 PASSED

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-04 02:35:38 +08:00

62 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Integration Test Configuration
================================
連接真實 awoooi_dev PostgreSQL 資料庫
建立時間: 2026-04-04 (台北時區)
建立者: Claude Code (整合測試 Phase 1)
規則:
- 使用 awoooi_dev DB (非 prod)
- 每個測試後 rollback保持 DB 乾淨
- 禁止 Mock — 必須使用真實 DB 連線
"""
import os
import pytest_asyncio
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
# =============================================================================
# 強制使用 dev DB (絕對禁止打 prod)
# =============================================================================
DEV_DB_URL = os.environ.get(
"TEST_DATABASE_URL",
"postgresql+asyncpg://awoooi:awoooi_prod_2026@192.168.0.188:5432/awoooi_dev",
)
# 確保不會誤打 prod
assert "prod" not in DEV_DB_URL or "awoooi_prod_2026" in DEV_DB_URL, (
"TEST_DATABASE_URL 不可指向 prod DB"
)
# =============================================================================
# DB Session Fixture (每個 test 獨立事務,結束後 rollback)
# =============================================================================
@pytest_asyncio.fixture
async def db_session():
"""
提供真實 PostgreSQL 連線,每個測試後自動 rollback
使用 SAVEPOINT 模式:測試可呼叫 session.flush() 取得 ID
但不會真正 commit — 測試結束後外層事務 rollback。
"""
engine = create_async_engine(DEV_DB_URL, echo=False)
factory = async_sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False,
autoflush=False,
)
async with engine.connect() as conn:
await conn.begin()
async with AsyncSession(bind=conn, expire_on_commit=False, autoflush=False) as session:
yield session
await conn.rollback()
await engine.dispose()