Skip to content

Commit ab18d1e

Browse files
committed
Merge repository delete_many tests
1 parent 63988f2 commit ab18d1e

File tree

3 files changed

+71
-141
lines changed

3 files changed

+71
-141
lines changed

tests/repository/async_/test_delete_many.py

Lines changed: 0 additions & 72 deletions
This file was deleted.

tests/repository/sync/test_delete_many.py

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import pytest
2+
from sqlalchemy import select
3+
4+
5+
async def test_can_delete_by_instance(repository_class, model_class, sa_bind, sync_async_wrapper):
6+
model = model_class(
7+
model_id=1,
8+
name="Someone",
9+
)
10+
model2 = model_class(
11+
model_id=2,
12+
name="SomeoneElse",
13+
)
14+
repo = repository_class(bind=sa_bind, model_class=model_class)
15+
await sync_async_wrapper(repo.save_many({model, model2}))
16+
17+
results = [x for x in await sync_async_wrapper(repo.find())]
18+
assert len(results) == 2
19+
20+
await sync_async_wrapper(repo.delete_many([model]))
21+
results = [x for x in await sync_async_wrapper(repo.find())]
22+
assert len(results) == 1
23+
assert results[0].model_id == 2
24+
assert results[0].name == "SomeoneElse"
25+
26+
27+
async def test_delete_inexistent_raises_exception(
28+
repository_class, model_class, sa_bind, sync_async_wrapper
29+
):
30+
repo = repository_class(bind=sa_bind, model_class=model_class)
31+
32+
results = [x for x in await sync_async_wrapper(repo.find())]
33+
assert len(results) == 0
34+
35+
with pytest.raises(Exception):
36+
await sync_async_wrapper(repo.delete_many([4]))
37+
38+
with pytest.raises(Exception):
39+
await sync_async_wrapper(repo.delete_many(
40+
[
41+
model_class(
42+
model_id=823,
43+
name="Someone",
44+
)
45+
]
46+
))
47+
48+
49+
async def test_relationships_are_respected(
50+
repository_class, model_classes, sa_bind, sync_async_wrapper
51+
):
52+
repo = repository_class(bind=sa_bind, model_class=model_classes[0])
53+
children_repo = repository_class(bind=sa_bind, model_class=model_classes[1])
54+
55+
parent = model_classes[0](
56+
name="A Parent",
57+
)
58+
child = model_classes[1](name="A Child")
59+
child2 = model_classes[1](name="Another Child")
60+
parent.children.append(child)
61+
parent.children.append(child2)
62+
await sync_async_wrapper(repo.save(parent))
63+
64+
retrieved_parent = await sync_async_wrapper(repo.get(parent.model_id))
65+
assert len(retrieved_parent.children) == 2
66+
67+
await sync_async_wrapper(repo.delete_many([retrieved_parent]))
68+
69+
assert len(await sync_async_wrapper(children_repo.find())) == 0
70+
children_retrieve_using_repo = await sync_async_wrapper(children_repo.find())
71+
assert len(children_retrieve_using_repo) == 0

0 commit comments

Comments
 (0)