Skip to content

Commit 298a2c3

Browse files
committed
atomic action remove object, coverage
1 parent 7a58be1 commit 298a2c3

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

fastapi_jsonapi/atomic/atomic.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ async def view_atomic(
132132
# response.data.id
133133
results.append({"data": response.data})
134134
elif operation.action == "remove":
135-
pass
135+
assert isinstance(operation.view, DetailViewBase)
136+
view: "DetailViewBase" = operation.view
137+
response = await view.process_delete_object(
138+
dl=dl,
139+
obj_id=operation.data.id,
140+
)
141+
results.append({"data": response.data})
136142
else:
137143
msg = f"unknown action {operation.action!r}"
138144
raise ValueError(msg)

fastapi_jsonapi/views/detail_view.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ async def handle_delete_resource(
7575
**extra_view_deps,
7676
):
7777
dl: "BaseDataLayer" = await self.get_data_layer(extra_view_deps)
78+
return await self.process_delete_object(dl=dl, obj_id=obj_id)
7879

80+
async def process_delete_object(
81+
self,
82+
dl: "BaseDataLayer",
83+
obj_id: str,
84+
):
7985
view_kwargs = {dl.url_id_field: obj_id}
8086
db_object = await dl.get_object(view_kwargs=view_kwargs, qs=self.query_params)
8187

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
child_4,
2424
computer_1,
2525
computer_2,
26+
computer_factory,
2627
p1_c1_association,
2728
p1_c2_association,
2829
p2_c1_association,

tests/fixtures/entities.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List
1+
from typing import Awaitable, Callable, List
22

33
from pytest import fixture # noqa
44
from pytest_asyncio import fixture as async_fixture
@@ -171,7 +171,7 @@ def user_1_post_for_comments(user_1_posts: List[Post]) -> Post:
171171
return user_1_posts[0]
172172

173173

174-
@async_fixture
174+
@async_fixture()
175175
async def computer_1(async_session: AsyncSession):
176176
computer = Computer(name="Halo")
177177

@@ -185,7 +185,7 @@ async def computer_1(async_session: AsyncSession):
185185
await async_session.commit()
186186

187187

188-
@async_fixture
188+
@async_fixture()
189189
async def computer_2(async_session: AsyncSession):
190190
computer = Computer(name="Nestor")
191191

@@ -199,6 +199,18 @@ async def computer_2(async_session: AsyncSession):
199199
await async_session.commit()
200200

201201

202+
@async_fixture()
203+
async def computer_factory(async_session: AsyncSession) -> Callable[[str | None], Awaitable[Computer]]:
204+
async def factory(name: str | None = None) -> Computer:
205+
computer = Computer(name=name or fake.word())
206+
async_session.add(computer)
207+
await async_session.commit()
208+
await async_session.refresh(computer)
209+
return computer
210+
211+
return factory
212+
213+
202214
@async_fixture()
203215
async def user_2_comment_for_one_u1_post(async_session: AsyncSession, user_2, user_1_post_for_comments):
204216
post = user_1_post_for_comments
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)