15
15
"""Cloud Spanner DB-API Connection class unit tests."""
16
16
17
17
import mock
18
- import sys
19
18
import unittest
20
19
import warnings
21
20
@@ -51,25 +50,57 @@ def _make_connection(self):
51
50
database = instance .database (self .DATABASE )
52
51
return Connection (instance , database )
53
52
54
- @unittest .skipIf (sys .version_info [0 ] < 3 , "Python 2 patching is outdated" )
55
- def test_property_autocommit_setter (self ):
56
- from google .cloud .spanner_dbapi import Connection
57
-
58
- connection = Connection (self .INSTANCE , self .DATABASE )
53
+ def test_autocommit_setter_transaction_not_started (self ):
54
+ connection = self ._make_connection ()
59
55
60
56
with mock .patch (
61
57
"google.cloud.spanner_dbapi.connection.Connection.commit"
62
58
) as mock_commit :
63
59
connection .autocommit = True
64
- mock_commit .assert_called_once_with ()
65
- self .assertEqual (connection ._autocommit , True )
60
+ mock_commit .assert_not_called ()
61
+ self .assertTrue (connection ._autocommit )
66
62
67
63
with mock .patch (
68
64
"google.cloud.spanner_dbapi.connection.Connection.commit"
69
65
) as mock_commit :
70
66
connection .autocommit = False
71
67
mock_commit .assert_not_called ()
72
- self .assertEqual (connection ._autocommit , False )
68
+ self .assertFalse (connection ._autocommit )
69
+
70
+ def test_autocommit_setter_transaction_started (self ):
71
+ connection = self ._make_connection ()
72
+
73
+ with mock .patch (
74
+ "google.cloud.spanner_dbapi.connection.Connection.commit"
75
+ ) as mock_commit :
76
+ connection ._transaction = mock .Mock (committed = False , rolled_back = False )
77
+
78
+ connection .autocommit = True
79
+ mock_commit .assert_called_once ()
80
+ self .assertTrue (connection ._autocommit )
81
+
82
+ def test_autocommit_setter_transaction_started_commited_rolled_back (self ):
83
+ connection = self ._make_connection ()
84
+
85
+ with mock .patch (
86
+ "google.cloud.spanner_dbapi.connection.Connection.commit"
87
+ ) as mock_commit :
88
+ connection ._transaction = mock .Mock (committed = True , rolled_back = False )
89
+
90
+ connection .autocommit = True
91
+ mock_commit .assert_not_called ()
92
+ self .assertTrue (connection ._autocommit )
93
+
94
+ connection .autocommit = False
95
+
96
+ with mock .patch (
97
+ "google.cloud.spanner_dbapi.connection.Connection.commit"
98
+ ) as mock_commit :
99
+ connection ._transaction = mock .Mock (committed = False , rolled_back = True )
100
+
101
+ connection .autocommit = True
102
+ mock_commit .assert_not_called ()
103
+ self .assertTrue (connection ._autocommit )
73
104
74
105
def test_property_database (self ):
75
106
from google .cloud .spanner_v1 .database import Database
@@ -166,7 +197,9 @@ def test_commit(self, mock_warn):
166
197
connection .commit ()
167
198
mock_release .assert_not_called ()
168
199
169
- connection ._transaction = mock_transaction = mock .MagicMock ()
200
+ connection ._transaction = mock_transaction = mock .MagicMock (
201
+ rolled_back = False , committed = False
202
+ )
170
203
mock_transaction .commit = mock_commit = mock .MagicMock ()
171
204
172
205
with mock .patch (
@@ -316,7 +349,7 @@ def test_run_statement_remember_statements(self):
316
349
317
350
connection = self ._make_connection ()
318
351
319
- statement = Statement (sql , params , param_types , ResultsChecksum (),)
352
+ statement = Statement (sql , params , param_types , ResultsChecksum (), False )
320
353
with mock .patch (
321
354
"google.cloud.spanner_dbapi.connection.Connection.transaction_checkout"
322
355
):
@@ -338,7 +371,7 @@ def test_run_statement_dont_remember_retried_statements(self):
338
371
339
372
connection = self ._make_connection ()
340
373
341
- statement = Statement (sql , params , param_types , ResultsChecksum (),)
374
+ statement = Statement (sql , params , param_types , ResultsChecksum (), False )
342
375
with mock .patch (
343
376
"google.cloud.spanner_dbapi.connection.Connection.transaction_checkout"
344
377
):
@@ -352,7 +385,7 @@ def test_clear_statements_on_commit(self):
352
385
cleared, when the transaction is commited.
353
386
"""
354
387
connection = self ._make_connection ()
355
- connection ._transaction = mock .Mock ()
388
+ connection ._transaction = mock .Mock (rolled_back = False , committed = False )
356
389
connection ._statements = [{}, {}]
357
390
358
391
self .assertEqual (len (connection ._statements ), 2 )
@@ -390,7 +423,7 @@ def test_retry_transaction(self):
390
423
checksum .consume_result (row )
391
424
retried_checkum = ResultsChecksum ()
392
425
393
- statement = Statement ("SELECT 1" , [], {}, checksum ,)
426
+ statement = Statement ("SELECT 1" , [], {}, checksum , False )
394
427
connection ._statements .append (statement )
395
428
396
429
with mock .patch (
@@ -423,7 +456,7 @@ def test_retry_transaction_checksum_mismatch(self):
423
456
checksum .consume_result (row )
424
457
retried_checkum = ResultsChecksum ()
425
458
426
- statement = Statement ("SELECT 1" , [], {}, checksum ,)
459
+ statement = Statement ("SELECT 1" , [], {}, checksum , False )
427
460
connection ._statements .append (statement )
428
461
429
462
with mock .patch (
@@ -453,9 +486,9 @@ def test_commit_retry_aborted_statements(self):
453
486
cursor ._checksum = ResultsChecksum ()
454
487
cursor ._checksum .consume_result (row )
455
488
456
- statement = Statement ("SELECT 1" , [], {}, cursor ._checksum ,)
489
+ statement = Statement ("SELECT 1" , [], {}, cursor ._checksum , False )
457
490
connection ._statements .append (statement )
458
- connection ._transaction = mock .Mock ()
491
+ connection ._transaction = mock .Mock (rolled_back = False , committed = False )
459
492
460
493
with mock .patch .object (
461
494
connection ._transaction , "commit" , side_effect = (Aborted ("Aborted" ), None ),
@@ -507,7 +540,7 @@ def test_retry_aborted_retry(self):
507
540
cursor ._checksum = ResultsChecksum ()
508
541
cursor ._checksum .consume_result (row )
509
542
510
- statement = Statement ("SELECT 1" , [], {}, cursor ._checksum ,)
543
+ statement = Statement ("SELECT 1" , [], {}, cursor ._checksum , False )
511
544
connection ._statements .append (statement )
512
545
513
546
metadata_mock = mock .Mock ()
0 commit comments