Skip to content

Commit 5dcb4a9

Browse files
committed
Renaming group_by->distinct_on in Query.
Updating in both the protobuf and our Query wrapper.
1 parent f220a36 commit 5dcb4a9

4 files changed

Lines changed: 46 additions & 46 deletions

File tree

gcloud/datastore/query.py

Lines changed: 11 additions & 11 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 ``dataset_id`` 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 dataset_id(self):
@@ -272,15 +272,15 @@ def order(self, value):
272272
self._order[:] = value
273273

274274
@property
275-
def group_by(self):
275+
def distinct_on(self):
276276
"""Names of fields used to group query results.
277277
278278
:rtype: sequence of string
279279
"""
280-
return self._group_by[:]
280+
return self._distinct_on[:]
281281

282-
@group_by.setter
283-
def group_by(self, value):
282+
@distinct_on.setter
283+
def distinct_on(self, value):
284284
"""Set fields used to group query results.
285285
286286
:type value: string or sequence of strings
@@ -289,7 +289,7 @@ def group_by(self, value):
289289
"""
290290
if isinstance(value, str):
291291
value = [value]
292-
self._group_by[:] = value
292+
self._distinct_on[:] = value
293293

294294
def fetch(self, limit=None, offset=0, start_cursor=None, end_cursor=None,
295295
client=None):
@@ -511,7 +511,7 @@ def _pb_from_query(query):
511511
property_order.property.name = prop
512512
property_order.direction = property_order.ASCENDING
513513

514-
for group_by_name in query.group_by:
515-
pb.group_by.add().name = group_by_name
514+
for distinct_on_name in query.distinct_on:
515+
pb.distinct_on.add().name = distinct_on_name
516516

517517
return pb

gcloud/datastore/test_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ def test_query_explicit(self):
899899
FILTERS = [('PROPERTY', '==', 'VALUE')]
900900
PROJECTION = ['__key__']
901901
ORDER = ['PROPERTY']
902-
GROUP_BY = ['GROUPBY']
902+
DISTINCT_ON = ['DISTINCT_ON']
903903

904904
creds = object()
905905
client = self._makeOne(credentials=creds)
@@ -912,7 +912,7 @@ def test_query_explicit(self):
912912
filters=FILTERS,
913913
projection=PROJECTION,
914914
order=ORDER,
915-
group_by=GROUP_BY,
915+
distinct_on=DISTINCT_ON,
916916
)
917917

918918
self.assertTrue(isinstance(query, _Dummy))
@@ -925,7 +925,7 @@ def test_query_explicit(self):
925925
'filters': FILTERS,
926926
'projection': PROJECTION,
927927
'order': ORDER,
928-
'group_by': GROUP_BY,
928+
'distinct_on': DISTINCT_ON,
929929
}
930930
self.assertEqual(query.kwargs, kwargs)
931931

gcloud/datastore/test_query.py

Lines changed: 30 additions & 30 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.dataset_id, _DATASET)
@@ -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')]
@@ -264,29 +264,29 @@ def test_order_setter_multiple(self):
264264
query.order = ['foo', '-bar']
265265
self.assertEqual(query.order, ['foo', '-bar'])
266266

267-
def test_group_by_setter_empty(self):
268-
query = self._makeOne(self._makeClient(), group_by=['foo', 'bar'])
269-
query.group_by = []
270-
self.assertEqual(query.group_by, [])
267+
def test_distinct_on_setter_empty(self):
268+
query = self._makeOne(self._makeClient(), distinct_on=['foo', 'bar'])
269+
query.distinct_on = []
270+
self.assertEqual(query.distinct_on, [])
271271

272-
def test_group_by_setter_string(self):
272+
def test_distinct_on_setter_string(self):
273273
query = self._makeOne(self._makeClient())
274-
query.group_by = 'field1'
275-
self.assertEqual(query.group_by, ['field1'])
274+
query.distinct_on = 'field1'
275+
self.assertEqual(query.distinct_on, ['field1'])
276276

277-
def test_group_by_setter_non_empty(self):
277+
def test_distinct_on_setter_non_empty(self):
278278
query = self._makeOne(self._makeClient())
279-
query.group_by = ['field1', 'field2']
280-
self.assertEqual(query.group_by, ['field1', 'field2'])
279+
query.distinct_on = ['field1', 'field2']
280+
self.assertEqual(query.distinct_on, ['field1', 'field2'])
281281

282-
def test_group_by_multiple_calls(self):
283-
_GROUP_BY1 = ['field1', 'field2']
284-
_GROUP_BY2 = ['field3']
282+
def test_distinct_on_multiple_calls(self):
283+
_DISTINCT_ON1 = ['field1', 'field2']
284+
_DISTINCT_ON2 = ['field3']
285285
query = self._makeOne(self._makeClient())
286-
query.group_by = _GROUP_BY1
287-
self.assertEqual(query.group_by, _GROUP_BY1)
288-
query.group_by = _GROUP_BY2
289-
self.assertEqual(query.group_by, _GROUP_BY2)
286+
query.distinct_on = _DISTINCT_ON1
287+
self.assertEqual(query.distinct_on, _DISTINCT_ON1)
288+
query.distinct_on = _DISTINCT_ON2
289+
self.assertEqual(query.distinct_on, _DISTINCT_ON2)
290290

291291
def test_fetch_defaults_w_client_attr(self):
292292
connection = _Connection()
@@ -537,7 +537,7 @@ def test_empty(self):
537537
self.assertEqual(list(pb.projection), [])
538538
self.assertEqual(list(pb.kind), [])
539539
self.assertEqual(list(pb.order), [])
540-
self.assertEqual(list(pb.group_by), [])
540+
self.assertEqual(list(pb.distinct_on), [])
541541
self.assertEqual(pb.filter.property_filter.property.name, '')
542542
cfilter = pb.filter.composite_filter
543543
self.assertEqual(cfilter.operator, query_pb2.CompositeFilter.AND)
@@ -616,9 +616,9 @@ def test_order(self):
616616
query_pb2.PropertyOrder.DESCENDING,
617617
query_pb2.PropertyOrder.ASCENDING])
618618

619-
def test_group_by(self):
620-
pb = self._callFUT(_Query(group_by=['a', 'b', 'c']))
621-
self.assertEqual([item.name for item in pb.group_by],
619+
def test_distinct_on(self):
620+
pb = self._callFUT(_Query(distinct_on=['a', 'b', 'c']))
621+
self.assertEqual([item.name for item in pb.distinct_on],
622622
['a', 'b', 'c'])
623623

624624

@@ -633,7 +633,7 @@ def __init__(self,
633633
filters=(),
634634
projection=(),
635635
order=(),
636-
group_by=()):
636+
distinct_on=()):
637637
self._client = client
638638
self.kind = kind
639639
self.dataset_id = dataset_id
@@ -642,7 +642,7 @@ def __init__(self,
642642
self.filters = filters
643643
self.projection = projection
644644
self.order = order
645-
self.group_by = group_by
645+
self.distinct_on = distinct_on
646646

647647

648648
class _Connection(object):

system_tests/datastore.py

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

316-
def test_query_group_by(self):
316+
def test_query_distinct_on(self):
317317
query = self._base_query()
318-
query.group_by = ['alive']
318+
query.distinct_on = ['alive']
319319

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

0 commit comments

Comments
 (0)