Skip to content

Commit c401bbc

Browse files
committed
Moves Dataset.allocate_ids to connection module.
Addresses fifth part of #477.
1 parent d171d44 commit c401bbc

File tree

8 files changed

+73
-91
lines changed

8 files changed

+73
-91
lines changed

gcloud/datastore/__init__.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
from gcloud import credentials
5050
from gcloud.datastore import _implicit_environ
51-
from gcloud.datastore.connection import Connection
51+
from gcloud.datastore import connection as connection_module
5252

5353

5454
SCOPE = ('https://www.googleapis.com/auth/datastore ',
@@ -104,7 +104,7 @@ def get_connection():
104104
"""
105105
implicit_credentials = credentials.get_credentials()
106106
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
107-
return Connection(credentials=scoped_credentials)
107+
return connection_module.Connection(credentials=scoped_credentials)
108108

109109

110110
def get_dataset(dataset_id):
@@ -156,18 +156,3 @@ def get_entities(keys):
156156
:returns: The requested entities.
157157
"""
158158
return _require_dataset().get_entities(keys)
159-
160-
161-
def allocate_ids(incomplete_key, num_ids):
162-
"""Allocates a list of IDs from a partial key.
163-
164-
:type incomplete_key: A :class:`gcloud.datastore.key.Key`
165-
:param incomplete_key: The partial key to use as base for allocated IDs.
166-
167-
:type num_ids: A :class:`int`.
168-
:param num_ids: The number of IDs to allocate.
169-
170-
:rtype: list of :class:`gcloud.datastore.key.Key`
171-
:returns: The (complete) keys allocated with `incomplete_key` as root.
172-
"""
173-
return _require_dataset().allocate_ids(incomplete_key, num_ids)

gcloud/datastore/connection.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
"""Connections to gcloud datastore API servers."""
1616

17-
from gcloud import connection
17+
from gcloud import connection as base_connection
1818
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
1919
from gcloud.datastore import helpers
2020
from gcloud.datastore.dataset import Dataset
2121

2222

23-
class Connection(connection.Connection):
23+
class Connection(base_connection.Connection):
2424
"""A connection to the Google Cloud Datastore via the Protobuf API.
2525
2626
This class should understand only the basic types (and protobufs)
@@ -125,7 +125,7 @@ def build_api_url(cls, dataset_id, method, base_url=None,
125125
api_version=(api_version or cls.API_VERSION),
126126
dataset_id=dataset_id, method=method)
127127

128-
def transaction(self, transaction=connection.Connection._EMPTY):
128+
def transaction(self, transaction=base_connection.Connection._EMPTY):
129129
"""Getter/setter for the connection's transaction object.
130130
131131
:type transaction: :class:`gcloud.datastore.transaction.Transaction`,
@@ -575,3 +575,35 @@ def _copy_deferred_keys(lookup_request, lookup_response):
575575
lookup_request.key.remove(old_key)
576576
for def_key in lookup_response.deferred:
577577
lookup_request.key.add().CopyFrom(def_key)
578+
579+
580+
def allocate_ids(incomplete_key, num_ids, connection, dataset_id):
581+
"""Allocates a list of IDs from a partial key.
582+
583+
:type incomplete_key: A :class:`gcloud.datastore.key.Key`
584+
:param incomplete_key: Partial key to use as base for allocated IDs.
585+
586+
:type num_ids: A :class:`int`.
587+
:param num_ids: The number of IDs to allocate.
588+
589+
:type connection: :class:`gcloud.datastore.connection.Connection`
590+
:param connection: The connection used to allocate IDs.
591+
592+
:type dataset_id: :class:`str`.
593+
:param dataset_id: The ID of the dataset used to allocate.
594+
595+
:rtype: list of :class:`gcloud.datastore.key.Key`
596+
:returns: The (complete) keys allocated with `incomplete_key` as root.
597+
:raises: `ValueError` if `incomplete_key` is not a partial key.
598+
"""
599+
if not incomplete_key.is_partial:
600+
raise ValueError(('Key is not partial.', incomplete_key))
601+
602+
incomplete_key_pb = incomplete_key.to_protobuf()
603+
incomplete_key_pbs = [incomplete_key_pb] * num_ids
604+
605+
allocated_key_pbs = connection.allocate_ids(dataset_id, incomplete_key_pbs)
606+
allocated_ids = [allocated_key_pb.path_element[-1].id
607+
for allocated_key_pb in allocated_key_pbs]
608+
return [incomplete_key.completed_key(allocated_id)
609+
for allocated_id in allocated_ids]

gcloud/datastore/dataset.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,3 @@ def get_entities(self, keys, missing=None, deferred=None):
112112
entities.append(helpers.entity_from_protobuf(
113113
entity_pb, dataset=self))
114114
return entities
115-
116-
def allocate_ids(self, incomplete_key, num_ids):
117-
"""Allocates a list of IDs from a partial key.
118-
119-
:type incomplete_key: A :class:`gcloud.datastore.key.Key`
120-
:param incomplete_key: Partial key to use as base for allocated IDs.
121-
122-
:type num_ids: A :class:`int`.
123-
:param num_ids: The number of IDs to allocate.
124-
125-
:rtype: list of :class:`gcloud.datastore.key.Key`
126-
:returns: The (complete) keys allocated with `incomplete_key` as root.
127-
:raises: `ValueError` if `incomplete_key` is not a partial key.
128-
"""
129-
if not incomplete_key.is_partial:
130-
raise ValueError(('Key is not partial.', incomplete_key))
131-
132-
incomplete_key_pb = incomplete_key.to_protobuf()
133-
incomplete_key_pbs = [incomplete_key_pb] * num_ids
134-
135-
allocated_key_pbs = self.connection().allocate_ids(
136-
self.id(), incomplete_key_pbs)
137-
allocated_ids = [allocated_key_pb.path_element[-1].id
138-
for allocated_key_pb in allocated_key_pbs]
139-
return [incomplete_key.completed_key(allocated_id)
140-
for allocated_id in allocated_ids]

gcloud/datastore/test___init__.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,3 @@ def test_get_entities(self):
181181
with _Monkey(_implicit_environ, DATASET=CUSTOM_DATASET):
182182
result = gcloud.datastore.get_entities(DUMMY_KEYS)
183183
self.assertTrue(result == DUMMY_VALS)
184-
185-
def test_allocate_ids(self):
186-
import gcloud.datastore
187-
from gcloud.datastore import _implicit_environ
188-
from gcloud.datastore.key import Key
189-
from gcloud.datastore.test_entity import _Dataset
190-
from gcloud._testing import _Monkey
191-
192-
CUSTOM_DATASET = _Dataset()
193-
NUM_IDS = 2
194-
with _Monkey(_implicit_environ, DATASET=CUSTOM_DATASET):
195-
INCOMPLETE_KEY = Key('KIND')
196-
result = gcloud.datastore.allocate_ids(INCOMPLETE_KEY, NUM_IDS)
197-
198-
# Check the IDs returned.
199-
self.assertEqual([key.id for key in result], range(1, NUM_IDS + 1))

gcloud/datastore/test_connection.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,38 @@ def mutation(self):
11391139
self.assertEqual(len(mutation.delete), 1)
11401140

11411141

1142+
class Test_allocate_ids_function(unittest2.TestCase):
1143+
1144+
def _callFUT(self, incomplete_key, num_ids, connection, dataset_id):
1145+
from gcloud.datastore.connection import allocate_ids
1146+
return allocate_ids(incomplete_key, num_ids, connection, dataset_id)
1147+
1148+
def test_allocate_ids(self):
1149+
from gcloud.datastore.key import Key
1150+
from gcloud.datastore.test_dataset import _Connection
1151+
1152+
DATASET_ID = 'DATASET'
1153+
INCOMPLETE_KEY = Key('KIND', dataset_id=DATASET_ID)
1154+
CONNECTION = _Connection()
1155+
NUM_IDS = 2
1156+
result = self._callFUT(INCOMPLETE_KEY, NUM_IDS,
1157+
CONNECTION, DATASET_ID)
1158+
1159+
# Check the IDs returned match.
1160+
self.assertEqual([key.id for key in result], range(NUM_IDS))
1161+
1162+
# Check connection is called correctly.
1163+
self.assertEqual(CONNECTION._called_dataset_id, DATASET_ID)
1164+
self.assertEqual(len(CONNECTION._called_key_pbs), NUM_IDS)
1165+
1166+
def test_allocate_ids_with_complete(self):
1167+
from gcloud.datastore.test_entity import _Key
1168+
1169+
COMPLETE_KEY = _Key()
1170+
self.assertRaises(ValueError, self._callFUT,
1171+
COMPLETE_KEY, 2, None, None)
1172+
1173+
11421174
class Http(object):
11431175

11441176
_called_with = None

gcloud/datastore/test_dataset.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -108,31 +108,6 @@ def test_get_entities_hit(self):
108108
self.assertEqual(list(result), ['foo'])
109109
self.assertEqual(result['foo'], 'Foo')
110110

111-
def test_allocate_ids(self):
112-
from gcloud.datastore.key import Key
113-
114-
DATASET_ID = 'DATASET'
115-
INCOMPLETE_KEY = Key('KIND', dataset_id=DATASET_ID)
116-
CONNECTION = _Connection()
117-
NUM_IDS = 2
118-
DATASET = self._makeOne(DATASET_ID, connection=CONNECTION)
119-
result = DATASET.allocate_ids(INCOMPLETE_KEY, NUM_IDS)
120-
121-
# Check the IDs returned match.
122-
self.assertEqual([key.id for key in result], range(NUM_IDS))
123-
124-
# Check connection is called correctly.
125-
self.assertEqual(CONNECTION._called_dataset_id, DATASET_ID)
126-
self.assertEqual(len(CONNECTION._called_key_pbs), NUM_IDS)
127-
128-
def test_allocate_ids_with_complete(self):
129-
from gcloud.datastore.test_entity import _Key
130-
131-
COMPLETE_KEY = _Key()
132-
DATASET = self._makeOne(None)
133-
self.assertRaises(ValueError, DATASET.allocate_ids,
134-
COMPLETE_KEY, 2)
135-
136111

137112
class _Connection(object):
138113
_called_with = None

gcloud/datastore/test_entity.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,6 @@ def connection(self):
274274
def get_entities(self, keys):
275275
return [self.get(key) for key in keys]
276276

277-
def allocate_ids(self, incomplete_key, num_ids):
278-
return [incomplete_key.completed_key(i + 1) for i in range(num_ids)]
279-
280277

281278
class _Connection(object):
282279
_transaction = _saved = _deleted = None

regression/datastore.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import unittest2
1919

2020
from gcloud import datastore
21+
from gcloud.datastore.connection import allocate_ids
2122
from gcloud.datastore.entity import Entity
2223
from gcloud.datastore.key import Key
2324
from gcloud.datastore.query import Query
@@ -48,7 +49,9 @@ class TestDatastoreAllocateIDs(TestDatastore):
4849
def test_allocate_ids(self):
4950
incomplete_key = Key('Kind')
5051
num_ids = 10
51-
allocated_keys = datastore.allocate_ids(incomplete_key, num_ids)
52+
connection = datastore.get_connection()
53+
allocated_keys = allocate_ids(incomplete_key, num_ids,
54+
connection, DATASET_ID)
5255
self.assertEqual(len(allocated_keys), num_ids)
5356

5457
unique_ids = set()

0 commit comments

Comments
 (0)