1
1
from django .test import TransactionTestCase
2
2
from django .db import transaction
3
3
4
- from django .db .transaction import on_commit
4
+ from django .db .transaction import on_commit , get_connection
5
5
6
- from chamber .utils .transaction import UniquePreCommitCallable , in_atomic_block , pre_commit , smart_atomic
6
+ from chamber .utils .transaction import UniquePreCommitCallable , pre_commit , smart_atomic
7
7
8
8
from test_chamber .models import TestSmartModel
9
9
@@ -19,6 +19,10 @@ def add_number(numbers_list, number):
19
19
numbers_list .append (number )
20
20
21
21
22
+ def raise_exc ():
23
+ raise Exception ()
24
+
25
+
22
26
class TransactionsTestCase (TransactionTestCase ):
23
27
24
28
def test_pre_commit_without_atomic_should_be_called_immediately (self ):
@@ -130,6 +134,23 @@ def pre_commit_fn_c():
130
134
131
135
assert_equal (data , ['a' , 'b' , 'c' ])
132
136
137
+ def test_transaction_is_properly_closed_if_pre_commit_fails (self ):
138
+ numbers_list = []
139
+
140
+ with transaction .atomic ():
141
+ on_commit (lambda : add_number (numbers_list , 2 ))
142
+ pre_commit (lambda : raise_exc ())
143
+ assert_equal (numbers_list , [])
144
+
145
+ # Check that another transaction is functional
146
+ with transaction .atomic ():
147
+ connection = get_connection ()
148
+ with connection .cursor () as cursor :
149
+ cursor .execute ("SELECT 1" )
150
+ on_commit (lambda : add_number (numbers_list , 2 ))
151
+ pre_commit (lambda : add_number (numbers_list , 1 ))
152
+ assert_equal (numbers_list , [1 , 2 ])
153
+
133
154
def test_chamber_atomic_should_ignore_errors (self ):
134
155
with assert_raises (RuntimeError ):
135
156
with smart_atomic ():
0 commit comments