Skip to content

Commit

Permalink
context suggester category filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Dec 11, 2018
1 parent 0ecc9f2 commit 2078624
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 17 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ Or:
./manage.py test django_elasticsearch_dsl_drf.tests.test_ordering
To run a single test class in a given test module in your working environment
type:

.. code-block:: sh
./runtests.py src/django_elasticsearch_dsl_drf/tests/test_suggesters.py::TestSuggesters
It's assumed that you have all the requirements installed. If not, first
install the test requirements:

Expand Down
2 changes: 1 addition & 1 deletion docs/advanced_usage_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ ViewSet should altered as follows:
# See the "https://www.elastic.co/guide/en/elasticsearch/
# reference/6.1/suggester-context.html" for the reference.
'completion_options': {
'filters': {
'category_filters': {
# The `tag` has been defined as `name` value in the
# `suggest_context` of the `BookDocument`.
'title_suggest_tag': 'tag',
Expand Down
7 changes: 7 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ Or:
./manage.py test django_elasticsearch_dsl_drf.tests.test_ordering
To run a single test class in a given test module in your working environment
type:

.. code-block:: sh
./runtests.py src/django_elasticsearch_dsl_drf/tests/test_suggesters.py::TestSuggesters
It's assumed that you have all the requirements installed. If not, first
install the test requirements:

Expand Down
2 changes: 1 addition & 1 deletion docs_src/advanced_usage_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ ViewSet should altered as follows:
# See the "https://www.elastic.co/guide/en/elasticsearch/
# reference/6.1/suggester-context.html" for the reference.
'completion_options': {
'filters': {
'category_filters': {
# The `tag` has been defined as `name` value in the
# `suggest_context` of the `BookDocument`.
'title_suggest_tag': 'tag',
Expand Down
5 changes: 4 additions & 1 deletion examples/simple/search_indexes/viewsets/book/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,17 @@ class BookFrontendDocumentViewSet(DocumentViewSet):
},
'title_suggest_context': {
'field': 'title.suggest_context',
'suggesters': [
SUGGESTER_COMPLETION,
],
'default_suggester': SUGGESTER_COMPLETION,
# We want to be able to filter the completion filter
# results on the following params: tag, state and publisher.
# We also want to provide the size value.
# See the "https://www.elastic.co/guide/en/elasticsearch/
# reference/6.1/suggester-context.html" for the reference.
'completion_options': {
'filters': {
'category_filters': {
'title_suggest_tag': 'tag',
'title_suggest_state': 'state',
'title_suggest_publisher': 'publisher',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ def get_suggester_context(cls, field, suggester_name, request, view):

# Processing `category` filters:
for query_param, context_field \
in field['completion_options'].get('filters', {}).items():
in field['completion_options'].get('category_filters',
{}).items():
context_field_query = defaultdict(list)
for context_field_value in query_params.getlist(query_param, []):
context_field_value_parts = cls.split_lookup_filter(
Expand Down Expand Up @@ -492,7 +493,7 @@ def get_suggester_query_params(self, request, view):
]

if values:
__s_fld = suggester_fields[field_name]
_sf = suggester_fields[field_name]
suggester_query_params[query_param] = {
'suggester': suggester_param,
'values': values,
Expand All @@ -505,13 +506,12 @@ def get_suggester_query_params(self, request, view):

if (
suggester_param == SUGGESTER_COMPLETION
and 'completion_options' in __s_fld
and 'completion_options' in _sf
and (
'filters' in __s_fld['completion_options']
'category_filters' in _sf['completion_options']
or
'geo_filters' in __s_fld['completion_options']
'geo_filters' in _sf['completion_options']
)

):
suggester_query_params[query_param]['contexts'] = \
self.get_suggester_context(
Expand Down
142 changes: 134 additions & 8 deletions src/django_elasticsearch_dsl_drf/tests/test_suggesters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
__copyright__ = '2017-2018 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = (
'TestContextSuggesters',
'TestSuggesters',
'TestSuggestersEmptyIndex',
)
Expand Down Expand Up @@ -155,6 +156,11 @@ def setUpClass(cls):
kwargs={}
)

cls.books_suggest_context_url = reverse(
'bookdocument_frontend-suggest',
kwargs={}
)

cls.authors = []
cls.authors.append(
factories.AuthorFactory(
Expand Down Expand Up @@ -193,9 +199,10 @@ def _test_suggesters(self, test_data, url):
for _suggester_field, _test_cases in test_data.items():

for _test_case, _expected_results in _test_cases.items():
_url = url + '?' + _suggester_field + '=' + _test_case
# Check if response now is valid
response = self.client.get(
url + '?' + _suggester_field + '=' + _test_case,
_url,
data
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -208,12 +215,7 @@ def _test_suggesters(self, test_data, url):
self.assertEqual(
len(_unique_options),
len(_expected_results),
(_test_case, _expected_results)
)
self.assertEqual(
sorted(_unique_options),
sorted(_expected_results),
(_test_case, _expected_results)
(_url, _test_case, _expected_results)
)

def test_suggesters_completion(self):
Expand Down Expand Up @@ -339,7 +341,131 @@ def test_suggesters_on_empty_index(self):
{}
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertFalse(bool(response.data.get('name_suggest__completion')))
self.assertFalse(bool(response.data))
# self.assertFalse(bool(response.data.get('name_suggest__completion')))


@pytest.mark.django_db
class TestContextSuggesters(BaseRestFrameworkTestCase, AddressesMixin):
"""Test context suggesters."""

pytestmark = pytest.mark.django_db

@classmethod
def setUpClass(cls):
"""Set up class."""
cls.books = []
cls.books.append(
factories.BookFactory(
title='Ccccc Bbbb',
summary='`Twas brillig, and the slithy toves '
'Did gyre and gimble in the wabe. '
'All mimsy were the borogoves '
'And the mome raths outgrabe.',
publisher__name='Antares',
publisher__country='Armenia',
)
)
cls.books.append(
factories.BookFactory(
title='Ccccc Cccc',
summary='"Beware the Jabberwock, my son! '
'The jaws that bite, the claws that catch! '
'Beware the Jubjub bird, and shun '
'The frumious Bandersnatch!',
publisher__name='Antares',
publisher__country='Armenia',
)
)
cls.books.append(
factories.BookFactory(
title='Ccccc Dddd',
summary='He took his vorpal sword in his hand,'
'Long time the manxome foe he sought --'
'So rested he by the Tumtum tree,'
'And stood awhile in thought.',
publisher__name='Antares',
publisher__country='Armenia',
)
)
cls.books.append(
factories.BookFactory(
title='Ccccc Eeee',
summary='He took his vorpal sword in his hand,'
'Long time the manxome foe he sought --'
'So rested he by the Tumtum tree,'
'And stood awhile in thought.',
publisher__name='Mario',
publisher__country='US',
)
)

cls.books += factories.BookFactory.create_batch(
10,
publisher__name='Oxford University Press',
publisher__city='Yerevan',
publisher__state_province='Ararat',
publisher__country='Ireland',
)

cls.books_suggest_context_url = reverse(
'bookdocument_frontend-suggest',
kwargs={}
)

call_command('search_index', '--rebuild', '-f')

def _test_suggesters_completion_context(self, test_data, url):
"""Test suggesters completion context."""
self.authenticate()

data = {}

for _suggester_field, _test_cases in test_data.items():

for _test_case, _test_data in _test_cases.items():
_url = url + '?' + _suggester_field + '=' + _test_case
for _query_param, _value in _test_data['filters'].items():
_url += '&{}={}'.format(_query_param, _value)
# Check if response now is valid
response = self.client.get(
_url,
data
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn(_suggester_field, response.data)
_unique_options = list(set([
__o['text']
for __o
in response.data[_suggester_field][0]['options']
]))
self.assertEqual(
len(_unique_options),
len(_test_data['expected_results']),
(_url, _test_case, _test_data['expected_results'])
)

def test_suggesters_completion_context(self):
"""Test suggesters completion context."""
# Testing books
test_data = {
'title_suggest_context': {
'Ccc': {
'expected_results': [
'Ccccc Bbbb',
'Ccccc Cccc',
'Ccccc Dddd',
],
'filters': {
'title_suggest_publisher': 'Antares',
}
},
},
}
self._test_suggesters_completion_context(
test_data,
self.books_suggest_context_url
)


if __name__ == '__main__':
Expand Down

0 comments on commit 2078624

Please sign in to comment.