|
| 1 | +import logging |
| 2 | + |
| 3 | +from httpx import AsyncClient |
| 4 | +from pytest import mark # noqa |
| 5 | +from sqlalchemy import and_, or_, select |
| 6 | +from sqlalchemy.engine import Result |
| 7 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 8 | +from starlette import status |
| 9 | + |
| 10 | +from tests.misc.utils import fake |
| 11 | +from tests.models import User |
| 12 | +from tests.schemas import UserAttributesBaseSchema |
| 13 | + |
| 14 | +pytestmark = mark.asyncio |
| 15 | + |
| 16 | +logging.basicConfig(level=logging.DEBUG) |
| 17 | + |
| 18 | + |
| 19 | +class TestAtomicCreateObjects: |
| 20 | + async def test_operations_empty_list(self, client: AsyncClient): |
| 21 | + data_atomic_request = { |
| 22 | + "atomic:operations": [], |
| 23 | + } |
| 24 | + response = await client.post("/operations", json=data_atomic_request) |
| 25 | + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY, response.text |
| 26 | + assert response.json() == { |
| 27 | + # TODO: JSON:API exception! |
| 28 | + "detail": [ |
| 29 | + { |
| 30 | + "loc": ["body", "atomic:operations"], |
| 31 | + "msg": "ensure this value has at least 1 items", |
| 32 | + "type": "value_error.list.min_items", |
| 33 | + "ctx": {"limit_value": 1}, |
| 34 | + }, |
| 35 | + ], |
| 36 | + } |
| 37 | + |
| 38 | + async def test_create_one_object( |
| 39 | + self, |
| 40 | + client: AsyncClient, |
| 41 | + async_session: AsyncSession, |
| 42 | + ): |
| 43 | + user = UserAttributesBaseSchema( |
| 44 | + name=fake.name(), |
| 45 | + age=fake.pyint(min_value=13, max_value=99), |
| 46 | + email=fake.email(), |
| 47 | + ) |
| 48 | + data_atomic_request = { |
| 49 | + "atomic:operations": [ |
| 50 | + { |
| 51 | + "op": "add", |
| 52 | + "data": { |
| 53 | + "type": "user", |
| 54 | + "attributes": user.dict(), |
| 55 | + }, |
| 56 | + }, |
| 57 | + ], |
| 58 | + } |
| 59 | + response = await client.post("/operations", json=data_atomic_request) |
| 60 | + assert response.status_code == status.HTTP_200_OK, response.text |
| 61 | + response_data = response.json() |
| 62 | + assert "atomic:results" in response_data, response_data |
| 63 | + results = response_data["atomic:results"] |
| 64 | + assert results, results |
| 65 | + result: dict = results[0] |
| 66 | + stmt = select(User).where( |
| 67 | + User.name == user.name, |
| 68 | + User.age == user.age, |
| 69 | + User.email == user.email, |
| 70 | + ) |
| 71 | + db_result: Result = await async_session.execute(stmt) |
| 72 | + user_obj: User = db_result.scalar_one() |
| 73 | + assert result.pop("meta") is None |
| 74 | + assert result == { |
| 75 | + "data": { |
| 76 | + "attributes": UserAttributesBaseSchema.from_orm(user_obj).dict(), |
| 77 | + "id": str(user_obj.id), |
| 78 | + "type": "user", |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + async def test_create_two_objects( |
| 83 | + self, |
| 84 | + client: AsyncClient, |
| 85 | + async_session: AsyncSession, |
| 86 | + ): |
| 87 | + user_data_1 = UserAttributesBaseSchema( |
| 88 | + name=fake.name(), |
| 89 | + age=fake.pyint(min_value=13, max_value=99), |
| 90 | + email=fake.email(), |
| 91 | + ) |
| 92 | + user_data_2 = UserAttributesBaseSchema( |
| 93 | + name=fake.name(), |
| 94 | + age=fake.pyint(min_value=13, max_value=99), |
| 95 | + email=fake.email(), |
| 96 | + ) |
| 97 | + users_data = [user_data_1, user_data_2] |
| 98 | + data_atomic_request = { |
| 99 | + "atomic:operations": [ |
| 100 | + { |
| 101 | + "op": "add", |
| 102 | + "data": { |
| 103 | + "type": "user", |
| 104 | + "attributes": user_data.dict(), |
| 105 | + }, |
| 106 | + } |
| 107 | + for user_data in users_data |
| 108 | + ], |
| 109 | + } |
| 110 | + response = await client.post("/operations", json=data_atomic_request) |
| 111 | + assert response.status_code == status.HTTP_200_OK, response.text |
| 112 | + response_data = response.json() |
| 113 | + assert "atomic:results" in response_data, response_data |
| 114 | + results = response_data["atomic:results"] |
| 115 | + assert results, results |
| 116 | + stmt = select(User).where( |
| 117 | + or_( |
| 118 | + and_( |
| 119 | + User.name == user_data.name, |
| 120 | + User.age == user_data.age, |
| 121 | + User.email == user_data.email, |
| 122 | + ) |
| 123 | + for user_data in users_data |
| 124 | + ), |
| 125 | + ) |
| 126 | + db_result: Result = await async_session.execute(stmt) |
| 127 | + users: list[User] = db_result.scalars().all() |
| 128 | + assert len(users) == len(results) |
| 129 | + for result, user in zip(results, users): |
| 130 | + assert result.pop("meta") is None |
| 131 | + assert result == { |
| 132 | + "data": { |
| 133 | + "attributes": UserAttributesBaseSchema.from_orm(user).dict(), |
| 134 | + "id": str(user.id), |
| 135 | + "type": "user", |
| 136 | + }, |
| 137 | + } |
0 commit comments