ai_pipeline/tests/pipeline/api/test_rag.py

42 lines
1.2 KiB
Python

from httpx import AsyncClient, ASGITransport
from src.pipeline.main import app
from src.pipeline.utils import logger
from src.pipeline.core import llm
import pytest
import pytest_asyncio
import json
@pytest_asyncio.fixture(scope="session")
async def init_llm():
logger.debug('init_llm')
await llm.init_client()
yield
await llm.close_client()
@pytest_asyncio.fixture(scope="session")
async def app_client():
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client
@pytest.mark.asyncio
async def test_stream_chat(app_client, init_llm):
res = await app_client.post("/api/v1/rag/stream-chat", json={"query": "hello"})
assert res.status_code == 200
sse_messages = []
async for line in res.aiter_lines():
if not line :
continue
if line.startswith("data:"):
payload = line[len("data:") :].strip()
try:
obj = json.loads(payload)
except Exception:
obj = {"raw": payload}
logger.debug(obj)
sse_messages.append(obj)
assert sse_messages