1515"""Cloud Spanner DB-API Connection class unit tests.""" 
1616
1717import  mock 
18- import  sys 
1918import  unittest 
2019import  warnings 
2120
@@ -51,25 +50,57 @@ def _make_connection(self):
5150        database  =  instance .database (self .DATABASE )
5251        return  Connection (instance , database )
5352
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 ()
5955
6056        with  mock .patch (
6157            "google.cloud.spanner_dbapi.connection.Connection.commit" 
6258        ) as  mock_commit :
6359            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 )
6662
6763        with  mock .patch (
6864            "google.cloud.spanner_dbapi.connection.Connection.commit" 
6965        ) as  mock_commit :
7066            connection .autocommit  =  False 
7167            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 )
73104
74105    def  test_property_database (self ):
75106        from  google .cloud .spanner_v1 .database  import  Database 
@@ -166,7 +197,9 @@ def test_commit(self, mock_warn):
166197            connection .commit ()
167198            mock_release .assert_not_called ()
168199
169-         connection ._transaction  =  mock_transaction  =  mock .MagicMock ()
200+         connection ._transaction  =  mock_transaction  =  mock .MagicMock (
201+             rolled_back = False , committed = False 
202+         )
170203        mock_transaction .commit  =  mock_commit  =  mock .MagicMock ()
171204
172205        with  mock .patch (
@@ -316,7 +349,7 @@ def test_run_statement_remember_statements(self):
316349
317350        connection  =  self ._make_connection ()
318351
319-         statement  =  Statement (sql , params , param_types , ResultsChecksum (),)
352+         statement  =  Statement (sql , params , param_types , ResultsChecksum (),  False )
320353        with  mock .patch (
321354            "google.cloud.spanner_dbapi.connection.Connection.transaction_checkout" 
322355        ):
@@ -338,7 +371,7 @@ def test_run_statement_dont_remember_retried_statements(self):
338371
339372        connection  =  self ._make_connection ()
340373
341-         statement  =  Statement (sql , params , param_types , ResultsChecksum (),)
374+         statement  =  Statement (sql , params , param_types , ResultsChecksum (),  False )
342375        with  mock .patch (
343376            "google.cloud.spanner_dbapi.connection.Connection.transaction_checkout" 
344377        ):
@@ -352,7 +385,7 @@ def test_clear_statements_on_commit(self):
352385        cleared, when the transaction is commited. 
353386        """ 
354387        connection  =  self ._make_connection ()
355-         connection ._transaction  =  mock .Mock ()
388+         connection ._transaction  =  mock .Mock (rolled_back = False ,  committed = False )
356389        connection ._statements  =  [{}, {}]
357390
358391        self .assertEqual (len (connection ._statements ), 2 )
@@ -390,7 +423,7 @@ def test_retry_transaction(self):
390423        checksum .consume_result (row )
391424        retried_checkum  =  ResultsChecksum ()
392425
393-         statement  =  Statement ("SELECT 1" , [], {}, checksum ,)
426+         statement  =  Statement ("SELECT 1" , [], {}, checksum ,  False )
394427        connection ._statements .append (statement )
395428
396429        with  mock .patch (
@@ -423,7 +456,7 @@ def test_retry_transaction_checksum_mismatch(self):
423456        checksum .consume_result (row )
424457        retried_checkum  =  ResultsChecksum ()
425458
426-         statement  =  Statement ("SELECT 1" , [], {}, checksum ,)
459+         statement  =  Statement ("SELECT 1" , [], {}, checksum ,  False )
427460        connection ._statements .append (statement )
428461
429462        with  mock .patch (
@@ -453,9 +486,9 @@ def test_commit_retry_aborted_statements(self):
453486        cursor ._checksum  =  ResultsChecksum ()
454487        cursor ._checksum .consume_result (row )
455488
456-         statement  =  Statement ("SELECT 1" , [], {}, cursor ._checksum ,)
489+         statement  =  Statement ("SELECT 1" , [], {}, cursor ._checksum ,  False )
457490        connection ._statements .append (statement )
458-         connection ._transaction  =  mock .Mock ()
491+         connection ._transaction  =  mock .Mock (rolled_back = False ,  committed = False )
459492
460493        with  mock .patch .object (
461494            connection ._transaction , "commit" , side_effect = (Aborted ("Aborted" ), None ),
@@ -507,7 +540,7 @@ def test_retry_aborted_retry(self):
507540        cursor ._checksum  =  ResultsChecksum ()
508541        cursor ._checksum .consume_result (row )
509542
510-         statement  =  Statement ("SELECT 1" , [], {}, cursor ._checksum ,)
543+         statement  =  Statement ("SELECT 1" , [], {}, cursor ._checksum ,  False )
511544        connection ._statements .append (statement )
512545
513546        metadata_mock  =  mock .Mock ()
0 commit comments