add async with all functions

This commit is contained in:
2026-10-23 00:20:58 +03:00
parent a85702a30e
commit 7b7ca1b888
42 changed files with 901 additions and 763 deletions
+58 -57
View File
@@ -2,17 +2,17 @@ from urllib.parse import quote
from uuid import uuid4
import pytest
from fastapi.testclient import TestClient
from httpx import AsyncClient
from tests.helpers import assert_request_id, bearer, create_chat, create_user
def test_create_and_get_chat(client: TestClient):
alice = create_user(client)
bob = create_user(client)
chat = create_chat(client, alice, bob)
async def test_create_and_get_chat(client: AsyncClient):
alice = await create_user(client)
bob = await create_user(client)
chat = await create_chat(client, alice, bob)
response = client.get(
response = await client.get(
f"/api/v1/chats/{chat['id']}",
headers=bearer(bob),
)
@@ -22,13 +22,13 @@ def test_create_and_get_chat(client: TestClient):
assert_request_id(response)
def test_chat_is_not_visible_to_outsider(client: TestClient):
alice = create_user(client)
bob = create_user(client)
eve = create_user(client)
chat = create_chat(client, alice, bob)
async def test_chat_is_not_visible_to_outsider(client: AsyncClient):
alice = await create_user(client)
bob = await create_user(client)
eve = await create_user(client)
chat = await create_chat(client, alice, bob)
response = client.get(
response = await client.get(
f"/api/v1/chats/{chat['id']}",
headers=bearer(eve),
)
@@ -38,11 +38,11 @@ def test_chat_is_not_visible_to_outsider(client: TestClient):
assert_request_id(response)
def test_chat_creation_requires_creator_in_members(client: TestClient):
alice = create_user(client)
bob = create_user(client)
async def test_chat_creation_requires_creator_in_members(client: AsyncClient):
alice = await create_user(client)
bob = await create_user(client)
response = client.post(
response = await client.post(
"/api/v1/chats",
headers=bearer(alice),
json={"title": "Чат", "member_ids": [bob["id"]]},
@@ -59,11 +59,11 @@ def test_chat_creation_requires_creator_in_members(client: TestClient):
{"title": " ", "member_ids": []},
],
)
def test_chat_payload_boundaries(client: TestClient, payload: dict):
alice = create_user(client)
async def test_chat_payload_boundaries(client: AsyncClient, payload: dict):
alice = await create_user(client)
payload["member_ids"] = [alice["id"]]
response = client.post(
response = await client.post(
"/api/v1/chats",
headers=bearer(alice),
json=payload,
@@ -73,11 +73,11 @@ def test_chat_payload_boundaries(client: TestClient, payload: dict):
assert response.json()["error"]["code"] == "invalid_request"
def test_duplicate_members_are_rejected(client: TestClient):
alice = create_user(client)
bob = create_user(client)
async def test_duplicate_members_are_rejected(client: AsyncClient):
alice = await create_user(client)
bob = await create_user(client)
response = client.post(
response = await client.post(
"/api/v1/chats",
headers=bearer(alice),
json={"title": "Чат", "member_ids": [alice["id"], bob["id"], bob["id"]]},
@@ -87,10 +87,10 @@ def test_duplicate_members_are_rejected(client: TestClient):
assert response.json()["error"]["code"] == "invalid_request"
def test_unknown_member_is_rejected(client: TestClient):
alice = create_user(client)
async def test_unknown_member_is_rejected(client: AsyncClient):
alice = await create_user(client)
response = client.post(
response = await client.post(
"/api/v1/chats",
headers=bearer(alice),
json={"title": "Чат", "member_ids": [alice["id"], str(uuid4())]},
@@ -100,9 +100,9 @@ def test_unknown_member_is_rejected(client: TestClient):
assert response.json()["error"]["code"] == "invalid_request"
def test_chat_exact_title_limit_is_accepted(client: TestClient):
alice = create_user(client)
response = client.post(
async def test_chat_exact_title_limit_is_accepted(client: AsyncClient):
alice = await create_user(client)
response = await client.post(
"/api/v1/chats",
headers=bearer(alice),
json={"title": "x" * 100, "member_ids": [alice["id"]]},
@@ -112,13 +112,13 @@ def test_chat_exact_title_limit_is_accepted(client: TestClient):
assert len(response.json()["title"]) == 100
def test_chat_list_hides_chats_without_membership(client: TestClient):
alice = create_user(client)
bob = create_user(client)
chat_for_bob = create_chat(client, bob, title="Only Bob")
chat_for_alice = create_chat(client, alice, title="Only Alice")
async def test_chat_list_hides_chats_without_membership(client: AsyncClient):
alice = await create_user(client)
bob = await create_user(client)
chat_for_bob = await create_chat(client, bob, title="Only Bob")
chat_for_alice = await create_chat(client, alice, title="Only Alice")
response = client.get(
response = await client.get(
"/api/v1/chats?limit=100",
headers=bearer(alice),
)
@@ -129,10 +129,10 @@ def test_chat_list_hides_chats_without_membership(client: TestClient):
assert chat_for_bob["id"] not in ids
def test_nonexistent_chat_returns_404(client: TestClient):
alice = create_user(client)
async def test_nonexistent_chat_returns_404(client: AsyncClient):
alice = await create_user(client)
response = client.get(
response = await client.get(
"/api/v1/chats/00000000-0000-4000-8000-000000000000",
headers=bearer(alice),
)
@@ -142,44 +142,45 @@ def test_nonexistent_chat_returns_404(client: TestClient):
assert_request_id(response)
def test_failed_chat_creation_leaves_no_partial_data(client: TestClient, db_engine):
async def test_failed_chat_creation_leaves_no_partial_data(client: AsyncClient, db_engine):
"""Атомарность: неизвестный участник -> нет ни чата, ни записей членства."""
from sqlalchemy import text
alice = create_user(client)
response = client.post(
alice = await create_user(client)
response = await client.post(
"/api/v1/chats",
headers=bearer(alice),
json={"title": "Чат", "member_ids": [alice["id"], str(uuid4())]},
)
assert response.status_code == 400
with db_engine.connect() as connection:
assert connection.execute(text("SELECT count(*) FROM chats")).scalar_one() == 0
assert (
connection.execute(text("SELECT count(*) FROM chat_members")).scalar_one() == 0
)
async with db_engine.connect() as connection:
chats = (await connection.execute(text("SELECT count(*) FROM chats"))).scalar_one()
members = (
await connection.execute(text("SELECT count(*) FROM chat_members"))
).scalar_one()
assert chats == 0 and members == 0
def test_chat_list_pagination_and_order(client: TestClient):
alice = create_user(client)
chats = [create_chat(client, alice, title=f"chat-{i}") for i in range(5)]
async def test_chat_list_pagination_and_order(client: AsyncClient):
alice = await create_user(client)
chats = [await create_chat(client, alice, title=f"chat-{i}") for i in range(5)]
page1 = client.get("/api/v1/chats?limit=2", headers=bearer(alice)).json()
page2 = client.get(
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 = client.get(
)).json()
page3 = (await client.get(
f"/api/v1/chats?limit=2&cursor={quote(page2['next_cursor'], safe='')}",
headers=bearer(alice),
).json()
)).json()
ids = [c["id"] for page in (page1, page2, page3) for c in page["items"]]
assert ids == [c["id"] for c in chats]
assert page3["next_cursor"] is None
def test_chat_endpoints_require_bearer(client: TestClient):
assert client.get("/api/v1/chats").status_code == 401
assert client.post("/api/v1/chats", json={"title": "x", "member_ids": []}).status_code in (401, 400)
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)