Skip to content

Commit f176098

Browse files
committed
Merge pull request #494 from dhermes/fix-486
Remove Transaction dependency on Dataset.
2 parents 9bafba0 + cea9371 commit f176098

File tree

8 files changed

+132
-131
lines changed

8 files changed

+132
-131
lines changed

gcloud/datastore/_implicit_environ.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
33
Acts as a mutable namespace to allow the datastore package to
44
imply the current dataset and connection from the enviroment.
5-
6-
Also provides a base class for classes in the `datastore` package
7-
which could utilize the implicit enviroment.
85
"""
96

107

@@ -16,15 +13,3 @@
1613

1714
CONNECTION = None
1815
"""Module global to allow persistent implied connection from enviroment."""
19-
20-
21-
class _DatastoreBase(object):
22-
"""Base for all classes in the datastore package.
23-
24-
Uses the implicit DATASET object as a default dataset attached
25-
to the instances being created. Stores the dataset passed in
26-
on the protected (i.e. non-public) attribute `_dataset`.
27-
"""
28-
29-
def __init__(self, dataset=None):
30-
self._dataset = dataset or DATASET

gcloud/datastore/connection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def mutation(self):
149149
(if one exists) or or a new mutation instance.
150150
"""
151151
if self.transaction():
152-
return self.transaction().mutation()
152+
return self.transaction().mutation
153153
else:
154154
return datastore_pb.Mutation()
155155

@@ -358,7 +358,7 @@ def commit(self, dataset_id, mutation_pb):
358358

359359
if self.transaction():
360360
request.mode = datastore_pb.CommitRequest.TRANSACTIONAL
361-
request.transaction = self.transaction().id()
361+
request.transaction = self.transaction().id
362362
else:
363363
request.mode = datastore_pb.CommitRequest.NON_TRANSACTIONAL
364364

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

384384
request = datastore_pb.RollbackRequest()
385-
request.transaction = self.transaction().id()
385+
request.transaction = self.transaction().id
386386
# Nothing to do with this response, so just execute the method.
387387
self._rpc(dataset_id, 'rollback', request,
388388
datastore_pb.RollbackResponse)
@@ -549,7 +549,7 @@ def _set_read_options(self, request, eventual):
549549
if eventual:
550550
opts.read_consistency = datastore_pb.ReadOptions.EVENTUAL
551551
elif transaction:
552-
opts.transaction = transaction.id()
552+
opts.transaction = transaction.id
553553

554554

555555
def _copy_deferred_keys(lookup_request, lookup_response):

gcloud/datastore/test_connection.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ class Mutation(object):
203203
pass
204204

205205
class Xact(object):
206-
def mutation(self):
207-
return Mutation()
206+
mutation = Mutation()
207+
208208
conn = self._makeOne()
209209
conn.transaction(Xact())
210210
found = conn.mutation()
@@ -765,8 +765,8 @@ def test_commit_w_transaction(self):
765765
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
766766

767767
class Xact(object):
768-
def id(self):
769-
return 'xact'
768+
id = 'xact'
769+
770770
DATASET_ID = 'DATASET'
771771
key_pb = self._make_key_pb(DATASET_ID)
772772
rsp_pb = datastore_pb.CommitResponse()
@@ -808,9 +808,8 @@ def test_rollback_wo_existing_transaction(self):
808808
def test_rollback_w_existing_transaction_no_id(self):
809809

810810
class Xact(object):
811+
id = None
811812

812-
def id(self):
813-
return None
814813
DATASET_ID = 'DATASET'
815814
conn = self._makeOne()
816815
conn.transaction(Xact())
@@ -823,9 +822,8 @@ def test_rollback_ok(self):
823822
TRANSACTION = 'xact'
824823

825824
class Xact(object):
825+
id = TRANSACTION
826826

827-
def id(self):
828-
return TRANSACTION
829827
rsp_pb = datastore_pb.RollbackResponse()
830828
conn = self._makeOne()
831829
conn.transaction(Xact())
@@ -1038,11 +1036,9 @@ def test_save_entity_wo_transaction_w_auto_id(self):
10381036
def test_save_entity_w_transaction(self):
10391037
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
10401038

1041-
mutation = datastore_pb.Mutation()
1042-
10431039
class Xact(object):
1044-
def mutation(self):
1045-
return mutation
1040+
mutation = datastore_pb.Mutation()
1041+
10461042
DATASET_ID = 'DATASET'
10471043
key_pb = self._make_key_pb(DATASET_ID)
10481044
rsp_pb = datastore_pb.CommitResponse()
@@ -1059,11 +1055,9 @@ def test_save_entity_w_transaction_nested_entity(self):
10591055
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
10601056
from gcloud.datastore.entity import Entity
10611057

1062-
mutation = datastore_pb.Mutation()
1063-
10641058
class Xact(object):
1065-
def mutation(self):
1066-
return mutation
1059+
mutation = datastore_pb.Mutation()
1060+
10671061
DATASET_ID = 'DATASET'
10681062
nested = Entity()
10691063
nested['bar'] = u'Bar'
@@ -1114,11 +1108,9 @@ def test_delete_entities_wo_transaction(self):
11141108
def test_delete_entities_w_transaction(self):
11151109
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
11161110

1117-
mutation = datastore_pb.Mutation()
1118-
11191111
class Xact(object):
1120-
def mutation(self):
1121-
return mutation
1112+
mutation = datastore_pb.Mutation()
1113+
11221114
DATASET_ID = 'DATASET'
11231115
key_pb = self._make_key_pb(DATASET_ID)
11241116
rsp_pb = datastore_pb.CommitResponse()
@@ -1162,6 +1154,7 @@ class Transaction(object):
11621154
def __init__(self, id):
11631155
self._id = id
11641156

1157+
@property
11651158
def id(self):
11661159
return self._id
11671160

gcloud/datastore/test_entity.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121

2222
class TestEntity(unittest2.TestCase):
2323

24-
def _getTargetClass(self):
24+
def setUp(self):
2525
from gcloud.datastore import _implicit_environ
26-
from gcloud.datastore.entity import Entity
26+
self._replaced_dataset = _implicit_environ.DATASET
27+
self._replaced_dataset_id = _implicit_environ.DATASET_ID
28+
_implicit_environ.DATASET = _implicit_environ.DATASET_ID = None
29+
30+
def tearDown(self):
31+
from gcloud.datastore import _implicit_environ
32+
_implicit_environ.DATASET = self._replaced_dataset
33+
_implicit_environ.DATASET_ID = self._replaced_dataset_id
2734

28-
_implicit_environ.DATASET = None
35+
def _getTargetClass(self):
36+
from gcloud.datastore.entity import Entity
2937
return Entity
3038

3139
def _makeOne(self, key=None, exclude_from_indexes=()):

gcloud/datastore/test_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ class Test_entity_from_protobuf(unittest2.TestCase):
2020
def setUp(self):
2121
from gcloud.datastore import _implicit_environ
2222
self._replaced_dataset = _implicit_environ.DATASET
23-
_implicit_environ.DATASET = None
23+
self._replaced_dataset_id = _implicit_environ.DATASET_ID
24+
_implicit_environ.DATASET = _implicit_environ.DATASET_ID = None
2425

2526
def tearDown(self):
2627
from gcloud.datastore import _implicit_environ
2728
_implicit_environ.DATASET = self._replaced_dataset
29+
_implicit_environ.DATASET_ID = self._replaced_dataset_id
2830

2931
def _callFUT(self, val):
3032
from gcloud.datastore.helpers import entity_from_protobuf

gcloud/datastore/test_key.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class TestKey(unittest2.TestCase):
2020
def setUp(self):
2121
self._DEFAULT_DATASET = 'DATASET'
2222

23+
from gcloud.datastore import _implicit_environ
24+
self._replaced_dataset = _implicit_environ.DATASET
25+
self._replaced_dataset_id = _implicit_environ.DATASET_ID
26+
_implicit_environ.DATASET = _implicit_environ.DATASET_ID = None
27+
28+
def tearDown(self):
29+
from gcloud.datastore import _implicit_environ
30+
_implicit_environ.DATASET = self._replaced_dataset
31+
_implicit_environ.DATASET_ID = self._replaced_dataset_id
32+
2333
def _getTargetClass(self):
2434
from gcloud.datastore import _implicit_environ
2535
from gcloud.datastore.dataset import Dataset

0 commit comments

Comments
 (0)