88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
"""辰正 time API 端点测试。"""
|
||
|
||
from collections.abc import AsyncGenerator
|
||
|
||
import pytest
|
||
from httpx import ASGITransport, AsyncClient
|
||
|
||
from app.main import app
|
||
|
||
|
||
@pytest.fixture
|
||
async def client(auth_headers: dict[str, str]) -> AsyncGenerator[AsyncClient, None]:
|
||
transport = ASGITransport(app=app)
|
||
async with AsyncClient(
|
||
transport=transport,
|
||
base_url="http://test",
|
||
headers=auth_headers,
|
||
) as http_client:
|
||
yield http_client
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_config(client: AsyncClient) -> None:
|
||
response = await client.get("/api/v1/time/config")
|
||
assert response.status_code == 200
|
||
data = response.json()
|
||
assert data["user_timezone"] == "Asia/Shanghai"
|
||
assert data["default_city"] == "上海"
|
||
assert data["care_enabled"] is True
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_anniversaries_crud(client: AsyncClient) -> None:
|
||
create = await client.post(
|
||
"/api/v1/time/anniversaries",
|
||
json={
|
||
"name": "测试纪念日",
|
||
"calendar": "solar",
|
||
"date": "2025-09-19",
|
||
"recurs_yearly": True,
|
||
"kind": "anniversary",
|
||
"subject_type": "relationship",
|
||
"source": "manual",
|
||
},
|
||
)
|
||
assert create.status_code == 201
|
||
anniv_id = create.json()["id"]
|
||
assert anniv_id.startswith("anniv_")
|
||
|
||
listed = await client.get("/api/v1/time/anniversaries")
|
||
assert listed.status_code == 200
|
||
assert any(a["id"] == anniv_id for a in listed.json())
|
||
|
||
patched = await client.patch(
|
||
f"/api/v1/time/anniversaries/{anniv_id}", json={"name": "改后的名字"}
|
||
)
|
||
assert patched.status_code == 200
|
||
assert patched.json()["name"] == "改后的名字"
|
||
|
||
deleted = await client.delete(f"/api/v1/time/anniversaries/{anniv_id}")
|
||
assert deleted.status_code == 204
|
||
|
||
not_found = await client.get(f"/api/v1/time/anniversaries/{anniv_id}")
|
||
assert not_found.status_code == 404
|
||
assert not_found.json()["error"]["code"] == "not_found"
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_snapshot(client: AsyncClient) -> None:
|
||
# ASGITransport 可能不触发 lifespan 装配 event_store;get_event_store 未就绪时返回 503
|
||
response = await client.get("/api/v1/time/snapshot")
|
||
assert response.status_code in (200, 503)
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
assert "context_text" in data
|
||
assert "snapshot" in data
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_acquaintance(client: AsyncClient) -> None:
|
||
# 与 /snapshot 同因:ASGITransport 可能不触发 lifespan;snapshot 降级时返回 503
|
||
response = await client.get("/api/v1/time/acquaintance")
|
||
assert response.status_code in (200, 503)
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
assert isinstance(data["total_days"], int)
|
||
assert "natural_language" in data
|