Skip to content

Commit

Permalink
Adds alias for grouping by multiple related fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinkels committed May 8, 2016
1 parent 39897fe commit a136069
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion flask_restless/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def search(session, model, filters=None, sort=None, group_by=None,
for field_name in group_by:
if '.' in field_name:
field_name, field_name_in_relation = field_name.split('.')
relation_model = get_related_model(model, field_name)
relation_model = aliased(get_related_model(model, field_name))
field = getattr(relation_model, field_name_in_relation)
query = query.join(relation_model)
query = query.group_by(field)
Expand Down
28 changes: 27 additions & 1 deletion tests/test_fetching.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
specification.
"""
from itertools import count
from itertools import product
from operator import itemgetter
from unittest2 import skip

Expand Down Expand Up @@ -50,6 +52,7 @@ def setUp(self):
class Person(self.Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
age = Column(Integer)
name = Column(Unicode)

class Article(self.Base):
Expand Down Expand Up @@ -187,13 +190,36 @@ def test_group_by_related(self):
article3.author = person2
self.session.add_all([person1, person2, article1, article2, article3])
self.session.commit()
response = self.app.get('/api/article?group=author.name')
query_string = {'group': 'author.name'}
response = self.app.get('/api/article', query_string=query_string)
document = loads(response.data)
articles = document['data']
author_ids = sorted(article['relationships']['author']['data']['id']
for article in articles)
assert ['1', '2'] == author_ids

def test_group_by_mutiple_relationship_attributes(self):
"""Tests for grouping results by multiple fields of a related model."""
i = count(start=1)
names = [u'foo', u'bar']
ages = [10, 20]
# There are two people with each combination of name and age.
for name, age in product(names, ages):
id1, id2 = next(i), next(i)
person1 = self.Person(id=id1, name=name, age=age)
person2 = self.Person(id=id2, name=name, age=age)
article1 = self.Article(author=person1)
article2 = self.Article(author=person2)
self.session.add_all([article1, article2, person1, person2])
self.session.commit()
query_string = {'group': ','.join(['author.name', 'author.age'])}
response = self.app.get('/api/article', query_string=query_string)
document = loads(response.data)
articles = document['data']
author_ids = sorted(article['relationships']['author']['data']['id']
for article in articles)
assert ['2', '4', '6', '8'] == author_ids

def test_pagination_links_empty_collection(self):
"""Tests that pagination links work correctly for an empty
collection.
Expand Down

0 comments on commit a136069

Please sign in to comment.