-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathtest_transaction.py
More file actions
314 lines (247 loc) · 9.86 KB
/
test_transaction.py
File metadata and controls
314 lines (247 loc) · 9.86 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import asyncio
from typing import cast
from unittest import TestCase
import pytest
from piccolo.engine.cockroach import CockroachTransaction
from piccolo.engine.sqlite import SQLiteEngine, TransactionType
from piccolo.table import drop_db_tables_sync
from piccolo.utils.sync import run_sync
from tests.base import engines_only
from tests.example_apps.music.tables import Band, Manager
class TestAtomic(TestCase):
def test_error(self):
"""
Make sure queries in a transaction aren't committed if a query fails.
"""
atomic = Band._meta.db.atomic()
atomic.add(
Manager.create_table(),
Band.create_table(),
Band.raw("MALFORMED QUERY ... SHOULD ERROR"),
)
try:
atomic.run_sync()
except Exception:
pass
self.assertTrue(not Band.table_exists().run_sync())
self.assertTrue(not Manager.table_exists().run_sync())
def test_succeeds(self):
"""
Make sure that when atomic is run successfully the database is modified
accordingly.
"""
atomic = Band._meta.db.atomic()
atomic.add(Manager.create_table(), Band.create_table())
atomic.run_sync()
self.assertTrue(Band.table_exists().run_sync())
self.assertTrue(Manager.table_exists().run_sync())
drop_db_tables_sync(Band, Manager)
@engines_only("postgres", "cockroach")
def test_pool(self) -> None:
"""
Make sure atomic works correctly when a connection pool is active.
"""
async def run() -> None:
"""
We have to run this async function, so we can use a connection
pool.
"""
engine = Band._meta.db
await engine.start_connection_pool()
atomic = engine.atomic()
atomic.add(
Manager.create_table(),
Band.create_table(),
)
await atomic.run()
await engine.close_connection_pool()
run_sync(run())
self.assertTrue(Band.table_exists().run_sync())
self.assertTrue(Manager.table_exists().run_sync())
drop_db_tables_sync(Band, Manager)
class TestTransaction(TestCase):
def tearDown(self):
for table in (Band, Manager):
if table.table_exists().run_sync():
table.alter().drop_table().run_sync()
def test_error(self):
"""
Make sure queries in a transaction aren't committed if a query fails.
"""
async def run_transaction():
try:
async with Band._meta.db.transaction():
Manager.create_table()
Band.create_table()
Band.raw("MALFORMED QUERY ... SHOULD ERROR")
except Exception:
pass
asyncio.run(run_transaction())
self.assertTrue(not Band.table_exists().run_sync())
self.assertTrue(not Manager.table_exists().run_sync())
def test_succeeds(self):
async def run_transaction():
async with Band._meta.db.transaction():
await Manager.create_table().run()
await Band.create_table().run()
asyncio.run(run_transaction())
self.assertTrue(Band.table_exists().run_sync())
self.assertTrue(Manager.table_exists().run_sync())
def test_manual_commit(self):
"""
The context manager automatically commits changes, but we also
allow the user to do it manually.
"""
async def run_transaction():
async with Band._meta.db.transaction() as transaction:
await Manager.create_table()
await transaction.commit()
asyncio.run(run_transaction())
self.assertTrue(Manager.table_exists().run_sync())
def test_manual_rollback(self):
"""
The context manager will automatically rollback changes if an exception
is raised, but we also allow the user to do it manually.
"""
async def run_transaction():
async with Band._meta.db.transaction() as transaction:
if isinstance(transaction, CockroachTransaction):
await transaction.autocommit_before_ddl(enabled=False)
await Manager.create_table()
await transaction.rollback()
asyncio.run(run_transaction())
self.assertFalse(Manager.table_exists().run_sync())
@engines_only("postgres")
def test_transaction_id(self):
"""
An extra sanity check, that the transaction id is the same for each
query inside the transaction block.
"""
async def run_transaction():
responses = []
async with Band._meta.db.transaction():
responses.append(
await Manager.raw("SELECT txid_current()").run()
)
responses.append(
await Manager.raw("SELECT txid_current()").run()
)
return [i[0]["txid_current"] for i in responses]
txids = asyncio.run(run_transaction())
self.assertEqual(len(set(txids)), 1)
# Now run it again and make sure the transaction ids differ.
next_txids = asyncio.run(run_transaction())
self.assertNotEqual(txids, next_txids)
class TestTransactionExists(TestCase):
def test_exists(self):
"""
Make sure we can detect when code is within a transaction.
"""
engine = cast(SQLiteEngine, Manager._meta.db)
async def run_inside_transaction():
async with engine.transaction():
return engine.transaction_exists()
self.assertTrue(asyncio.run(run_inside_transaction()))
async def run_outside_transaction():
return engine.transaction_exists()
self.assertFalse(asyncio.run(run_outside_transaction()))
@engines_only("sqlite")
class TestTransactionType(TestCase):
def setUp(self):
Manager.create_table().run_sync()
def tearDown(self):
Manager.alter().drop_table().run_sync()
def test_transaction(self):
"""
With SQLite, we can specify the transaction type. This helps when
we want to do concurrent writes, to avoid locking the database.
https://github.com/piccolo-orm/piccolo/issues/687
"""
engine = cast(SQLiteEngine, Manager._meta.db)
async def run_transaction(name: str):
async with engine.transaction(
transaction_type=TransactionType.immediate
):
# This does a SELECT followed by an INSERT, so is a good test.
# If using TransactionType.deferred it would fail because
# the database will become locked.
await Manager.objects().get_or_create(Manager.name == name)
manager_names = [f"Manager_{i}" for i in range(1, 10)]
async def run_all():
"""
Run all of the transactions concurrently.
"""
await asyncio.gather(
*[run_transaction(name=name) for name in manager_names]
)
asyncio.run(run_all())
# Make sure it all ran effectively.
self.assertListEqual(
Manager.select(Manager.name)
.order_by(Manager.name)
.output(as_list=True)
.run_sync(),
manager_names,
)
def test_atomic(self):
"""
Similar to above, but with ``Atomic``.
"""
engine = cast(SQLiteEngine, Manager._meta.db)
async def run_atomic(name: str):
atomic = engine.atomic(transaction_type=TransactionType.immediate)
atomic.add(Manager.objects().get_or_create(Manager.name == name))
await atomic.run()
manager_names = [f"Manager_{i}" for i in range(1, 10)]
async def run_all():
"""
Run all of the transactions concurrently.
"""
await asyncio.gather(
*[run_atomic(name=name) for name in manager_names]
)
asyncio.run(run_all())
# Make sure it all ran effectively.
self.assertListEqual(
Manager.select(Manager.name)
.order_by(Manager.name)
.output(as_list=True)
.run_sync(),
manager_names,
)
class TestSavepoint(TestCase):
def setUp(self):
Manager.create_table().run_sync()
def tearDown(self):
Manager.alter().drop_table().run_sync()
def test_savepoint(self):
async def run_test():
async with Manager._meta.db.transaction() as transaction:
await Manager.insert(Manager(name="Manager 1"))
savepoint = await transaction.savepoint()
await Manager.insert(Manager(name="Manager 2"))
await savepoint.rollback_to()
run_sync(run_test())
self.assertListEqual(
Manager.select(Manager.name).run_sync(), [{"name": "Manager 1"}]
)
def test_named_savepoint(self):
async def run_test():
async with Manager._meta.db.transaction() as transaction:
await Manager.insert(Manager(name="Manager 1"))
await transaction.savepoint("my_savepoint")
await Manager.insert(Manager(name="Manager 2"))
await transaction.rollback_to("my_savepoint")
run_sync(run_test())
self.assertListEqual(
Manager.select(Manager.name).run_sync(), [{"name": "Manager 1"}]
)
def test_savepoint_sqli_checks(self):
# Added to test the fix for GHSA-xq59-7jf3-rjc6
async def run_test():
async with Manager._meta.db.transaction() as transaction:
await transaction.savepoint(
"my_savepoint; SELECT * FROM Manager"
)
with pytest.raises(ValueError):
run_sync(run_test())