File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed
Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments