Skip to content

Commit

Permalink
feat(spanner): exporting transaction._rolled_back as transaction.roll…
Browse files Browse the repository at this point in the history
…ed_back (#16)

Co-authored-by: larkee <31196561+larkee@users.noreply.github.com>
  • Loading branch information
HemangChothani and larkee committed Feb 18, 2020
1 parent 74577c7 commit 974ee92
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion google/cloud/spanner_v1/pool.py
Expand Up @@ -503,7 +503,7 @@ def put(self, session):
raise queue.Full

txn = session._transaction
if txn is None or txn.committed or txn._rolled_back:
if txn is None or txn.committed or txn.rolled_back:
session.transaction()
self._pending_sessions.put(session)
else:
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/spanner_v1/session.py
Expand Up @@ -255,7 +255,7 @@ def transaction(self):
raise ValueError("Session has not been created.")

if self._transaction is not None:
self._transaction._rolled_back = True
self._transaction.rolled_back = True
del self._transaction

txn = self._transaction = Transaction(self)
Expand Down
8 changes: 4 additions & 4 deletions google/cloud/spanner_v1/transaction.py
Expand Up @@ -36,7 +36,7 @@ class Transaction(_SnapshotBase, _BatchBase):

committed = None
"""Timestamp at which the transaction was successfully committed."""
_rolled_back = False
rolled_back = False
_multi_use = True
_execute_sql_count = 0

Expand All @@ -58,7 +58,7 @@ def _check_state(self):
if self.committed is not None:
raise ValueError("Transaction is already committed")

if self._rolled_back:
if self.rolled_back:
raise ValueError("Transaction is already rolled back")

def _make_txn_selector(self):
Expand All @@ -85,7 +85,7 @@ def begin(self):
if self.committed is not None:
raise ValueError("Transaction already committed")

if self._rolled_back:
if self.rolled_back:
raise ValueError("Transaction is already rolled back")

database = self._session._database
Expand All @@ -105,7 +105,7 @@ def rollback(self):
api = database.spanner_api
metadata = _metadata_with_prefix(database.name)
api.rollback(self._session.name, self._transaction_id, metadata=metadata)
self._rolled_back = True
self.rolled_back = True
del self._session._transaction

def commit(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_pool.py
Expand Up @@ -837,7 +837,7 @@ def _make_transaction(*args, **kw):

txn = mock.create_autospec(Transaction)(*args, **kw)
txn.committed = None
txn._rolled_back = False
txn.rolled_back = False
return txn


Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_session.py
Expand Up @@ -463,7 +463,7 @@ def test_transaction_w_existing_txn(self):
another = session.transaction() # invalidates existing txn

self.assertIs(session._transaction, another)
self.assertTrue(existing._rolled_back)
self.assertTrue(existing.rolled_back)

def test_run_in_transaction_callback_raises_non_gax_error(self):
from google.cloud.spanner_v1.proto.transaction_pb2 import (
Expand Down Expand Up @@ -506,7 +506,7 @@ def unit_of_work(txn, *args, **kw):
txn, args, kw = called_with[0]
self.assertIsInstance(txn, Transaction)
self.assertIsNone(txn.committed)
self.assertTrue(txn._rolled_back)
self.assertTrue(txn.rolled_back)
self.assertEqual(args, ())
self.assertEqual(kw, {})

Expand Down Expand Up @@ -561,7 +561,7 @@ def unit_of_work(txn, *args, **kw):
txn, args, kw = called_with[0]
self.assertIsInstance(txn, Transaction)
self.assertIsNone(txn.committed)
self.assertFalse(txn._rolled_back)
self.assertFalse(txn.rolled_back)
self.assertEqual(args, ())
self.assertEqual(kw, {})

Expand Down
16 changes: 8 additions & 8 deletions tests/unit/test_transaction.py
Expand Up @@ -76,7 +76,7 @@ def test_ctor_defaults(self):
self.assertIs(transaction._session, session)
self.assertIsNone(transaction._transaction_id)
self.assertIsNone(transaction.committed)
self.assertFalse(transaction._rolled_back)
self.assertFalse(transaction.rolled_back)
self.assertTrue(transaction._multi_use)
self.assertEqual(transaction._execute_sql_count, 0)

Expand All @@ -98,7 +98,7 @@ def test__check_state_already_rolled_back(self):
session = _Session()
transaction = self._make_one(session)
transaction._transaction_id = self.TRANSACTION_ID
transaction._rolled_back = True
transaction.rolled_back = True
with self.assertRaises(ValueError):
transaction._check_state()

Expand All @@ -125,7 +125,7 @@ def test_begin_already_begun(self):
def test_begin_already_rolled_back(self):
session = _Session()
transaction = self._make_one(session)
transaction._rolled_back = True
transaction.rolled_back = True
with self.assertRaises(ValueError):
transaction.begin()

Expand Down Expand Up @@ -187,7 +187,7 @@ def test_rollback_already_rolled_back(self):
session = _Session()
transaction = self._make_one(session)
transaction._transaction_id = self.TRANSACTION_ID
transaction._rolled_back = True
transaction.rolled_back = True
with self.assertRaises(ValueError):
transaction.rollback()

Expand All @@ -203,7 +203,7 @@ def test_rollback_w_other_error(self):
with self.assertRaises(RuntimeError):
transaction.rollback()

self.assertFalse(transaction._rolled_back)
self.assertFalse(transaction.rolled_back)

def test_rollback_ok(self):
from google.protobuf.empty_pb2 import Empty
Expand All @@ -218,7 +218,7 @@ def test_rollback_ok(self):

transaction.rollback()

self.assertTrue(transaction._rolled_back)
self.assertTrue(transaction.rolled_back)
self.assertIsNone(session._transaction)

session_id, txn_id, metadata = api._rolled_back
Expand All @@ -244,7 +244,7 @@ def test_commit_already_rolled_back(self):
session = _Session()
transaction = self._make_one(session)
transaction._transaction_id = self.TRANSACTION_ID
transaction._rolled_back = True
transaction.rolled_back = True
with self.assertRaises(ValueError):
transaction.commit()

Expand Down Expand Up @@ -546,7 +546,7 @@ def test_context_mgr_failure(self):
raise Exception("bail out")

self.assertEqual(transaction.committed, None)
self.assertTrue(transaction._rolled_back)
self.assertTrue(transaction.rolled_back)
self.assertEqual(len(transaction._mutations), 1)

self.assertEqual(api._committed, None)
Expand Down

0 comments on commit 974ee92

Please sign in to comment.