Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
'group_by' is now a list
Browse files Browse the repository at this point in the history
Backdrop needs to be able to handle being passed multiple 'group_by'
query-string parameters.
  • Loading branch information
nick-gravgaard committed Jul 29, 2014
1 parent 9055763 commit ea2c43e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions backdrop/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def create(cls,
delta = duration if start_at else -duration
start_at, end_at = cls.__calculate_start_and_end(period, date,
delta)
return Query(start_at, end_at, delta, period,
filter_by or [], group_by, sort_by, limit, collect or [])
return Query(start_at, end_at, delta, period, filter_by or [],
group_by or [], sort_by, limit, collect or [])

@staticmethod
def __calculate_start_and_end(period, date, delta):
Expand Down Expand Up @@ -57,16 +57,16 @@ def group_keys(self):
create the hierarchical response.
>>> from ..core.timeseries import WEEK
>>> Query.create(group_by="foo").group_keys
>>> Query.create(group_by=['foo']).group_keys
['foo']
>>> Query.create(period=WEEK).group_keys
['_week_start_at']
>>> Query.create(group_by="foo", period=WEEK).group_keys
>>> Query.create(group_by=['foo'], period=WEEK).group_keys
['foo', '_week_start_at']
"""
keys = []
if self.group_by:
keys.append(self.group_by)
keys += self.group_by
if self.period:
keys.append(self.period.start_at_key)
return keys
Expand Down
2 changes: 1 addition & 1 deletion backdrop/read/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def parse_filter_by(filter_by):

args['filter_by'] = map(parse_filter_by, request_args.getlist('filter_by'))

args['group_by'] = request_args.get('group_by')
args['group_by'] = request_args.getlist('group_by')

args['sort_by'] = if_present(lambda sort_by: sort_by.split(':', 1),
request_args.get('sort_by'))
Expand Down
10 changes: 5 additions & 5 deletions tests/core/storage/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_query_grouped_by_field(self):
{'foo': 'bar'})

results = self.engine.execute_query('foo_bar', Query.create(
group_by='foo'))
group_by=['foo']))

assert_that(results,
contains_inanyorder(
Expand Down Expand Up @@ -242,7 +242,7 @@ def test_group_by_field_and_period(self):
{'_timestamp': d_tz(2012, 12, 12), 'foo': 'bar'})

results = self.engine.execute_query('foo_bar', Query.create(
group_by='foo', period=DAY))
group_by=['foo'], period=DAY))

assert_that(results,
contains_inanyorder(
Expand All @@ -256,7 +256,7 @@ def test_group_query_with_collect_fields(self):
{'foo': 'bar', 'c': 2})

results = self.engine.execute_query('foo_bar', Query.create(
group_by='foo', collect=[('c', 'sum')]))
group_by=['foo'], collect=[('c', 'sum')]))

assert_that(results,
contains_inanyorder(
Expand All @@ -271,7 +271,7 @@ def test_group_and_collect_with_false_values(self):
{'foo': 'one', 'bar': False})

results = self.engine.execute_query('foo_bar', Query.create(
group_by='foo', collect=[('bar', 'sum')]))
group_by=['foo'], collect=[('bar', 'sum')]))

assert_that(results,
contains_inanyorder(
Expand All @@ -285,7 +285,7 @@ def test_group_query_ignores_records_without_grouping_key(self):
{'bar': 'one'})

results = self.engine.execute_query('foo_bar', Query.create(
group_by='foo'))
group_by=['foo']))

assert_that(results,
contains_inanyorder(
Expand Down
12 changes: 6 additions & 6 deletions tests/core/test_data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_week_and_group_query(self):
{"some_group": "val2", "_week_start_at": d(2013, 1, 14), "_count": 6},
]
data = self.data_set.execute_query(
Query.create(period=WEEK, group_by="some_group"))
Query.create(period=WEEK, group_by=['some_group']))

assert_that(data, has_length(2))
assert_that(data, has_item(has_entries({
Expand Down Expand Up @@ -200,7 +200,7 @@ def test_month_and_group_query(self):
]

data = self.data_set.execute_query(Query.create(period=MONTH,
group_by="some_group"))
group_by=['some_group']))
assert_that(data,
has_item(has_entries({"values": has_length(2)})))
assert_that(data,
Expand All @@ -217,7 +217,7 @@ def test_month_and_group_query_with_start_and_end_at(self):

data = self.data_set.execute_query(
Query.create(period=MONTH,
group_by="some_group",
group_by=['some_group'],
start_at=d(2013, 1, 1),
end_at=d(2013, 4, 2)))
assert_that(data,
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_period_group_query_adds_missing_periods_in_correct_order(self):
]

data = self.data_set.execute_query(
Query.create(period=WEEK, group_by="some_group",
Query.create(period=WEEK, group_by=['some_group'],
start_at=d_tz(2013, 1, 7, 0, 0, 0),
end_at=d_tz(2013, 2, 4, 0, 0, 0)))

Expand Down Expand Up @@ -278,7 +278,7 @@ def test_sorted_week_and_group_query(self):
{'some_group': 'val2', '_week_start_at': d(2013, 1, 14), '_count': 6},
]

query = Query.create(period=WEEK, group_by="some_group",
query = Query.create(period=WEEK, group_by=['some_group'],
sort_by=["_count", "descending"])
data = self.data_set.execute_query(query)

Expand All @@ -293,7 +293,7 @@ def test_sorted_week_and_group_query_with_limit(self):
{'some_group': 'val2', '_week_start_at': d(2013, 1, 14), '_count': 5},
]

query = Query.create(period=WEEK, group_by="some_group",
query = Query.create(period=WEEK, group_by=['some_group'],
sort_by=["_count", "descending"], limit=1,
collect=[])
data = self.data_set.execute_query(query)
Expand Down
2 changes: 1 addition & 1 deletion tests/read/test_parse_request_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_group_by_is_passed_through_untouched(self):

args = parse_request_args(request_args)

assert_that(args['group_by'], is_('foobar'))
assert_that(args['group_by'], is_(['foobar']))

def test_sort_is_parsed(self):
request_args = MultiDict([
Expand Down
2 changes: 1 addition & 1 deletion tests/read/test_read_api_query_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_group_by_query_is_executed(self, mock_query):
mock_query.return_value = NoneData()
self.app.get('/foo?group_by=zombies')
mock_query.assert_called_with(
Query.create(group_by=u'zombies'))
Query.create(group_by=[u'zombies']))

@fake_data_set_exists("foo", raw_queries_allowed=True)
@patch('backdrop.core.data_set.DataSet.execute_query')
Expand Down
4 changes: 2 additions & 2 deletions tests/read/test_read_api_service_data_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_group_by_query_is_executed(self, mock_query):
mock_query.return_value = NoneData()
self.app.get('/data/some-group/some-type?group_by=zombies')
mock_query.assert_called_with(
Query.create(group_by=u'zombies'))
Query.create(group_by=[u'zombies']))

@fake_data_set_exists("foo", data_group="some-group", data_type="some-type", raw_queries_allowed=True)
@patch('backdrop.core.data_set.DataSet.execute_query')
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_group_by_with_period_is_executed(self, mock_query):

mock_query.assert_called_with(
Query.create(period=WEEK,
group_by="stuff",
group_by=['stuff'],
start_at=d_tz(2012, 11, 5),
end_at=d_tz(2012, 12, 3)))

Expand Down

0 comments on commit ea2c43e

Please sign in to comment.