add tests
This commit is contained in:
+21
-11
@@ -142,7 +142,9 @@ async def test_nonexistent_chat_returns_404(client: AsyncClient):
|
||||
assert_request_id(response)
|
||||
|
||||
|
||||
async def test_failed_chat_creation_leaves_no_partial_data(client: AsyncClient, db_engine):
|
||||
async def test_failed_chat_creation_leaves_no_partial_data(
|
||||
client: AsyncClient, db_engine
|
||||
):
|
||||
"""Атомарность: неизвестный участник -> нет ни чата, ни записей членства."""
|
||||
from sqlalchemy import text
|
||||
|
||||
@@ -155,7 +157,9 @@ async def test_failed_chat_creation_leaves_no_partial_data(client: AsyncClient,
|
||||
assert response.status_code == 400
|
||||
|
||||
async with db_engine.connect() as connection:
|
||||
chats = (await connection.execute(text("SELECT count(*) FROM chats"))).scalar_one()
|
||||
chats = (
|
||||
await connection.execute(text("SELECT count(*) FROM chats"))
|
||||
).scalar_one()
|
||||
members = (
|
||||
await connection.execute(text("SELECT count(*) FROM chat_members"))
|
||||
).scalar_one()
|
||||
@@ -167,14 +171,18 @@ async def test_chat_list_pagination_and_order(client: AsyncClient):
|
||||
chats = [await create_chat(client, alice, title=f"chat-{i}") for i in range(5)]
|
||||
|
||||
page1 = (await client.get("/api/v1/chats?limit=2", headers=bearer(alice))).json()
|
||||
page2 = (await client.get(
|
||||
f"/api/v1/chats?limit=2&cursor={quote(page1['next_cursor'], safe='')}",
|
||||
headers=bearer(alice),
|
||||
)).json()
|
||||
page3 = (await client.get(
|
||||
f"/api/v1/chats?limit=2&cursor={quote(page2['next_cursor'], safe='')}",
|
||||
headers=bearer(alice),
|
||||
)).json()
|
||||
page2 = (
|
||||
await client.get(
|
||||
f"/api/v1/chats?limit=2&cursor={quote(page1['next_cursor'], safe='')}",
|
||||
headers=bearer(alice),
|
||||
)
|
||||
).json()
|
||||
page3 = (
|
||||
await client.get(
|
||||
f"/api/v1/chats?limit=2&cursor={quote(page2['next_cursor'], safe='')}",
|
||||
headers=bearer(alice),
|
||||
)
|
||||
).json()
|
||||
|
||||
ids = [c["id"] for page in (page1, page2, page3) for c in page["items"]]
|
||||
assert ids == [c["id"] for c in chats]
|
||||
@@ -183,4 +191,6 @@ async def test_chat_list_pagination_and_order(client: AsyncClient):
|
||||
|
||||
async def test_chat_endpoints_require_bearer(client: AsyncClient):
|
||||
assert (await client.get("/api/v1/chats")).status_code == 401
|
||||
assert (await client.post("/api/v1/chats", json={"title": "x", "member_ids": []})).status_code in (401, 400)
|
||||
assert (
|
||||
await client.post("/api/v1/chats", json={"title": "x", "member_ids": []})
|
||||
).status_code in (401, 400)
|
||||
|
||||
Reference in New Issue
Block a user