@@ -14,12 +14,15 @@ class TestTransaction(tb.ConnectedTestCase):
14
14
15
15
async def test_transaction_regular (self ):
16
16
self .assertIsNone (self .con ._top_xact )
17
+ self .assertFalse (self .con .is_in_transaction ())
17
18
tr = self .con .transaction ()
18
19
self .assertIsNone (self .con ._top_xact )
20
+ self .assertFalse (self .con .is_in_transaction ())
19
21
20
22
with self .assertRaises (ZeroDivisionError ):
21
23
async with tr as with_tr :
22
24
self .assertIs (self .con ._top_xact , tr )
25
+ self .assertTrue (self .con .is_in_transaction ())
23
26
24
27
# We don't return the transaction object from __aenter__,
25
28
# to make it harder for people to use '.rollback()' and
@@ -33,6 +36,7 @@ async def test_transaction_regular(self):
33
36
1 / 0
34
37
35
38
self .assertIsNone (self .con ._top_xact )
39
+ self .assertFalse (self .con .is_in_transaction ())
36
40
37
41
with self .assertRaisesRegex (asyncpg .PostgresError ,
38
42
'"mytab" does not exist' ):
@@ -42,31 +46,39 @@ async def test_transaction_regular(self):
42
46
43
47
async def test_transaction_nested (self ):
44
48
self .assertIsNone (self .con ._top_xact )
49
+ self .assertFalse (self .con .is_in_transaction ())
50
+
45
51
tr = self .con .transaction ()
52
+
46
53
self .assertIsNone (self .con ._top_xact )
54
+ self .assertFalse (self .con .is_in_transaction ())
47
55
48
56
with self .assertRaises (ZeroDivisionError ):
49
57
async with tr :
50
58
self .assertIs (self .con ._top_xact , tr )
59
+ self .assertTrue (self .con .is_in_transaction ())
51
60
52
61
await self .con .execute ('''
53
62
CREATE TABLE mytab (a int);
54
63
''' )
55
64
56
65
async with self .con .transaction ():
57
66
self .assertIs (self .con ._top_xact , tr )
67
+ self .assertTrue (self .con .is_in_transaction ())
58
68
59
69
await self .con .execute ('''
60
70
INSERT INTO mytab (a) VALUES (1), (2);
61
71
''' )
62
72
63
73
self .assertIs (self .con ._top_xact , tr )
74
+ self .assertTrue (self .con .is_in_transaction ())
64
75
65
76
with self .assertRaises (ZeroDivisionError ):
66
77
in_tr = self .con .transaction ()
67
78
async with in_tr :
68
79
69
80
self .assertIs (self .con ._top_xact , tr )
81
+ self .assertTrue (self .con .is_in_transaction ())
70
82
71
83
await self .con .execute ('''
72
84
INSERT INTO mytab (a) VALUES (3), (4);
@@ -85,10 +97,12 @@ async def test_transaction_nested(self):
85
97
self .assertEqual (recs [1 ][0 ], 2 )
86
98
87
99
self .assertIs (self .con ._top_xact , tr )
100
+ self .assertTrue (self .con .is_in_transaction ())
88
101
89
102
1 / 0
90
103
91
104
self .assertIs (self .con ._top_xact , None )
105
+ self .assertFalse (self .con .is_in_transaction ())
92
106
93
107
with self .assertRaisesRegex (asyncpg .PostgresError ,
94
108
'"mytab" does not exist' ):
@@ -98,6 +112,7 @@ async def test_transaction_nested(self):
98
112
99
113
async def test_transaction_interface_errors (self ):
100
114
self .assertIsNone (self .con ._top_xact )
115
+ self .assertFalse (self .con .is_in_transaction ())
101
116
102
117
tr = self .con .transaction (readonly = True , isolation = 'serializable' )
103
118
with self .assertRaisesRegex (asyncpg .InterfaceError ,
@@ -109,13 +124,15 @@ async def test_transaction_interface_errors(self):
109
124
'<asyncpg.Transaction state:rolledback serializable readonly' ))
110
125
111
126
self .assertIsNone (self .con ._top_xact )
127
+ self .assertFalse (self .con .is_in_transaction ())
112
128
113
129
with self .assertRaisesRegex (asyncpg .InterfaceError ,
114
130
'cannot start; .* already rolled back' ):
115
131
async with tr :
116
132
pass
117
133
118
134
self .assertIsNone (self .con ._top_xact )
135
+ self .assertFalse (self .con .is_in_transaction ())
119
136
120
137
tr = self .con .transaction ()
121
138
with self .assertRaisesRegex (asyncpg .InterfaceError ,
@@ -124,6 +141,7 @@ async def test_transaction_interface_errors(self):
124
141
await tr .commit ()
125
142
126
143
self .assertIsNone (self .con ._top_xact )
144
+ self .assertFalse (self .con .is_in_transaction ())
127
145
128
146
tr = self .con .transaction ()
129
147
with self .assertRaisesRegex (asyncpg .InterfaceError ,
@@ -132,6 +150,7 @@ async def test_transaction_interface_errors(self):
132
150
await tr .rollback ()
133
151
134
152
self .assertIsNone (self .con ._top_xact )
153
+ self .assertFalse (self .con .is_in_transaction ())
135
154
136
155
tr = self .con .transaction ()
137
156
with self .assertRaisesRegex (asyncpg .InterfaceError ,
@@ -142,11 +161,13 @@ async def test_transaction_interface_errors(self):
142
161
143
162
async def test_transaction_within_manual_transaction (self ):
144
163
self .assertIsNone (self .con ._top_xact )
164
+ self .assertFalse (self .con .is_in_transaction ())
145
165
146
166
await self .con .execute ('BEGIN' )
147
167
148
168
tr = self .con .transaction ()
149
169
self .assertIsNone (self .con ._top_xact )
170
+ self .assertTrue (self .con .is_in_transaction ())
150
171
151
172
with self .assertRaisesRegex (asyncpg .InterfaceError ,
152
173
'cannot use Connection.transaction' ):
@@ -157,3 +178,4 @@ async def test_transaction_within_manual_transaction(self):
157
178
await self .con .reset ()
158
179
159
180
self .assertIsNone (self .con ._top_xact )
181
+ self .assertFalse (self .con .is_in_transaction ())
0 commit comments