Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dbapi): autocommit enabling fails if no transactions begun #177

Merged
merged 35 commits into from Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
73f6330
fix(dbapi): autocommit enabling fails if no transactions begun
Nov 30, 2020
632bc20
remove unused import
Nov 30, 2020
9f09bfb
Merge branch 'master' into autocommit_change
c24t Dec 1, 2020
1f8e864
don't calculate checksums in autocommit mode
Dec 3, 2020
0841fef
Merge branch 'autocommit_change' of https://github.com/q-logic/python…
Dec 3, 2020
fcc20ae
try using dummy WHERE clause
Dec 3, 2020
73f91ef
revert where clause
Dec 3, 2020
df9c35f
unveil error
Dec 4, 2020
5f04934
fix where clauses
Dec 4, 2020
6a0ff47
add print
Dec 7, 2020
bb75c34
don't log
Dec 8, 2020
e9e5260
Merge branch 'master' into autocommit_change
c24t Dec 10, 2020
1527a0e
print failed exceptions
Dec 14, 2020
6294e97
Merge branch 'autocommit_change' of https://github.com/q-logic/python…
Dec 14, 2020
dd4e74e
don't print
Dec 14, 2020
092c334
separate insert statements
Dec 15, 2020
5d997fd
don't return
Dec 15, 2020
9e07ec8
re-run
Dec 15, 2020
914930f
don't pyformat insert args
Dec 15, 2020
824d3ff
args
Dec 15, 2020
18e815f
re-run
Dec 15, 2020
7ddd0b5
fix
Dec 15, 2020
96f5df1
Merge branch 'master' into autocommit_change
c24t Dec 15, 2020
705dd1d
Merge branch 'master' into autocommit_change
c24t Dec 17, 2020
51da009
Merge branch 'master' into autocommit_change
larkee Dec 17, 2020
e40ccc6
fix error in transactions.tests.NonAutocommitTests.test_orm_query_wit…
Dec 21, 2020
2b4d830
Merge branch 'autocommit_change' of https://github.com/q-logic/python…
Dec 21, 2020
2124092
fix "already committed" error
Dec 22, 2020
d36becb
Merge branch 'master' into autocommit_change
c24t Dec 22, 2020
6a4e248
fix for AttributeError: 'tuple' object has no attribute 'items'
Dec 23, 2020
9406e68
Merge branch 'autocommit_change' of https://github.com/q-logic/python…
Dec 23, 2020
dbd75ce
fix
Dec 23, 2020
f9306b2
fix KeyError: 'type'
Dec 24, 2020
5fa4dff
Merge branch 'master' into autocommit_change
AVaksman Dec 29, 2020
e50aa83
Merge branch 'master' into autocommit_change
larkee Dec 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion google/cloud/spanner_dbapi/connection.py
Expand Up @@ -82,7 +82,13 @@ def autocommit(self, value):
:type value: bool
:param value: New autocommit mode state.
"""
if value and not self._autocommit:
if (
value
and not self._autocommit
and self._transaction
and not self._transaction.committed
and not self._transaction.rolled_back
):
self.commit()

self._autocommit = value
Expand Down
49 changes: 40 additions & 9 deletions tests/unit/spanner_dbapi/test_connection.py
Expand Up @@ -15,7 +15,6 @@
"""Cloud Spanner DB-API Connection class unit tests."""

import mock
import sys
import unittest
import warnings

Expand Down Expand Up @@ -51,25 +50,57 @@ def _make_connection(self):
database = instance.database(self.DATABASE)
return Connection(instance, database)

@unittest.skipIf(sys.version_info[0] < 3, "Python 2 patching is outdated")
def test_property_autocommit_setter(self):
from google.cloud.spanner_dbapi import Connection

connection = Connection(self.INSTANCE, self.DATABASE)
def test_autocommit_setter_transaction_not_started(self):
connection = self._make_connection()

with mock.patch(
"google.cloud.spanner_dbapi.connection.Connection.commit"
) as mock_commit:
connection.autocommit = True
mock_commit.assert_called_once_with()
self.assertEqual(connection._autocommit, True)
mock_commit.assert_not_called()
self.assertTrue(connection._autocommit)

with mock.patch(
"google.cloud.spanner_dbapi.connection.Connection.commit"
) as mock_commit:
connection.autocommit = False
mock_commit.assert_not_called()
self.assertEqual(connection._autocommit, False)
self.assertFalse(connection._autocommit)

def test_autocommit_setter_transaction_started(self):
connection = self._make_connection()

with mock.patch(
"google.cloud.spanner_dbapi.connection.Connection.commit"
) as mock_commit:
connection._transaction = mock.Mock(committed=False, rolled_back=False)

connection.autocommit = True
mock_commit.assert_called_once()
self.assertTrue(connection._autocommit)

def test_autocommit_setter_transaction_started_commited_rolled_back(self):
connection = self._make_connection()

with mock.patch(
"google.cloud.spanner_dbapi.connection.Connection.commit"
) as mock_commit:
connection._transaction = mock.Mock(committed=True, rolled_back=False)

connection.autocommit = True
mock_commit.assert_not_called()
self.assertTrue(connection._autocommit)

connection.autocommit = False

with mock.patch(
"google.cloud.spanner_dbapi.connection.Connection.commit"
) as mock_commit:
connection._transaction = mock.Mock(committed=False, rolled_back=True)

connection.autocommit = True
mock_commit.assert_not_called()
self.assertTrue(connection._autocommit)

def test_property_database(self):
from google.cloud.spanner_v1.database import Database
Expand Down