|
| 1 | +import logging |
| 2 | +from typing import Awaitable, Callable |
| 3 | + |
| 4 | +from httpx import AsyncClient |
| 5 | +from pytest import mark # noqa |
| 6 | +from sqlalchemy import select |
| 7 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 8 | +from sqlalchemy.sql.functions import count |
| 9 | +from starlette import status |
| 10 | + |
| 11 | +from tests.models import Computer |
| 12 | +from tests.schemas import ComputerAttributesBaseSchema |
| 13 | + |
| 14 | +pytestmark = mark.asyncio |
| 15 | + |
| 16 | +logging.basicConfig(level=logging.DEBUG) |
| 17 | + |
| 18 | + |
| 19 | +class TestAtomicDeleteObjects: |
| 20 | + async def test_delete_two_objects( |
| 21 | + self, |
| 22 | + client: AsyncClient, |
| 23 | + async_session: AsyncSession, |
| 24 | + computer_factory: Callable[..., Awaitable[Computer]], |
| 25 | + ): |
| 26 | + computer_1 = await computer_factory() |
| 27 | + computer_2 = await computer_factory() |
| 28 | + |
| 29 | + computers_ids = [ |
| 30 | + computer_1.id, |
| 31 | + computer_2.id, |
| 32 | + ] |
| 33 | + stmt_computers = select(count(Computer.id)).where( |
| 34 | + Computer.id.in_(computers_ids), |
| 35 | + ) |
| 36 | + computers_count = await async_session.scalar(stmt_computers) |
| 37 | + assert computers_count == len(computers_ids) |
| 38 | + |
| 39 | + data_atomic_request = { |
| 40 | + "atomic:operations": [ |
| 41 | + { |
| 42 | + "op": "remove", |
| 43 | + "data": { |
| 44 | + "id": str(computer_1.id), |
| 45 | + "type": "computer", |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + "op": "remove", |
| 50 | + "data": { |
| 51 | + "id": str(computer_2.id), |
| 52 | + "type": "computer", |
| 53 | + }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + } |
| 57 | + response = await client.post("/operations", json=data_atomic_request) |
| 58 | + assert response.status_code == status.HTTP_200_OK, response.text |
| 59 | + response_data = response.json() |
| 60 | + assert "atomic:results" in response_data, response_data |
| 61 | + results = response_data["atomic:results"] |
| 62 | + assert results |
| 63 | + |
| 64 | + computers_count = await async_session.scalar(stmt_computers) |
| 65 | + assert computers_count == 0 |
| 66 | + |
| 67 | + assert results == [ |
| 68 | + { |
| 69 | + "data": { |
| 70 | + "id": str(computer_1.id), |
| 71 | + "type": "computer", |
| 72 | + "attributes": ComputerAttributesBaseSchema.from_orm(computer_1), |
| 73 | + }, |
| 74 | + "meta": None, |
| 75 | + }, |
| 76 | + { |
| 77 | + "data": { |
| 78 | + "id": str(computer_2.id), |
| 79 | + "type": "computer", |
| 80 | + "attributes": ComputerAttributesBaseSchema.from_orm(computer_2), |
| 81 | + }, |
| 82 | + "meta": None, |
| 83 | + }, |
| 84 | + ] |
0 commit comments