Skip to content

Commit

Permalink
Removing serializable option from datastore Transaction.
Browse files Browse the repository at this point in the history
This is in advance of the option being removed in v1beta3.
  • Loading branch information
dhermes committed Dec 16, 2015
1 parent 36b08c1 commit 9ad1f2d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 126 deletions.
9 changes: 2 additions & 7 deletions gcloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,12 @@ def batch(self):
"""
return Batch(self)

def transaction(self, serializable=False):
def transaction(self):
"""Proxy to :class:`gcloud.datastore.transaction.Transaction`.
Passes our ``dataset_id``.
:type serializable: boolean
:param serializable: if true, perform this transaction at
``serializable`` isolation level; otherwise,
perform it at ``snapshot`` level.
"""
return Transaction(self, serializable=serializable)
return Transaction(self)

def query(self, **kwargs):
"""Proxy to :class:`gcloud.datastore.query.Query`.
Expand Down
16 changes: 1 addition & 15 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,34 +267,20 @@ def run_query(self, dataset_id, query_pb, namespace=None,
response.batch.skipped_results,
)

def begin_transaction(self, dataset_id, serializable=False):
def begin_transaction(self, dataset_id):
"""Begin a transaction.
Maps the ``DatastoreService.BeginTransaction`` protobuf RPC.
:type dataset_id: string
:param dataset_id: The ID dataset to which the transaction applies.
:type serializable: boolean
:param serializable: Boolean indicating if the isolation level of the
transaction should be SERIALIZABLE (True) or
SNAPSHOT (False).
:rtype: :class:`._datastore_v1_pb2.BeginTransactionResponse`
:returns': the result protobuf for the begin transaction request.
"""
request = datastore_pb.BeginTransactionRequest()

if serializable:
request.isolation_level = (
datastore_pb.BeginTransactionRequest.SERIALIZABLE)
else:
request.isolation_level = (
datastore_pb.BeginTransactionRequest.SNAPSHOT)

response = self._rpc(dataset_id, 'beginTransaction', request,
datastore_pb.BeginTransactionResponse)

return response.transaction

def commit(self, dataset_id, mutation_pb, transaction_id):
Expand Down
16 changes: 1 addition & 15 deletions gcloud/datastore/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,21 +858,7 @@ def test_transaction_defaults(self):

self.assertTrue(isinstance(xact, _Dummy))
self.assertEqual(xact.args, (client,))
self.assertEqual(xact.kwargs, {'serializable': False})

def test_transaction_explicit(self):
from gcloud.datastore import client as MUT
from gcloud._testing import _Monkey

creds = object()
client = self._makeOne(credentials=creds)

with _Monkey(MUT, Transaction=_Dummy):
xact = client.transaction(serializable=True)

self.assertTrue(isinstance(xact, _Dummy))
self.assertEqual(xact.args, (client,))
self.assertEqual(xact.kwargs, {'serializable': True})
self.assertEqual(xact.kwargs, {})

def test_query_w_client(self):
KIND = 'KIND'
Expand Down
28 changes: 1 addition & 27 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def test_run_query_w_namespace_nonempty_result(self):
self.assertEqual(request.partition_id.namespace, 'NS')
self.assertEqual(request.query, q_pb)

def test_begin_transaction_default_serialize(self):
def test_begin_transaction(self):
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb

DATASET_ID = 'DATASET'
Expand All @@ -658,32 +658,6 @@ def test_begin_transaction_default_serialize(self):
rq_class = datastore_pb.BeginTransactionRequest
request = rq_class()
request.ParseFromString(cw['body'])
self.assertEqual(request.isolation_level, rq_class.SNAPSHOT)

def test_begin_transaction_explicit_serialize(self):
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb

DATASET_ID = 'DATASET'
TRANSACTION = b'TRANSACTION'
rsp_pb = datastore_pb.BeginTransactionResponse()
rsp_pb.transaction = TRANSACTION
conn = self._makeOne()
URI = '/'.join([
conn.api_base_url,
'datastore',
conn.API_VERSION,
'datasets',
DATASET_ID,
'beginTransaction',
])
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
self.assertEqual(conn.begin_transaction(DATASET_ID, True), TRANSACTION)
cw = http._called_with
self._verifyProtobufCall(cw, URI, conn)
rq_class = datastore_pb.BeginTransactionRequest
request = rq_class()
request.ParseFromString(cw['body'])
self.assertEqual(request.isolation_level, rq_class.SERIALIZABLE)

def test_commit_wo_transaction(self):
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb
Expand Down
46 changes: 8 additions & 38 deletions gcloud/datastore/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,6 @@ def test_ctor_defaults(self):
self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
self.assertTrue(isinstance(xact.mutation, Mutation))
self.assertEqual(len(xact._auto_id_entities), 0)
self.assertFalse(xact.serializable)

def test_ctor_explicit(self):
from gcloud.datastore._datastore_v1_pb2 import Mutation

_DATASET = 'DATASET'
connection = _Connection()
client = _Client(_DATASET, connection)
xact = self._makeOne(client, serializable=True)
self.assertEqual(xact.dataset_id, _DATASET)
self.assertEqual(xact.connection, connection)
self.assertEqual(xact.id, None)
self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
self.assertTrue(isinstance(xact.mutation, Mutation))
self.assertEqual(len(xact._auto_id_entities), 0)
self.assertTrue(xact.serializable)

def test_current(self):
from gcloud.datastore.test_client import _NoCommitBatch
Expand Down Expand Up @@ -80,25 +64,14 @@ def test_current(self):
self.assertTrue(xact1.current() is None)
self.assertTrue(xact2.current() is None)

def test_begin_wo_serializable(self):
def test_begin(self):
_DATASET = 'DATASET'
connection = _Connection(234)
client = _Client(_DATASET, connection)
xact = self._makeOne(client)
xact.begin()
self.assertEqual(xact.id, 234)
self.assertEqual(connection._begun[0], _DATASET)
self.assertFalse(connection._begun[1])

def test_begin_w_serializable(self):
_DATASET = 'DATASET'
connection = _Connection(234)
client = _Client(_DATASET, connection)
xact = self._makeOne(client, serializable=True)
xact.begin()
self.assertEqual(xact.id, 234)
self.assertEqual(connection._begun[0], _DATASET)
self.assertTrue(connection._begun[1])
self.assertEqual(connection._begun, _DATASET)

def test_begin_tombstoned(self):
_DATASET = 'DATASET'
Expand All @@ -107,8 +80,7 @@ def test_begin_tombstoned(self):
xact = self._makeOne(client)
xact.begin()
self.assertEqual(xact.id, 234)
self.assertEqual(connection._begun[0], _DATASET)
self.assertFalse(connection._begun[1])
self.assertEqual(connection._begun, _DATASET)

xact.rollback()
self.assertEqual(xact.id, None)
Expand Down Expand Up @@ -162,8 +134,7 @@ def test_context_manager_no_raise(self):
xact._mutation = mutation = object()
with xact:
self.assertEqual(xact.id, 234)
self.assertEqual(connection._begun[0], _DATASET)
self.assertFalse(connection._begun[1])
self.assertEqual(connection._begun, _DATASET)
self.assertEqual(connection._committed, (_DATASET, mutation, 234))
self.assertEqual(xact.id, None)

Expand All @@ -175,13 +146,12 @@ class Foo(Exception):
_DATASET = 'DATASET'
connection = _Connection(234)
client = _Client(_DATASET, connection)
xact = self._makeOne(client, serializable=True)
xact = self._makeOne(client)
xact._mutation = object()
try:
with xact:
self.assertEqual(xact.id, 234)
self.assertEqual(connection._begun[0], _DATASET)
self.assertTrue(connection._begun[1])
self.assertEqual(connection._begun, _DATASET)
raise Foo()
except Foo:
self.assertEqual(xact.id, None)
Expand Down Expand Up @@ -209,8 +179,8 @@ def __init__(self, xact_id=123):
self._xact_id = xact_id
self._commit_result = _CommitResult()

def begin_transaction(self, dataset_id, serializable):
self._begun = (dataset_id, serializable)
def begin_transaction(self, dataset_id):
self._begun = dataset_id
return self._xact_id

def rollback(self, dataset_id, transaction_id):
Expand Down
26 changes: 4 additions & 22 deletions gcloud/datastore/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
class Transaction(Batch):
"""An abstraction representing datastore Transactions.
Transactions can be used to build up a bulk mutuation as well as
provide isolation.
Transactions can be used to build up a bulk mutation and ensure all
or none succeed (transactionally).
For example, the following snippet of code will put the two ``save``
operations (either ``insert_auto_id`` or ``upsert``) into the same
Expand Down Expand Up @@ -86,11 +86,6 @@ class Transaction(Batch):
:type client: :class:`gcloud.datastore.client.Client`
:param client: the client used to connect to datastore.
:type serializable: boolean
:param serializable: if true, perform this transaction at
``serializable`` isolation level; otherwise, perform
it at ``snapshot`` level.
"""

_INITIAL = 0
Expand All @@ -105,11 +100,10 @@ class Transaction(Batch):
_FINISHED = 3
"""Enum value for _FINISHED status of transaction."""

def __init__(self, client, serializable=False):
def __init__(self, client):
super(Transaction, self).__init__(client)
self._id = None
self._status = self._INITIAL
self._serializable = serializable

@property
def id(self):
Expand All @@ -120,17 +114,6 @@ def id(self):
"""
return self._id

@property
def serializable(self):
"""Should this transaction be run at ``serializable`` isolation
:rtype: boolean
:returns: if true, perform this transaction at
``serializable`` isolation level; otherwise, perform
it at ``snapshot`` level.
"""
return self._serializable

def current(self):
"""Return the topmost transaction.
Expand All @@ -155,8 +138,7 @@ def begin(self):
if self._status != self._INITIAL:
raise ValueError('Transaction already started previously.')
self._status = self._IN_PROGRESS
self._id = self.connection.begin_transaction(
self.dataset_id, serializable=self.serializable)
self._id = self.connection.begin_transaction(self.dataset_id)

def rollback(self):
"""Rolls back the current transaction.
Expand Down
4 changes: 2 additions & 2 deletions system_tests/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_post_with_id(self):
def test_post_with_generated_id(self):
self._generic_test_post()

def test_save_multiple_serializable(self):
with CLIENT.transaction(serializable=True) as xact:
def test_save_multiple(self):
with CLIENT.transaction() as xact:
entity1 = self._get_post()
xact.put(entity1)
# Register entity to be deleted.
Expand Down

0 comments on commit 9ad1f2d

Please sign in to comment.