Skip to content

Commit

Permalink
Add test for nested filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lingfromSh committed Mar 6, 2021
1 parent b9b1c34 commit a3b440f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
9 changes: 9 additions & 0 deletions examples/factories/blog_post.py
Expand Up @@ -30,6 +30,7 @@ class Comment(Serializable):

def __init__(self, *args, **kwargs):
self.author = kwargs.get('author')
self.tag = kwargs.get('tag')
self.content = kwargs.get('content')
self.created_at = kwargs.get('created_at')

Expand All @@ -45,6 +46,14 @@ class CommentFactory(Factory):
"""Comment factory."""

author = Faker('name')
tag = FuzzyChoice([
'Elastic',
'MongoDB',
'Machine Learning',
'Model Photography',
'Python',
'Django',
])
content = Faker('text')
created_at = Faker('date')

Expand Down
2 changes: 1 addition & 1 deletion src/graphene_elastic/filter_backends/filtering/mixins.py
Expand Up @@ -18,7 +18,7 @@ def QParams(lookup, options, query=None):
if query is None:
query = {options["field"]: options["values"]}

if options["path"]:
if options.get("path"):
# Nested query
return Q("nested", query={lookup: query}, path=options["path"])
return Q(lookup, **query)
Expand Down
99 changes: 92 additions & 7 deletions src/graphene_elastic/tests/test_filter_backend.py
Expand Up @@ -58,8 +58,8 @@ def setUp(self):
start_date="+1d", end_date="+30d"
)
)
# for _post in self.elastic_posts:
# _post.save()
for _post in self.elastic_posts:
_post.save()

self.num_django_posts = 3
self.django_posts = factories.PostFactory.create_batch(
Expand All @@ -69,8 +69,8 @@ def setUp(self):
start_date="+1d", end_date="+30d"
)
)
# for _post in self.django_posts:
# _post.save()
for _post in self.django_posts:
_post.save()

self.num_python_posts = 2
self.python_posts = factories.ManyViewsPostFactory.create_batch(
Expand All @@ -80,8 +80,8 @@ def setUp(self):
start_date="-30d", end_date="-1d"
)
)
# for _post in self.python_posts:
# _post.save()
for _post in self.python_posts:
_post.save()

self.num_all_posts = (
self.num_elastic_posts +
Expand Down Expand Up @@ -138,6 +138,55 @@ def __test_filter_text_lookups(self,
num_posts
)

def __test_nested_filter_lookups(self,
*fields,
lookup,
value,
num_posts):
"""Test nested filter lookups and check num of results.
:param fields: fields hierachy
:param lookup:
:param value:
:param num_posts: num of results
:return:
"""
def _query_params():
s = ""
for field in fields:
s += "{}:{{".format(field)
s += "{}: {}".format(lookup, value)
s += "}" * len(fields)
return s

_query = """
query {
%s(filter:{%s}) {
edges {
node {
category
title
content
numViews
comments{
author
tag
content
createdAt
}
}
}
}
}
"""% (self.endpoint, _query_params())
executed = self.client.execute(_query)
self.assertEqual(
len(executed["data"][self.endpoint]["edges"]),
num_posts,
_query
)


def __test_filter_number_lookups(self,
field,
value,
Expand Down Expand Up @@ -468,6 +517,42 @@ def _test_filter_gt_gte_lt_lte_range_lookups(self):
lookup=LOOKUP_FILTER_RANGE
)

def _test_filter_nested_lookup(self):

with self.subTest('Test filter on field `comments.tag`'
'using `term` lookup'):
_count = 0
for p in self.all_posts:
for pcomment in p.comments:
if pcomment.tag == "Python":
_count += 1
break

self.__test_nested_filter_lookups(
"comments",
"tag",
lookup="term",
value='"Python"',
num_posts=_count
)

with self.subTest('Test filter on field `comments.tag`'
'using `terms` lookup'):
_count = 0
for p in self.all_posts:
for pcomment in p.comments:
if pcomment.tag in ["Python", "MongoDB"]:
_count += 1
break

self.__test_nested_filter_lookups(
"comments",
"tag",
lookup="terms",
value='["Python", "MongoDB"]',
num_posts=_count
)

# TODO: Test range dates

def test_all(self):
Expand All @@ -482,7 +567,7 @@ def test_all(self):
self._test_filter_exclude_lookup()
self._test_filter_exists_is_null_lookups()
self._test_filter_gt_gte_lt_lte_range_lookups()

self._test_filter_nested_lookup()

if __name__ == '__main__':
unittest.main()

0 comments on commit a3b440f

Please sign in to comment.