forked from tortoise/tortoise-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transactions.py
More file actions
215 lines (173 loc) · 8.22 KB
/
test_transactions.py
File metadata and controls
215 lines (173 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from tests.testmodels import CharPkModel, Event, Team, Tournament
from tortoise.contrib import test
from tortoise.exceptions import OperationalError, TransactionManagementError
from tortoise.transactions import atomic, in_transaction
class SomeException(Exception):
"""
A very specific exception so as to not accidentally catch another exception.
"""
@atomic()
async def atomic_decorated_func():
tournament = Tournament(name="Test")
await tournament.save()
return tournament
@test.requireCapability(supports_transactions=True)
class TestTransactions(test.TruncationTestCase):
async def test_transactions(self):
with self.assertRaises(SomeException):
async with in_transaction():
tournament = Tournament(name="Test")
await tournament.save()
await Tournament.filter(id=tournament.id).update(name="Updated name")
saved_event = await Tournament.filter(name="Updated name").first()
self.assertEqual(saved_event.id, tournament.id)
raise SomeException("Some error")
saved_event = await Tournament.filter(name="Updated name").first()
self.assertIsNone(saved_event)
async def test_get_or_create_transaction_using_db(self):
async with in_transaction() as connection:
obj = await CharPkModel.get_or_create(id="FooMip", using_db=connection)
self.assertIsNotNone(obj)
await connection.rollback()
obj2 = await CharPkModel.filter(id="FooMip").first()
self.assertIsNone(obj2)
async def test_nested_transactions(self):
async with in_transaction():
tournament = Tournament(name="Test")
await tournament.save()
await Tournament.filter(id=tournament.id).update(name="Updated name")
saved_event = await Tournament.filter(name="Updated name").first()
self.assertEqual(saved_event.id, tournament.id)
with self.assertRaises(SomeException):
async with in_transaction():
tournament = await Tournament.create(name="Nested")
saved_tournament = await Tournament.filter(name="Nested").first()
self.assertEqual(tournament.id, saved_tournament.id)
raise SomeException("Some error")
# TODO: reactive once savepoints are implemented
# saved_event = await Tournament.filter(name="Updated name").first()
# self.assertIsNotNone(saved_event)
not_saved_event = await Tournament.filter(name="Nested").first()
self.assertIsNone(not_saved_event)
async def test_transaction_decorator(self):
@atomic()
async def bound_to_succeed():
tournament = Tournament(name="Test")
await tournament.save()
await Tournament.filter(id=tournament.id).update(name="Updated name")
saved_event = await Tournament.filter(name="Updated name").first()
self.assertEqual(saved_event.id, tournament.id)
return tournament
tournament = await bound_to_succeed()
saved_event = await Tournament.filter(name="Updated name").first()
self.assertEqual(saved_event.id, tournament.id)
async def test_transaction_decorator_defined_before_init(self):
tournament = await atomic_decorated_func()
saved_event = await Tournament.filter(name="Test").first()
self.assertEqual(saved_event.id, tournament.id)
async def test_transaction_decorator_fail(self):
tournament = await Tournament.create(name="Test")
@atomic()
async def bound_to_fall():
saved_event = await Tournament.filter(name="Test").first()
self.assertEqual(saved_event.id, tournament.id)
await Tournament.filter(id=tournament.id).update(name="Updated name")
saved_event = await Tournament.filter(name="Updated name").first()
self.assertEqual(saved_event.id, tournament.id)
raise OperationalError()
with self.assertRaises(OperationalError):
await bound_to_fall()
saved_event = await Tournament.filter(name="Test").first()
self.assertEqual(saved_event.id, tournament.id)
saved_event = await Tournament.filter(name="Updated name").first()
self.assertIsNone(saved_event)
async def test_transaction_with_m2m_relations(self):
async with in_transaction():
tournament = await Tournament.create(name="Test")
event = await Event.create(name="Test event", tournament=tournament)
team = await Team.create(name="Test team")
await event.participants.add(team)
async def test_transaction_exception_1(self):
with self.assertRaises(TransactionManagementError):
async with in_transaction() as connection:
await connection.rollback()
await connection.rollback()
async def test_transaction_exception_2(self):
with self.assertRaises(TransactionManagementError):
async with in_transaction() as connection:
await connection.commit()
await connection.commit()
async def test_insert_await_across_transaction_fail(self):
tournament = Tournament(name="Test")
query = tournament.save() # pylint: disable=E1111
try:
async with in_transaction():
await query
raise KeyError("moo")
except KeyError:
pass
self.assertEqual(await Tournament.all(), [])
async def test_insert_await_across_transaction_success(self):
tournament = Tournament(name="Test")
query = tournament.save() # pylint: disable=E1111
async with in_transaction():
await query
self.assertEqual(await Tournament.all(), [tournament])
async def test_update_await_across_transaction_fail(self):
obj = await Tournament.create(name="Test1")
query = Tournament.filter(id=obj.id).update(name="Test2")
try:
async with in_transaction():
await query
raise KeyError("moo")
except KeyError:
pass
self.assertEqual(
await Tournament.all().values("id", "name"), [{"id": obj.id, "name": "Test1"}]
)
async def test_update_await_across_transaction_success(self):
obj = await Tournament.create(name="Test1")
query = Tournament.filter(id=obj.id).update(name="Test2")
async with in_transaction():
await query
self.assertEqual(
await Tournament.all().values("id", "name"), [{"id": obj.id, "name": "Test2"}]
)
async def test_delete_await_across_transaction_fail(self):
obj = await Tournament.create(name="Test1")
query = Tournament.filter(id=obj.id).delete()
try:
async with in_transaction():
await query
raise KeyError("moo")
except KeyError:
pass
self.assertEqual(
await Tournament.all().values("id", "name"), [{"id": obj.id, "name": "Test1"}]
)
async def test_delete_await_across_transaction_success(self):
obj = await Tournament.create(name="Test1")
query = Tournament.filter(id=obj.id).delete()
async with in_transaction():
await query
self.assertEqual(await Tournament.all(), [])
async def test_select_await_across_transaction_fail(self):
try:
async with in_transaction():
query = Tournament.all().values("name")
await Tournament.create(name="Test1")
result = await query
raise KeyError("moo")
except KeyError:
pass
self.assertEqual(result, [{"name": "Test1"}])
self.assertEqual(await Tournament.all(), [])
async def test_select_await_across_transaction_success(self):
async with in_transaction():
query = Tournament.all().values("id", "name")
obj = await Tournament.create(name="Test1")
result = await query
self.assertEqual(result, [{"id": obj.id, "name": "Test1"}])
self.assertEqual(
await Tournament.all().values("id", "name"), [{"id": obj.id, "name": "Test1"}]
)