Skip to content

Commit f8a55f1

Browse files
committed
Changing Transaction constructor to take dataset ID and connection.
Also - Fixed some unfound bugs due to disagreeing code and mocks. - Fixed test pollution of _implicit_environ namespace.
1 parent beafafe commit f8a55f1

File tree

8 files changed

+86
-71
lines changed

8 files changed

+86
-71
lines changed

gcloud/datastore/connection.py

Lines changed: 1 addition & 1 deletion
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

gcloud/datastore/test_connection.py

Lines changed: 8 additions & 14 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()
@@ -1036,11 +1036,9 @@ def test_save_entity_wo_transaction_w_auto_id(self):
10361036
def test_save_entity_w_transaction(self):
10371037
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
10381038

1039-
mutation = datastore_pb.Mutation()
1040-
10411039
class Xact(object):
1042-
def mutation(self):
1043-
return mutation
1040+
mutation = datastore_pb.Mutation()
1041+
10441042
DATASET_ID = 'DATASET'
10451043
key_pb = self._make_key_pb(DATASET_ID)
10461044
rsp_pb = datastore_pb.CommitResponse()
@@ -1057,11 +1055,9 @@ def test_save_entity_w_transaction_nested_entity(self):
10571055
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
10581056
from gcloud.datastore.entity import Entity
10591057

1060-
mutation = datastore_pb.Mutation()
1061-
10621058
class Xact(object):
1063-
def mutation(self):
1064-
return mutation
1059+
mutation = datastore_pb.Mutation()
1060+
10651061
DATASET_ID = 'DATASET'
10661062
nested = Entity()
10671063
nested['bar'] = u'Bar'
@@ -1112,11 +1108,9 @@ def test_delete_entities_wo_transaction(self):
11121108
def test_delete_entities_w_transaction(self):
11131109
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
11141110

1115-
mutation = datastore_pb.Mutation()
1116-
11171111
class Xact(object):
1118-
def mutation(self):
1119-
return mutation
1112+
mutation = datastore_pb.Mutation()
1113+
11201114
DATASET_ID = 'DATASET'
11211115
key_pb = self._make_key_pb(DATASET_ID)
11221116
rsp_pb = datastore_pb.CommitResponse()

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

gcloud/datastore/test_query.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ class TestQuery(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 _getTargetClass(self):
3032
from gcloud.datastore.query import Query
@@ -429,11 +431,13 @@ class TestIterator(unittest2.TestCase):
429431
def setUp(self):
430432
from gcloud.datastore import _implicit_environ
431433
self._replaced_dataset = _implicit_environ.DATASET
432-
_implicit_environ.DATASET = None
434+
self._replaced_dataset_id = _implicit_environ.DATASET_ID
435+
_implicit_environ.DATASET = _implicit_environ.DATASET_ID = None
433436

434437
def tearDown(self):
435438
from gcloud.datastore import _implicit_environ
436439
_implicit_environ.DATASET = self._replaced_dataset
440+
_implicit_environ.DATASET_ID = self._replaced_dataset_id
437441

438442
def _getTargetClass(self):
439443
from gcloud.datastore.query import Iterator

gcloud/datastore/test_transaction.py

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,60 @@ def _getTargetClass(self):
2222

2323
return Transaction
2424

25-
def _makeOne(self, dataset=None):
26-
return self._getTargetClass()(dataset)
25+
def _makeOne(self, dataset_id=None, connection=None):
26+
return self._getTargetClass()(dataset_id=dataset_id,
27+
connection=connection)
28+
29+
def test_ctor_missing_required(self):
30+
from gcloud.datastore import _implicit_environ
31+
32+
self.assertEqual(_implicit_environ.DATASET, None)
33+
34+
with self.assertRaises(ValueError):
35+
self._makeOne()
36+
with self.assertRaises(ValueError):
37+
self._makeOne(dataset_id=object())
38+
with self.assertRaises(ValueError):
39+
self._makeOne(connection=object())
2740

2841
def test_ctor(self):
2942
from gcloud.datastore.datastore_v1_pb2 import Mutation
3043

3144
_DATASET = 'DATASET'
3245
connection = _Connection()
33-
dataset = _Dataset(_DATASET, connection)
34-
xact = self._makeOne(dataset)
46+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
3547
self.assertEqual(xact._dataset_id, _DATASET)
3648
self.assertEqual(xact._connection, connection)
3749
self.assertEqual(xact.id, None)
3850
self.assertTrue(isinstance(xact.mutation, Mutation))
3951
self.assertEqual(len(xact._auto_id_entities), 0)
4052

4153
def test_ctor_with_env(self):
54+
from gcloud._testing import _Monkey
4255
from gcloud.datastore import _implicit_environ
4356

4457
DATASET_ID = 'DATASET'
4558
CONNECTION = _Connection()
46-
DATASET = _Dataset(DATASET_ID, CONNECTION)
4759

48-
_implicit_environ.DATASET = DATASET
60+
with _Monkey(_implicit_environ, DATASET_ID=DATASET_ID,
61+
CONNECTION=CONNECTION):
62+
transaction = self._makeOne()
4963

50-
transaction = self._makeOne(dataset=None)
5164
self.assertEqual(transaction._dataset_id, DATASET_ID)
5265
self.assertEqual(transaction._connection, CONNECTION)
5366

5467
def test_add_auto_id_entity(self):
5568
entity = _Entity()
5669
_DATASET = 'DATASET'
5770
connection = _Connection()
58-
dataset = _Dataset(_DATASET, connection)
59-
xact = self._makeOne(dataset)
71+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
6072
xact.add_auto_id_entity(entity)
6173
self.assertEqual(xact._auto_id_entities, [entity])
6274

6375
def test_begin(self):
6476
_DATASET = 'DATASET'
6577
connection = _Connection(234)
66-
dataset = _Dataset(_DATASET, connection)
67-
xact = self._makeOne(dataset)
78+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
6879
xact.begin()
6980
self.assertEqual(xact.id, 234)
7081
self.assertEqual(connection._begun, _DATASET)
@@ -73,8 +84,7 @@ def test_begin(self):
7384
def test_rollback(self):
7485
_DATASET = 'DATASET'
7586
connection = _Connection(234)
76-
dataset = _Dataset(_DATASET, connection)
77-
xact = self._makeOne(dataset)
87+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
7888
xact.begin()
7989
xact.rollback()
8090
self.assertEqual(xact.id, None)
@@ -84,8 +94,7 @@ def test_rollback(self):
8494
def test_commit_no_auto_ids(self):
8595
_DATASET = 'DATASET'
8696
connection = _Connection(234)
87-
dataset = _Dataset(_DATASET, connection)
88-
xact = self._makeOne(dataset)
97+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
8998
xact._mutation = mutation = object()
9099
xact.begin()
91100
xact.commit()
@@ -100,8 +109,7 @@ def test_commit_w_auto_ids(self):
100109
connection = _Connection(234)
101110
connection._commit_result = _CommitResult(
102111
_make_key(_KIND, _ID, _DATASET))
103-
dataset = _Dataset(_DATASET, connection)
104-
xact = self._makeOne(dataset)
112+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
105113
entity = _Entity()
106114
xact.add_auto_id_entity(entity)
107115
xact._mutation = mutation = object()
@@ -110,13 +118,12 @@ def test_commit_w_auto_ids(self):
110118
self.assertEqual(connection._committed, (_DATASET, mutation))
111119
self.assertTrue(connection._xact is None)
112120
self.assertEqual(xact.id, None)
113-
self.assertEqual(entity._key._path, [{'kind': _KIND, 'id': _ID}])
121+
self.assertEqual(entity.key.path, [{'kind': _KIND, 'id': _ID}])
114122

115123
def test_commit_w_already(self):
116124
_DATASET = 'DATASET'
117125
connection = _Connection(234)
118-
dataset = _Dataset(_DATASET, connection)
119-
xact = self._makeOne(dataset)
126+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
120127
xact._mutation = object()
121128
xact.begin()
122129
connection.transaction(()) # Simulate previous commit via false-ish.
@@ -128,8 +135,7 @@ def test_commit_w_already(self):
128135
def test_context_manager_no_raise(self):
129136
_DATASET = 'DATASET'
130137
connection = _Connection(234)
131-
dataset = _Dataset(_DATASET, connection)
132-
xact = self._makeOne(dataset)
138+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
133139
xact._mutation = mutation = object()
134140
with xact:
135141
self.assertEqual(xact.id, 234)
@@ -144,8 +150,7 @@ class Foo(Exception):
144150
pass
145151
_DATASET = 'DATASET'
146152
connection = _Connection(234)
147-
dataset = _Dataset(_DATASET, connection)
148-
xact = self._makeOne(dataset)
153+
xact = self._makeOne(dataset_id=_DATASET, connection=connection)
149154
xact._mutation = object()
150155
try:
151156
with xact:
@@ -173,19 +178,6 @@ def _make_key(kind, id, dataset_id):
173178
return key
174179

175180

176-
class _Dataset(object):
177-
178-
def __init__(self, id, connection=None):
179-
self._id = id
180-
self._connection = connection
181-
182-
def id(self):
183-
return self._id
184-
185-
def connection(self):
186-
return self._connection
187-
188-
189181
class _Connection(object):
190182
_marker = object()
191183
_begun = _rolled_back = _committed = _xact = None
@@ -221,9 +213,4 @@ class _Entity(object):
221213

222214
def __init__(self):
223215
from gcloud.datastore.key import Key
224-
self._key = Key('KIND', dataset_id='DATASET')
225-
226-
def key(self, key=None):
227-
if key is not None:
228-
self._key = key
229-
return self._key
216+
self.key = Key('KIND', dataset_id='DATASET')

gcloud/datastore/transaction.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,24 @@ class Transaction(object):
121121
... with dataset2.transaction():
122122
... dataset2.entity('Thing').save()
123123
124-
:type dataset: :class:`gcloud.datastore.dataset.Dataset`
125-
:param dataset: The dataset to which this :class:`Transaction` belongs.
124+
:type dataset_id: :class:`str`.
125+
:param dataset_id: The ID of the dataset.
126+
127+
:type connection: :class:`gcloud.datastore.connection.Connection`
128+
:param connection: The connection used to connect to datastore.
129+
130+
:raises: :class:`ValueError` if either a connection or dataset ID
131+
are not set.
126132
"""
127133

128-
def __init__(self, dataset=None):
129-
dataset = dataset or _implicit_environ.DATASET
130-
self._connection = dataset.connection()
131-
self._dataset_id = dataset.id()
134+
def __init__(self, dataset_id=None, connection=None):
135+
self._connection = connection or _implicit_environ.CONNECTION
136+
self._dataset_id = dataset_id or _implicit_environ.DATASET_ID
137+
138+
if self._connection is None or self._dataset_id is None:
139+
raise ValueError('A transaction must have a connection and '
140+
'a dataset ID set.')
141+
132142
self._id = None
133143
self._mutation = datastore_pb.Mutation()
134144
self._auto_id_entities = []
@@ -227,7 +237,7 @@ def commit(self):
227237
for i, entity in enumerate(self._auto_id_entities):
228238
key_pb = result.insert_auto_id_key[i]
229239
new_id = key_pb.path_element[-1].id
230-
entity.key(entity.key().completed_key(new_id))
240+
entity.key = entity.key.completed_key(new_id)
231241

232242
# Tell the connection that the transaction is over.
233243
self.connection.transaction(None)

0 commit comments

Comments
 (0)