Skip to content

Commit

Permalink
Merge pull request #494 from dhermes/fix-486
Browse files Browse the repository at this point in the history
Remove Transaction dependency on Dataset.
  • Loading branch information
dhermes committed Jan 7, 2015
2 parents 9bafba0 + cea9371 commit f176098
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 131 deletions.
15 changes: 0 additions & 15 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
Acts as a mutable namespace to allow the datastore package to
imply the current dataset and connection from the enviroment.
Also provides a base class for classes in the `datastore` package
which could utilize the implicit enviroment.
"""


Expand All @@ -16,15 +13,3 @@

CONNECTION = None
"""Module global to allow persistent implied connection from enviroment."""


class _DatastoreBase(object):
"""Base for all classes in the datastore package.
Uses the implicit DATASET object as a default dataset attached
to the instances being created. Stores the dataset passed in
on the protected (i.e. non-public) attribute `_dataset`.
"""

def __init__(self, dataset=None):
self._dataset = dataset or DATASET
10 changes: 5 additions & 5 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def mutation(self):
(if one exists) or or a new mutation instance.
"""
if self.transaction():
return self.transaction().mutation()
return self.transaction().mutation
else:
return datastore_pb.Mutation()

Expand Down Expand Up @@ -358,7 +358,7 @@ def commit(self, dataset_id, mutation_pb):

if self.transaction():
request.mode = datastore_pb.CommitRequest.TRANSACTIONAL
request.transaction = self.transaction().id()
request.transaction = self.transaction().id
else:
request.mode = datastore_pb.CommitRequest.NON_TRANSACTIONAL

Expand All @@ -378,11 +378,11 @@ def rollback(self, dataset_id):
:type dataset_id: string
:param dataset_id: The dataset to which the transaction belongs.
"""
if not self.transaction() or not self.transaction().id():
if not self.transaction() or not self.transaction().id:
raise ValueError('No transaction to rollback.')

request = datastore_pb.RollbackRequest()
request.transaction = self.transaction().id()
request.transaction = self.transaction().id
# Nothing to do with this response, so just execute the method.
self._rpc(dataset_id, 'rollback', request,
datastore_pb.RollbackResponse)
Expand Down Expand Up @@ -549,7 +549,7 @@ def _set_read_options(self, request, eventual):
if eventual:
opts.read_consistency = datastore_pb.ReadOptions.EVENTUAL
elif transaction:
opts.transaction = transaction.id()
opts.transaction = transaction.id


def _copy_deferred_keys(lookup_request, lookup_response):
Expand Down
33 changes: 13 additions & 20 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ class Mutation(object):
pass

class Xact(object):
def mutation(self):
return Mutation()
mutation = Mutation()

conn = self._makeOne()
conn.transaction(Xact())
found = conn.mutation()
Expand Down Expand Up @@ -765,8 +765,8 @@ def test_commit_w_transaction(self):
from gcloud.datastore import datastore_v1_pb2 as datastore_pb

class Xact(object):
def id(self):
return 'xact'
id = 'xact'

DATASET_ID = 'DATASET'
key_pb = self._make_key_pb(DATASET_ID)
rsp_pb = datastore_pb.CommitResponse()
Expand Down Expand Up @@ -808,9 +808,8 @@ def test_rollback_wo_existing_transaction(self):
def test_rollback_w_existing_transaction_no_id(self):

class Xact(object):
id = None

def id(self):
return None
DATASET_ID = 'DATASET'
conn = self._makeOne()
conn.transaction(Xact())
Expand All @@ -823,9 +822,8 @@ def test_rollback_ok(self):
TRANSACTION = 'xact'

class Xact(object):
id = TRANSACTION

def id(self):
return TRANSACTION
rsp_pb = datastore_pb.RollbackResponse()
conn = self._makeOne()
conn.transaction(Xact())
Expand Down Expand Up @@ -1038,11 +1036,9 @@ def test_save_entity_wo_transaction_w_auto_id(self):
def test_save_entity_w_transaction(self):
from gcloud.datastore import datastore_v1_pb2 as datastore_pb

mutation = datastore_pb.Mutation()

class Xact(object):
def mutation(self):
return mutation
mutation = datastore_pb.Mutation()

DATASET_ID = 'DATASET'
key_pb = self._make_key_pb(DATASET_ID)
rsp_pb = datastore_pb.CommitResponse()
Expand All @@ -1059,11 +1055,9 @@ def test_save_entity_w_transaction_nested_entity(self):
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
from gcloud.datastore.entity import Entity

mutation = datastore_pb.Mutation()

class Xact(object):
def mutation(self):
return mutation
mutation = datastore_pb.Mutation()

DATASET_ID = 'DATASET'
nested = Entity()
nested['bar'] = u'Bar'
Expand Down Expand Up @@ -1114,11 +1108,9 @@ def test_delete_entities_wo_transaction(self):
def test_delete_entities_w_transaction(self):
from gcloud.datastore import datastore_v1_pb2 as datastore_pb

mutation = datastore_pb.Mutation()

class Xact(object):
def mutation(self):
return mutation
mutation = datastore_pb.Mutation()

DATASET_ID = 'DATASET'
key_pb = self._make_key_pb(DATASET_ID)
rsp_pb = datastore_pb.CommitResponse()
Expand Down Expand Up @@ -1162,6 +1154,7 @@ class Transaction(object):
def __init__(self, id):
self._id = id

@property
def id(self):
return self._id

Expand Down
14 changes: 11 additions & 3 deletions gcloud/datastore/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@

class TestEntity(unittest2.TestCase):

def _getTargetClass(self):
def setUp(self):
from gcloud.datastore import _implicit_environ
from gcloud.datastore.entity import Entity
self._replaced_dataset = _implicit_environ.DATASET
self._replaced_dataset_id = _implicit_environ.DATASET_ID
_implicit_environ.DATASET = _implicit_environ.DATASET_ID = None

def tearDown(self):
from gcloud.datastore import _implicit_environ
_implicit_environ.DATASET = self._replaced_dataset
_implicit_environ.DATASET_ID = self._replaced_dataset_id

_implicit_environ.DATASET = None
def _getTargetClass(self):
from gcloud.datastore.entity import Entity
return Entity

def _makeOne(self, key=None, exclude_from_indexes=()):
Expand Down
4 changes: 3 additions & 1 deletion gcloud/datastore/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class Test_entity_from_protobuf(unittest2.TestCase):
def setUp(self):
from gcloud.datastore import _implicit_environ
self._replaced_dataset = _implicit_environ.DATASET
_implicit_environ.DATASET = None
self._replaced_dataset_id = _implicit_environ.DATASET_ID
_implicit_environ.DATASET = _implicit_environ.DATASET_ID = None

def tearDown(self):
from gcloud.datastore import _implicit_environ
_implicit_environ.DATASET = self._replaced_dataset
_implicit_environ.DATASET_ID = self._replaced_dataset_id

def _callFUT(self, val):
from gcloud.datastore.helpers import entity_from_protobuf
Expand Down
10 changes: 10 additions & 0 deletions gcloud/datastore/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class TestKey(unittest2.TestCase):
def setUp(self):
self._DEFAULT_DATASET = 'DATASET'

from gcloud.datastore import _implicit_environ
self._replaced_dataset = _implicit_environ.DATASET
self._replaced_dataset_id = _implicit_environ.DATASET_ID
_implicit_environ.DATASET = _implicit_environ.DATASET_ID = None

def tearDown(self):
from gcloud.datastore import _implicit_environ
_implicit_environ.DATASET = self._replaced_dataset
_implicit_environ.DATASET_ID = self._replaced_dataset_id

def _getTargetClass(self):
from gcloud.datastore import _implicit_environ
from gcloud.datastore.dataset import Dataset
Expand Down
Loading

0 comments on commit f176098

Please sign in to comment.