112 lines
3.2 KiB
Python
112 lines
3.2 KiB
Python
|
|
"""Tests for GET /api/sms — SMS log.
|
||
|
|
|
||
|
|
Seeds 3 SMS rows (2 for user u1 at create_time 1000/3000, 1 for u2 at 2000)
|
||
|
|
and asserts filters + ordering (``create_time desc``) + pagination.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.db.models import Sms
|
||
|
|
|
||
|
|
|
||
|
|
async def _seed(session: AsyncSession) -> None:
|
||
|
|
session.add_all(
|
||
|
|
[
|
||
|
|
Sms(
|
||
|
|
sms_id="s1",
|
||
|
|
channel="sms",
|
||
|
|
user_id="u1",
|
||
|
|
receive_name="Alice",
|
||
|
|
content_preview="hello",
|
||
|
|
create_time=1000,
|
||
|
|
direction=1,
|
||
|
|
sender="111",
|
||
|
|
customer_phone="999",
|
||
|
|
),
|
||
|
|
Sms(
|
||
|
|
sms_id="s2",
|
||
|
|
channel="sms",
|
||
|
|
user_id="u2",
|
||
|
|
receive_name="Bob",
|
||
|
|
content_preview="hi",
|
||
|
|
create_time=2000,
|
||
|
|
direction=0,
|
||
|
|
),
|
||
|
|
Sms(
|
||
|
|
sms_id="s3",
|
||
|
|
channel="sms",
|
||
|
|
user_id="u1",
|
||
|
|
receive_name="Alice",
|
||
|
|
content_preview="bye",
|
||
|
|
create_time=3000,
|
||
|
|
direction=1,
|
||
|
|
),
|
||
|
|
]
|
||
|
|
)
|
||
|
|
await session.commit()
|
||
|
|
|
||
|
|
|
||
|
|
async def test_default_list_returns_all_ordered_desc(make_client):
|
||
|
|
"""No filters -> 3 rows ordered by ``create_time desc``."""
|
||
|
|
client = await make_client(_seed)
|
||
|
|
r = await client.get("/api/sms")
|
||
|
|
assert r.status_code == 200
|
||
|
|
body = r.json()
|
||
|
|
assert body["total"] == 3
|
||
|
|
assert [it["sms_id"] for it in body["items"]] == ["s3", "s2", "s1"]
|
||
|
|
for key in (
|
||
|
|
"sms_id",
|
||
|
|
"customer_phone",
|
||
|
|
"content_preview",
|
||
|
|
"sender",
|
||
|
|
"direction",
|
||
|
|
"user_id",
|
||
|
|
"receive_name",
|
||
|
|
"create_time",
|
||
|
|
"sim_phone",
|
||
|
|
):
|
||
|
|
assert key in body["items"][0]
|
||
|
|
|
||
|
|
|
||
|
|
async def test_filter_by_user_id(make_client):
|
||
|
|
"""``?user_id=u1`` -> 2 SMS for Alice."""
|
||
|
|
client = await make_client(_seed)
|
||
|
|
r = await client.get("/api/sms", params={"user_id": "u1"})
|
||
|
|
assert r.status_code == 200
|
||
|
|
body = r.json()
|
||
|
|
assert body["total"] == 2
|
||
|
|
assert {it["sms_id"] for it in body["items"]} == {"s1", "s3"}
|
||
|
|
|
||
|
|
|
||
|
|
async def test_time_window_filter(make_client):
|
||
|
|
"""``?start=1500&end=2500`` -> only s2."""
|
||
|
|
client = await make_client(_seed)
|
||
|
|
r = await client.get("/api/sms", params={"start": 1500, "end": 2500})
|
||
|
|
assert r.status_code == 200
|
||
|
|
body = r.json()
|
||
|
|
assert body["total"] == 1
|
||
|
|
assert body["items"][0]["sms_id"] == "s2"
|
||
|
|
|
||
|
|
|
||
|
|
async def test_filters_compose(make_client):
|
||
|
|
"""``user_id=u1 & end=2000`` -> only s1."""
|
||
|
|
client = await make_client(_seed)
|
||
|
|
r = await client.get("/api/sms", params={"user_id": "u1", "end": 2000})
|
||
|
|
assert r.status_code == 200
|
||
|
|
body = r.json()
|
||
|
|
assert body["total"] == 1
|
||
|
|
assert body["items"][0]["sms_id"] == "s1"
|
||
|
|
|
||
|
|
|
||
|
|
async def test_pagination(make_client):
|
||
|
|
"""``page_size=1&page=1`` -> 1 item, total 3."""
|
||
|
|
client = await make_client(_seed)
|
||
|
|
r = await client.get("/api/sms", params={"page": 1, "page_size": 1})
|
||
|
|
assert r.status_code == 200
|
||
|
|
body = r.json()
|
||
|
|
assert body["total"] == 3
|
||
|
|
assert len(body["items"]) == 1
|
||
|
|
assert body["items"][0]["sms_id"] == "s3"
|