Skip to content

Commit d2bd84d

Browse files
committed
added transaction tests
1 parent 0a65b4b commit d2bd84d

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

aragorm/engine/postgres.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ async def run(self):
2929
for query in self.queries:
3030
await connection.execute(query.__str__())
3131

32+
# In case the transaction object gets reused:
33+
self.queries = []
34+
3235
def run_sync(self):
3336
return asyncio.run(self.run())
3437

tests/engine/test_transaction.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from unittest import TestCase
2+
3+
from ..example_project.tables import Pokemon, Trainer
4+
5+
6+
class TestTransaction(TestCase):
7+
8+
def test_error(self):
9+
"""
10+
Make sure queries in a transaction aren't committed if a query fails.
11+
"""
12+
transaction = Pokemon.Meta.db.transaction()
13+
transaction.add(
14+
Pokemon.create(),
15+
Trainer.create(),
16+
Pokemon.raw('MALFORMED QUERY ... SHOULD ERROR')
17+
)
18+
try:
19+
transaction.run_sync()
20+
except Exception:
21+
pass
22+
self.assertTrue(
23+
not Pokemon.table_exists().run_sync()
24+
)
25+
self.assertTrue(
26+
not Trainer.table_exists().run_sync()
27+
)
28+
29+
def test_succeeds(self):
30+
transaction = Pokemon.Meta.db.transaction()
31+
transaction.add(
32+
Pokemon.create(),
33+
Trainer.create()
34+
)
35+
transaction.run_sync()
36+
37+
self.assertTrue(
38+
Pokemon.table_exists().run_sync()
39+
)
40+
self.assertTrue(
41+
Trainer.table_exists().run_sync()
42+
)
43+
44+
transaction.add(
45+
Pokemon.drop(),
46+
Trainer.drop()
47+
)
48+
transaction.run_sync()

0 commit comments

Comments
 (0)