Skip to content

Commit 6a3b032

Browse files
committed
Merge pull request #1455 from dhermes/query-renames-1288-2
Handling datastore renames on Query and QueryResultBatch.
2 parents 84a3a38 + b27a3f6 commit 6a3b032

File tree

6 files changed

+51
-51
lines changed

6 files changed

+51
-51
lines changed

gcloud/datastore/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def run_query(self, project, query_pb, namespace=None,
268268
response = self._rpc(project, 'runQuery', request,
269269
_datastore_pb2.RunQueryResponse)
270270
return (
271-
[e.entity for e in response.batch.entity_result],
271+
[e.entity for e in response.batch.entity_results],
272272
response.batch.end_cursor, # Assume response always has cursor.
273273
response.batch.more_results,
274274
response.batch.skipped_results,

gcloud/datastore/query.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class Query(object):
5656
:param order: field names used to order query results. Prepend '-'
5757
to a field name to sort it in descending order.
5858
59-
:type group_by: sequence of string
60-
:param group_by: field names used to group query results.
59+
:type distinct_on: sequence of string
60+
:param distinct_on: field names used to group query results.
6161
6262
:raises: ValueError if ``project`` is not passed and no implicit
6363
default is set.
@@ -81,7 +81,7 @@ def __init__(self,
8181
filters=(),
8282
projection=(),
8383
order=(),
84-
group_by=()):
84+
distinct_on=()):
8585

8686
self._client = client
8787
self._kind = kind
@@ -94,7 +94,7 @@ def __init__(self,
9494
self.add_filter(property_name, operator, value)
9595
self._projection = _ensure_tuple_or_list('projection', projection)
9696
self._order = _ensure_tuple_or_list('order', order)
97-
self._group_by = _ensure_tuple_or_list('group_by', group_by)
97+
self._distinct_on = _ensure_tuple_or_list('distinct_on', distinct_on)
9898

9999
@property
100100
def project(self):
@@ -285,15 +285,15 @@ def order(self, value):
285285
self._order[:] = value
286286

287287
@property
288-
def group_by(self):
288+
def distinct_on(self):
289289
"""Names of fields used to group query results.
290290
291291
:rtype: sequence of string
292292
"""
293-
return self._group_by[:]
293+
return self._distinct_on[:]
294294

295-
@group_by.setter
296-
def group_by(self, value):
295+
@distinct_on.setter
296+
def distinct_on(self, value):
297297
"""Set fields used to group query results.
298298
299299
:type value: string or sequence of strings
@@ -302,7 +302,7 @@ def group_by(self, value):
302302
"""
303303
if isinstance(value, str):
304304
value = [value]
305-
self._group_by[:] = value
305+
self._distinct_on[:] = value
306306

307307
def fetch(self, limit=None, offset=0, start_cursor=None, end_cursor=None,
308308
client=None):
@@ -408,7 +408,7 @@ def next_page(self):
408408
pb.end_cursor = base64.urlsafe_b64decode(end_cursor)
409409

410410
if self._limit is not None:
411-
pb.limit = self._limit
411+
pb.limit.value = self._limit
412412

413413
pb.offset = self._offset
414414

@@ -524,7 +524,7 @@ def _pb_from_query(query):
524524
property_order.property.name = prop
525525
property_order.direction = property_order.ASCENDING
526526

527-
for group_by_name in query.group_by:
528-
pb.group_by.add().name = group_by_name
527+
for distinct_on_name in query.distinct_on:
528+
pb.distinct_on.add().name = distinct_on_name
529529

530530
return pb

gcloud/datastore/test_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ def test_query_explicit(self):
909909
FILTERS = [('PROPERTY', '==', 'VALUE')]
910910
PROJECTION = ['__key__']
911911
ORDER = ['PROPERTY']
912-
GROUP_BY = ['GROUPBY']
912+
DISTINCT_ON = ['DISTINCT_ON']
913913

914914
creds = object()
915915
client = self._makeOne(credentials=creds)
@@ -922,7 +922,7 @@ def test_query_explicit(self):
922922
filters=FILTERS,
923923
projection=PROJECTION,
924924
order=ORDER,
925-
group_by=GROUP_BY,
925+
distinct_on=DISTINCT_ON,
926926
)
927927

928928
self.assertTrue(isinstance(query, _Dummy))
@@ -935,7 +935,7 @@ def test_query_explicit(self):
935935
'filters': FILTERS,
936936
'projection': PROJECTION,
937937
'order': ORDER,
938-
'group_by': GROUP_BY,
938+
'distinct_on': DISTINCT_ON,
939939
}
940940
self.assertEqual(query.kwargs, kwargs)
941941

gcloud/datastore/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ def test_run_query_w_namespace_nonempty_result(self):
589589
entity = entity_pb2.Entity()
590590
q_pb = self._make_query_pb(KIND)
591591
rsp_pb = datastore_pb2.RunQueryResponse()
592-
rsp_pb.batch.entity_result.add(entity=entity)
592+
rsp_pb.batch.entity_results.add(entity=entity)
593593
rsp_pb.batch.entity_result_type = 1 # FULL
594594
rsp_pb.batch.more_results = 3 # NO_MORE_RESULTS
595595
conn = self._makeOne()

gcloud/datastore/test_query.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_ctor_defaults(self):
4242
self.assertEqual(query.filters, [])
4343
self.assertEqual(query.projection, [])
4444
self.assertEqual(query.order, [])
45-
self.assertEqual(query.group_by, [])
45+
self.assertEqual(query.distinct_on, [])
4646

4747
def test_ctor_explicit(self):
4848
from gcloud.datastore.key import Key
@@ -54,7 +54,7 @@ def test_ctor_explicit(self):
5454
FILTERS = [('foo', '=', 'Qux'), ('bar', '<', 17)]
5555
PROJECTION = ['foo', 'bar', 'baz']
5656
ORDER = ['foo', 'bar']
57-
GROUP_BY = ['foo']
57+
DISTINCT_ON = ['foo']
5858
query = self._makeOne(
5959
client,
6060
kind=_KIND,
@@ -64,7 +64,7 @@ def test_ctor_explicit(self):
6464
filters=FILTERS,
6565
projection=PROJECTION,
6666
order=ORDER,
67-
group_by=GROUP_BY,
67+
distinct_on=DISTINCT_ON,
6868
)
6969
self.assertTrue(query._client is client)
7070
self.assertEqual(query.project, _PROJECT)
@@ -74,7 +74,7 @@ def test_ctor_explicit(self):
7474
self.assertEqual(query.filters, FILTERS)
7575
self.assertEqual(query.projection, PROJECTION)
7676
self.assertEqual(query.order, ORDER)
77-
self.assertEqual(query.group_by, GROUP_BY)
77+
self.assertEqual(query.distinct_on, DISTINCT_ON)
7878

7979
def test_ctor_bad_projection(self):
8080
BAD_PROJECTION = object()
@@ -86,10 +86,10 @@ def test_ctor_bad_order(self):
8686
self.assertRaises(TypeError, self._makeOne, self._makeClient(),
8787
order=BAD_ORDER)
8888

89-
def test_ctor_bad_group_by(self):
90-
BAD_GROUP_BY = object()
89+
def test_ctor_bad_distinct_on(self):
90+
BAD_DISTINCT_ON = object()
9191
self.assertRaises(TypeError, self._makeOne, self._makeClient(),
92-
group_by=BAD_GROUP_BY)
92+
distinct_on=BAD_DISTINCT_ON)
9393

9494
def test_ctor_bad_filters(self):
9595
FILTERS_CANT_UNPACK = [('one', 'two')]
@@ -284,29 +284,29 @@ def test_order_setter_multiple(self):
284284
query.order = ['foo', '-bar']
285285
self.assertEqual(query.order, ['foo', '-bar'])
286286

287-
def test_group_by_setter_empty(self):
288-
query = self._makeOne(self._makeClient(), group_by=['foo', 'bar'])
289-
query.group_by = []
290-
self.assertEqual(query.group_by, [])
287+
def test_distinct_on_setter_empty(self):
288+
query = self._makeOne(self._makeClient(), distinct_on=['foo', 'bar'])
289+
query.distinct_on = []
290+
self.assertEqual(query.distinct_on, [])
291291

292-
def test_group_by_setter_string(self):
292+
def test_distinct_on_setter_string(self):
293293
query = self._makeOne(self._makeClient())
294-
query.group_by = 'field1'
295-
self.assertEqual(query.group_by, ['field1'])
294+
query.distinct_on = 'field1'
295+
self.assertEqual(query.distinct_on, ['field1'])
296296

297-
def test_group_by_setter_non_empty(self):
297+
def test_distinct_on_setter_non_empty(self):
298298
query = self._makeOne(self._makeClient())
299-
query.group_by = ['field1', 'field2']
300-
self.assertEqual(query.group_by, ['field1', 'field2'])
299+
query.distinct_on = ['field1', 'field2']
300+
self.assertEqual(query.distinct_on, ['field1', 'field2'])
301301

302-
def test_group_by_multiple_calls(self):
303-
_GROUP_BY1 = ['field1', 'field2']
304-
_GROUP_BY2 = ['field3']
302+
def test_distinct_on_multiple_calls(self):
303+
_DISTINCT_ON1 = ['field1', 'field2']
304+
_DISTINCT_ON2 = ['field3']
305305
query = self._makeOne(self._makeClient())
306-
query.group_by = _GROUP_BY1
307-
self.assertEqual(query.group_by, _GROUP_BY1)
308-
query.group_by = _GROUP_BY2
309-
self.assertEqual(query.group_by, _GROUP_BY2)
306+
query.distinct_on = _DISTINCT_ON1
307+
self.assertEqual(query.distinct_on, _DISTINCT_ON1)
308+
query.distinct_on = _DISTINCT_ON2
309+
self.assertEqual(query.distinct_on, _DISTINCT_ON2)
310310

311311
def test_fetch_defaults_w_client_attr(self):
312312
connection = _Connection()
@@ -427,7 +427,7 @@ def test_next_page_no_cursors_no_more_w_offset_and_limit(self):
427427
[{'kind': self._KIND, 'id': self._ID}])
428428
self.assertEqual(entities[0]['foo'], u'Foo')
429429
qpb = _pb_from_query(query)
430-
qpb.limit = 13
430+
qpb.limit.value = 13
431431
qpb.offset = 29
432432
EXPECTED = {
433433
'project': self._PROJECT,
@@ -557,14 +557,14 @@ def test_empty(self):
557557
self.assertEqual(list(pb.projection), [])
558558
self.assertEqual(list(pb.kind), [])
559559
self.assertEqual(list(pb.order), [])
560-
self.assertEqual(list(pb.group_by), [])
560+
self.assertEqual(list(pb.distinct_on), [])
561561
self.assertEqual(pb.filter.property_filter.property.name, '')
562562
cfilter = pb.filter.composite_filter
563563
self.assertEqual(cfilter.operator, query_pb2.CompositeFilter.AND)
564564
self.assertEqual(list(cfilter.filter), [])
565565
self.assertEqual(pb.start_cursor, b'')
566566
self.assertEqual(pb.end_cursor, b'')
567-
self.assertEqual(pb.limit, 0)
567+
self.assertEqual(pb.limit.value, 0)
568568
self.assertEqual(pb.offset, 0)
569569

570570
def test_projection(self):
@@ -636,9 +636,9 @@ def test_order(self):
636636
query_pb2.PropertyOrder.DESCENDING,
637637
query_pb2.PropertyOrder.ASCENDING])
638638

639-
def test_group_by(self):
640-
pb = self._callFUT(_Query(group_by=['a', 'b', 'c']))
641-
self.assertEqual([item.name for item in pb.group_by],
639+
def test_distinct_on(self):
640+
pb = self._callFUT(_Query(distinct_on=['a', 'b', 'c']))
641+
self.assertEqual([item.name for item in pb.distinct_on],
642642
['a', 'b', 'c'])
643643

644644

@@ -653,7 +653,7 @@ def __init__(self,
653653
filters=(),
654654
projection=(),
655655
order=(),
656-
group_by=()):
656+
distinct_on=()):
657657
self._client = client
658658
self.kind = kind
659659
self.project = project
@@ -662,7 +662,7 @@ def __init__(self,
662662
self.filters = filters
663663
self.projection = projection
664664
self.order = order
665-
self.group_by = group_by
665+
self.distinct_on = distinct_on
666666

667667

668668
class _Connection(object):

system_tests/datastore.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,9 @@ def test_query_paginate_with_start_cursor(self):
376376
self.assertEqual(new_entities[0]['name'], 'Sansa')
377377
self.assertEqual(new_entities[2]['name'], 'Arya')
378378

379-
def test_query_group_by(self):
379+
def test_query_distinct_on(self):
380380
query = self._base_query()
381-
query.group_by = ['alive']
381+
query.distinct_on = ['alive']
382382

383383
expected_matches = 2
384384
# We expect 2, but allow the query to get 1 extra.

0 commit comments

Comments
 (0)