Skip to content

Commit 44eed24

Browse files
committed
Merge UOW session behaviour tests
1 parent 1643610 commit 44eed24

File tree

3 files changed

+154
-230
lines changed

3 files changed

+154
-230
lines changed

tests/unit_of_work/async_/test_session_behaviour.py

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

tests/unit_of_work/sync/test_session_behaviour.py

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
from unittest.mock import patch
2+
3+
import pytest
4+
from sqlalchemy.exc import InvalidRequestError
5+
6+
7+
async def test_commit_triggers_based_on_external_uow_context_manager(
8+
sa_bind,
9+
repository_class,
10+
model_classes,
11+
uow_class,
12+
session_handler_class,
13+
sync_async_wrapper,
14+
sync_async_cm_wrapper,
15+
):
16+
class RepoClass(repository_class):
17+
_model = model_classes[0]
18+
19+
class ChildRepoClass(repository_class):
20+
_model = model_classes[1]
21+
22+
repository_classes = [RepoClass, ChildRepoClass]
23+
24+
with patch.object(
25+
session_handler_class, "commit", return_value=None
26+
) as mocked_uow_commit:
27+
uow = uow_class(sa_bind, repository_classes)
28+
repo1 = getattr(uow, repository_classes[0].__name__)
29+
repo2 = getattr(uow, repository_classes[1].__name__)
30+
31+
# Populate a database entry to be used for tests
32+
model1 = model_classes[0](
33+
name="Someone",
34+
)
35+
model2 = model_classes[1](
36+
name="SomeoneElse",
37+
)
38+
async with sync_async_cm_wrapper(uow.transaction()):
39+
await sync_async_wrapper(repo1.save(model1))
40+
await sync_async_wrapper(repo2.save(model2))
41+
42+
assert mocked_uow_commit.call_count == 1
43+
44+
45+
async def test_models_are_persisted_using_external_uow(
46+
sa_bind,
47+
repository_class,
48+
model_classes,
49+
uow_class,
50+
sync_async_wrapper,
51+
sync_async_cm_wrapper,
52+
):
53+
class RepoClass(repository_class):
54+
_model = model_classes[0]
55+
56+
class OtherRepoClass(repository_class):
57+
_model = model_classes[0]
58+
59+
repository_classes = [RepoClass, OtherRepoClass]
60+
uow = uow_class(sa_bind, repository_classes)
61+
repo1 = getattr(uow, repository_classes[0].__name__)
62+
repo2 = getattr(uow, repository_classes[1].__name__)
63+
64+
# Populate a database entry to be used for tests
65+
model1 = model_classes[0](
66+
name="Someone",
67+
)
68+
model2 = model_classes[0](
69+
name="SomeoneElse",
70+
)
71+
async with sync_async_cm_wrapper(uow.transaction()):
72+
await sync_async_wrapper(repo1.save(model1))
73+
await sync_async_wrapper(repo2.save(model2))
74+
75+
assert model1.name is not None
76+
assert model1.model_id is not None
77+
assert model2.model_id is not None
78+
79+
80+
async def test_uow_repository_operations_fail_without_transaction(
81+
sa_bind,
82+
repository_class,
83+
model_classes,
84+
uow_class,
85+
sync_async_wrapper,
86+
):
87+
class RepoClass(repository_class):
88+
_model = model_classes[0]
89+
90+
repository_classes = [RepoClass]
91+
92+
uow = uow_class(sa_bind, repository_classes)
93+
repo1 = getattr(uow, repository_classes[0].__name__)
94+
95+
# Populate a database entry to be used for tests
96+
model1 = model_classes[0](
97+
name="Someone",
98+
)
99+
100+
with pytest.raises(InvalidRequestError):
101+
await sync_async_wrapper(repo1.save(model1))
102+
103+
104+
async def test_models_operations_with_external_session(
105+
sa_bind,
106+
repository_class,
107+
model_classes,
108+
uow_class,
109+
sync_async_wrapper,
110+
sync_async_cm_wrapper,
111+
):
112+
class RepoClass(repository_class):
113+
_model = model_classes[0]
114+
115+
class OtherRepoClass(repository_class):
116+
_model = model_classes[0]
117+
118+
repository_classes = [RepoClass, OtherRepoClass]
119+
120+
uow = uow_class(sa_bind, repository_classes)
121+
repo1 = getattr(uow, repository_classes[0].__name__)
122+
repo2 = getattr(uow, repository_classes[1].__name__)
123+
124+
# Populate a database entry to be used for tests
125+
model1 = model_classes[0](
126+
name="Someone",
127+
)
128+
model2 = model_classes[0](
129+
name="SomeoneElse",
130+
)
131+
async with sync_async_cm_wrapper(uow.transaction()):
132+
await sync_async_wrapper(repo1.save(model1))
133+
await sync_async_wrapper(repo2.save(model2))
134+
135+
model1.name = "SomeoneNew"
136+
model2.name = "SomeoneElseNew"
137+
138+
async with sync_async_cm_wrapper(uow.transaction()):
139+
await sync_async_wrapper(repo1.save(model1))
140+
141+
async with sync_async_cm_wrapper(uow.transaction()):
142+
m1new = await sync_async_wrapper(repo1.get(model1.model_id))
143+
m2new = await sync_async_wrapper(repo2.get(model2.model_id))
144+
145+
assert model1 is not m1new
146+
assert model2 is not m2new
147+
assert m1new.name == "SomeoneNew"
148+
assert m2new.name == "SomeoneElse"
149+
150+
async with sync_async_cm_wrapper(uow.transaction(read_only=True)):
151+
m2new.name = "pippo"
152+
m2updated = await sync_async_wrapper(repo2.get(model2.model_id))
153+
154+
assert m2updated.name != "pippo"

0 commit comments

Comments
 (0)