Skip to content

Commit 539c418

Browse files
committed
Accomodate regression tests to new query-iterator split.
1 parent d74a02d commit 539c418

2 files changed

Lines changed: 68 additions & 56 deletions

File tree

regression/clear_datastore.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
# This assumes the command is being run via tox hence the
1919
# repository root is the current directory.
20+
from gcloud.datastore.query import Query
2021
from regression import regression_utils
2122
from six.moves import input
2223

@@ -34,12 +35,11 @@
3435

3536
def fetch_keys(dataset, kind, fetch_max=FETCH_MAX, query=None, cursor=None):
3637
if query is None:
37-
query = dataset.query(kind=kind).limit(
38-
fetch_max).projection(['__key__'])
39-
# Make new query with start cursor. Will be ignored if None.
40-
query = query.with_cursor(cursor)
38+
query = Query(kind=kind, dataset=dataset, projection=['__key__'])
4139

42-
entities, cursor, _ = query.fetch_page()
40+
iterator = query.fetch(limit=fetch_max, start_cursor=cursor)
41+
42+
entities, _, cursor = iterator.next_page()
4343
return query, entities, cursor
4444

4545

regression/datastore.py

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import unittest2
1919

2020
from gcloud import datastore
21+
from gcloud.datastore.entity import Entity
22+
from gcloud.datastore.key import Key
23+
from gcloud.datastore.query import Query
24+
from gcloud.datastore.transaction import Transaction
2125
# This assumes the command is being run via tox hence the
2226
# repository root is the current directory.
2327
from regression import populate_datastore
@@ -33,15 +37,15 @@ def setUp(self):
3337
self.case_entities_to_delete = []
3438

3539
def tearDown(self):
36-
with datastore.transaction.Transaction():
40+
with Transaction():
3741
for entity in self.case_entities_to_delete:
3842
entity.delete()
3943

4044

4145
class TestDatastoreAllocateIDs(TestDatastore):
4246

4347
def test_allocate_ids(self):
44-
incomplete_key = datastore.key.Key('Kind')
48+
incomplete_key = Key('Kind')
4549
num_ids = 10
4650
allocated_keys = datastore.allocate_ids(incomplete_key, num_ids)
4751
self.assertEqual(len(allocated_keys), num_ids)
@@ -68,7 +72,7 @@ def _get_post(self, name=None, key_id=None, post_content=None):
6872
'rating': 5.0,
6973
}
7074
# Create an entity with the given content.
71-
entity = datastore.entity.Entity(kind='Post')
75+
entity = Entity(kind='Post')
7276
entity.update(post_content)
7377

7478
# Update the entity key.
@@ -114,7 +118,7 @@ def test_post_with_generated_id(self):
114118
self._generic_test_post()
115119

116120
def test_save_multiple(self):
117-
with datastore.transaction.Transaction():
121+
with Transaction():
118122
entity1 = self._get_post()
119123
entity1.save()
120124
# Register entity to be deleted.
@@ -139,25 +143,26 @@ def test_save_multiple(self):
139143
self.assertEqual(len(matches), 2)
140144

141145
def test_empty_kind(self):
142-
posts = datastore.query.Query(kind='Post').limit(2).fetch()
146+
query = Query(kind='Post')
147+
posts = list(query.fetch(limit=2))
143148
self.assertEqual(posts, [])
144149

145150

146151
class TestDatastoreSaveKeys(TestDatastore):
147152

148153
def test_save_key_self_reference(self):
149-
key = datastore.key.Key('Person', 'name')
150-
entity = datastore.entity.Entity(kind=None).key(key)
154+
key = Key('Person', 'name')
155+
entity = Entity(kind=None).key(key)
151156
entity['fullName'] = u'Full name'
152157
entity['linkedTo'] = key # Self reference.
153158

154159
entity.save()
155160
self.case_entities_to_delete.append(entity)
156161

157-
query = datastore.query.Query(kind='Person').filter(
158-
'linkedTo', '=', key).limit(2)
162+
query = Query(kind='Person')
163+
query.add_filter('linkedTo', '=', key)
159164

160-
stored_persons = query.fetch()
165+
stored_persons = list(query.fetch(limit=2))
161166
self.assertEqual(len(stored_persons), 1)
162167

163168
stored_person = stored_persons[0]
@@ -172,81 +177,85 @@ class TestDatastoreQuery(TestDatastore):
172177
def setUpClass(cls):
173178
super(TestDatastoreQuery, cls).setUpClass()
174179
cls.CHARACTERS = populate_datastore.CHARACTERS
175-
cls.ANCESTOR_KEY = datastore.key.Key(*populate_datastore.ANCESTOR)
180+
cls.ANCESTOR_KEY = Key(*populate_datastore.ANCESTOR)
176181

177182
def _base_query(self):
178-
return datastore.query.Query(kind='Character').ancestor(
179-
self.ANCESTOR_KEY)
183+
return Query(kind='Character', ancestor=self.ANCESTOR_KEY)
180184

181185
def test_limit_queries(self):
182186
limit = 5
183-
query = self._base_query().limit(limit)
187+
query = self._base_query()
184188

185189
# Fetch characters.
186-
character_entities, cursor, _ = query.fetch_page()
190+
iterator = query.fetch(limit=limit)
191+
character_entities, _, cursor = iterator.next_page()
187192
self.assertEqual(len(character_entities), limit)
188193

189194
# Check cursor after fetch.
190195
self.assertTrue(cursor is not None)
191196

192-
# Fetch next batch of characters.
193-
new_query = self._base_query().with_cursor(cursor)
194-
new_character_entities = new_query.fetch()
197+
# Fetch remaining of characters.
198+
new_character_entities = list(iterator)
195199
characters_remaining = len(self.CHARACTERS) - limit
196200
self.assertEqual(len(new_character_entities), characters_remaining)
197201

198202
def test_query_simple_filter(self):
199-
query = self._base_query().filter('appearances', '>=', 20)
203+
query = self._base_query()
204+
query.add_filter('appearances', '>=', 20)
200205
expected_matches = 6
201206
# We expect 6, but allow the query to get 1 extra.
202-
entities = query.fetch(limit=expected_matches + 1)
207+
entities = list(query.fetch(limit=expected_matches + 1))
203208
self.assertEqual(len(entities), expected_matches)
204209

205210
def test_query_multiple_filters(self):
206-
query = self._base_query().filter(
207-
'appearances', '>=', 26).filter('family', '=', 'Stark')
211+
query = self._base_query()
212+
query.add_filter('appearances', '>=', 26)
213+
query.add_filter('family', '=', 'Stark')
208214
expected_matches = 4
209215
# We expect 4, but allow the query to get 1 extra.
210-
entities = query.fetch(limit=expected_matches + 1)
216+
entities = list(query.fetch(limit=expected_matches + 1))
211217
self.assertEqual(len(entities), expected_matches)
212218

213219
def test_ancestor_query(self):
214220
filtered_query = self._base_query()
215221

216222
expected_matches = 8
217223
# We expect 8, but allow the query to get 1 extra.
218-
entities = filtered_query.fetch(limit=expected_matches + 1)
224+
entities = list(filtered_query.fetch(limit=expected_matches + 1))
219225
self.assertEqual(len(entities), expected_matches)
220226

221227
def test_query___key___filter(self):
222-
rickard_key = datastore.key.Key(*populate_datastore.RICKARD)
228+
rickard_key = Key(*populate_datastore.RICKARD)
223229

224-
query = self._base_query().filter('__key__', '=', rickard_key)
230+
query = self._base_query()
231+
query.add_filter('__key__', '=', rickard_key)
225232
expected_matches = 1
226233
# We expect 1, but allow the query to get 1 extra.
227-
entities = query.fetch(limit=expected_matches + 1)
234+
entities = list(query.fetch(limit=expected_matches + 1))
228235
self.assertEqual(len(entities), expected_matches)
229236

230237
def test_ordered_query(self):
231-
query = self._base_query().order('appearances')
238+
query = self._base_query()
239+
query.order = 'appearances'
232240
expected_matches = 8
233241
# We expect 8, but allow the query to get 1 extra.
234-
entities = query.fetch(limit=expected_matches + 1)
242+
entities = list(query.fetch(limit=expected_matches + 1))
235243
self.assertEqual(len(entities), expected_matches)
236244

237245
# Actually check the ordered data returned.
238246
self.assertEqual(entities[0]['name'], self.CHARACTERS[0]['name'])
239247
self.assertEqual(entities[7]['name'], self.CHARACTERS[3]['name'])
240248

241249
def test_projection_query(self):
242-
filtered_query = self._base_query().projection(['name', 'family'])
250+
filtered_query = self._base_query()
251+
filtered_query.projection = ['name', 'family']
243252

244253
# NOTE: There are 9 responses because of Catelyn. She has both
245254
# Stark and Tully as her families, hence occurs twice in
246255
# the results.
247256
expected_matches = 9
248257
# We expect 9, but allow the query to get 1 extra.
249-
entities = filtered_query.fetch(limit=expected_matches + 1)
258+
entities = list(filtered_query.fetch(limit=expected_matches + 1))
250259
self.assertEqual(len(entities), expected_matches)
251260

252261
arya_entity = entities[0]
@@ -278,54 +287,57 @@ def test_projection_query(self):
278287
self.assertEqual(sansa_dict, {'name': 'Sansa', 'family': 'Stark'})
279288

280289
def test_query_paginate_with_offset(self):
281-
query = self._base_query()
290+
page_query = self._base_query()
291+
page_query.order = 'appearances'
282292
offset = 2
283293
limit = 3
284-
page_query = query.offset(offset).limit(limit).order('appearances')
294+
iterator = page_query.fetch(limit=limit, offset=offset)
285295

286296
# Fetch characters.
287-
entities, cursor, _ = page_query.fetch_page()
297+
entities, _, cursor = iterator.next_page()
288298
self.assertEqual(len(entities), limit)
289299
self.assertEqual(entities[0]['name'], 'Robb')
290300
self.assertEqual(entities[1]['name'], 'Bran')
291301
self.assertEqual(entities[2]['name'], 'Catelyn')
292302

293-
# Use cursor to begin next query.
294-
next_query = page_query.with_cursor(cursor).offset(0)
295-
self.assertEqual(next_query.limit(), limit)
296303
# Fetch next set of characters.
297-
entities = next_query.fetch()
304+
new_iterator = page_query.fetch(limit=limit, offset=0,
305+
start_cursor=cursor)
306+
entities = list(new_iterator)
298307
self.assertEqual(len(entities), limit)
299308
self.assertEqual(entities[0]['name'], 'Sansa')
300309
self.assertEqual(entities[1]['name'], 'Jon Snow')
301310
self.assertEqual(entities[2]['name'], 'Arya')
302311

303312
def test_query_paginate_with_start_cursor(self):
304-
query = self._base_query()
313+
page_query = self._base_query()
314+
page_query.order = 'appearances'
315+
limit = 3
305316
offset = 2
306-
limit = 2
307-
page_query = query.offset(offset).limit(limit).order('appearances')
317+
iterator = page_query.fetch(limit=limit, offset=offset)
308318

309319
# Fetch characters.
310-
entities, cursor, _ = page_query.fetch_page()
320+
entities, _, cursor = iterator.next_page()
311321
self.assertEqual(len(entities), limit)
312322

313323
# Use cursor to create a fresh query.
314324
fresh_query = self._base_query()
315-
fresh_query = fresh_query.order('appearances').with_cursor(cursor)
325+
fresh_query.order = 'appearances'
316326

317-
new_entities = fresh_query.fetch()
327+
new_entities = list(fresh_query.fetch(start_cursor=cursor,
328+
limit=limit))
318329
characters_remaining = len(self.CHARACTERS) - limit - offset
319330
self.assertEqual(len(new_entities), characters_remaining)
320-
self.assertEqual(new_entities[0]['name'], 'Catelyn')
321-
self.assertEqual(new_entities[3]['name'], 'Arya')
331+
self.assertEqual(new_entities[0]['name'], 'Sansa')
332+
self.assertEqual(new_entities[2]['name'], 'Arya')
322333

323334
def test_query_group_by(self):
324-
query = self._base_query().group_by(['alive'])
335+
query = self._base_query()
336+
query.group_by = ['alive']
325337

326338
expected_matches = 2
327339
# We expect 2, but allow the query to get 1 extra.
328-
entities = query.fetch(limit=expected_matches + 1)
340+
entities = list(query.fetch(limit=expected_matches + 1))
329341
self.assertEqual(len(entities), expected_matches)
330342

331343
self.assertEqual(entities[0]['name'], 'Catelyn')
@@ -335,11 +347,11 @@ def test_query_group_by(self):
335347
class TestDatastoreTransaction(TestDatastore):
336348

337349
def test_transaction(self):
338-
key = datastore.key.Key('Company', 'Google')
339-
entity = datastore.entity.Entity(kind=None).key(key)
350+
key = Key('Company', 'Google')
351+
entity = Entity(kind=None).key(key)
340352
entity['url'] = u'www.google.com'
341353

342-
with datastore.transaction.Transaction():
354+
with Transaction():
343355
retrieved_entity = datastore.get_entity(key)
344356
if retrieved_entity is None:
345357
entity.save()

0 commit comments

Comments
 (0)