Skip to content

Commit 2078624

Browse files
context suggester category filter tests
1 parent 0ecc9f2 commit 2078624

File tree

7 files changed

+160
-17
lines changed

7 files changed

+160
-17
lines changed

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ Or:
169169
170170
./manage.py test django_elasticsearch_dsl_drf.tests.test_ordering
171171
172+
To run a single test class in a given test module in your working environment
173+
type:
174+
175+
.. code-block:: sh
176+
177+
./runtests.py src/django_elasticsearch_dsl_drf/tests/test_suggesters.py::TestSuggesters
178+
172179
It's assumed that you have all the requirements installed. If not, first
173180
install the test requirements:
174181

docs/advanced_usage_examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ ViewSet should altered as follows:
13791379
# See the "https://www.elastic.co/guide/en/elasticsearch/
13801380
# reference/6.1/suggester-context.html" for the reference.
13811381
'completion_options': {
1382-
'filters': {
1382+
'category_filters': {
13831383
# The `tag` has been defined as `name` value in the
13841384
# `suggest_context` of the `BookDocument`.
13851385
'title_suggest_tag': 'tag',

docs/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ Or:
169169
170170
./manage.py test django_elasticsearch_dsl_drf.tests.test_ordering
171171
172+
To run a single test class in a given test module in your working environment
173+
type:
174+
175+
.. code-block:: sh
176+
177+
./runtests.py src/django_elasticsearch_dsl_drf/tests/test_suggesters.py::TestSuggesters
178+
172179
It's assumed that you have all the requirements installed. If not, first
173180
install the test requirements:
174181

docs_src/advanced_usage_examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ ViewSet should altered as follows:
13791379
# See the "https://www.elastic.co/guide/en/elasticsearch/
13801380
# reference/6.1/suggester-context.html" for the reference.
13811381
'completion_options': {
1382-
'filters': {
1382+
'category_filters': {
13831383
# The `tag` has been defined as `name` value in the
13841384
# `suggest_context` of the `BookDocument`.
13851385
'title_suggest_tag': 'tag',

examples/simple/search_indexes/viewsets/book/frontend.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,17 @@ class BookFrontendDocumentViewSet(DocumentViewSet):
268268
},
269269
'title_suggest_context': {
270270
'field': 'title.suggest_context',
271+
'suggesters': [
272+
SUGGESTER_COMPLETION,
273+
],
271274
'default_suggester': SUGGESTER_COMPLETION,
272275
# We want to be able to filter the completion filter
273276
# results on the following params: tag, state and publisher.
274277
# We also want to provide the size value.
275278
# See the "https://www.elastic.co/guide/en/elasticsearch/
276279
# reference/6.1/suggester-context.html" for the reference.
277280
'completion_options': {
278-
'filters': {
281+
'category_filters': {
279282
'title_suggest_tag': 'tag',
280283
'title_suggest_state': 'state',
281284
'title_suggest_publisher': 'publisher',

src/django_elasticsearch_dsl_drf/filter_backends/suggester/native.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ def get_suggester_context(cls, field, suggester_name, request, view):
276276

277277
# Processing `category` filters:
278278
for query_param, context_field \
279-
in field['completion_options'].get('filters', {}).items():
279+
in field['completion_options'].get('category_filters',
280+
{}).items():
280281
context_field_query = defaultdict(list)
281282
for context_field_value in query_params.getlist(query_param, []):
282283
context_field_value_parts = cls.split_lookup_filter(
@@ -492,7 +493,7 @@ def get_suggester_query_params(self, request, view):
492493
]
493494

494495
if values:
495-
__s_fld = suggester_fields[field_name]
496+
_sf = suggester_fields[field_name]
496497
suggester_query_params[query_param] = {
497498
'suggester': suggester_param,
498499
'values': values,
@@ -505,13 +506,12 @@ def get_suggester_query_params(self, request, view):
505506

506507
if (
507508
suggester_param == SUGGESTER_COMPLETION
508-
and 'completion_options' in __s_fld
509+
and 'completion_options' in _sf
509510
and (
510-
'filters' in __s_fld['completion_options']
511+
'category_filters' in _sf['completion_options']
511512
or
512-
'geo_filters' in __s_fld['completion_options']
513+
'geo_filters' in _sf['completion_options']
513514
)
514-
515515
):
516516
suggester_query_params[query_param]['contexts'] = \
517517
self.get_suggester_context(

src/django_elasticsearch_dsl_drf/tests/test_suggesters.py

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
__copyright__ = '2017-2018 Artur Barseghyan'
3131
__license__ = 'GPL 2.0/LGPL 2.1'
3232
__all__ = (
33+
'TestContextSuggesters',
3334
'TestSuggesters',
3435
'TestSuggestersEmptyIndex',
3536
)
@@ -155,6 +156,11 @@ def setUpClass(cls):
155156
kwargs={}
156157
)
157158

159+
cls.books_suggest_context_url = reverse(
160+
'bookdocument_frontend-suggest',
161+
kwargs={}
162+
)
163+
158164
cls.authors = []
159165
cls.authors.append(
160166
factories.AuthorFactory(
@@ -193,9 +199,10 @@ def _test_suggesters(self, test_data, url):
193199
for _suggester_field, _test_cases in test_data.items():
194200

195201
for _test_case, _expected_results in _test_cases.items():
202+
_url = url + '?' + _suggester_field + '=' + _test_case
196203
# Check if response now is valid
197204
response = self.client.get(
198-
url + '?' + _suggester_field + '=' + _test_case,
205+
_url,
199206
data
200207
)
201208
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -208,12 +215,7 @@ def _test_suggesters(self, test_data, url):
208215
self.assertEqual(
209216
len(_unique_options),
210217
len(_expected_results),
211-
(_test_case, _expected_results)
212-
)
213-
self.assertEqual(
214-
sorted(_unique_options),
215-
sorted(_expected_results),
216-
(_test_case, _expected_results)
218+
(_url, _test_case, _expected_results)
217219
)
218220

219221
def test_suggesters_completion(self):
@@ -339,7 +341,131 @@ def test_suggesters_on_empty_index(self):
339341
{}
340342
)
341343
self.assertEqual(response.status_code, status.HTTP_200_OK)
342-
self.assertFalse(bool(response.data.get('name_suggest__completion')))
344+
self.assertFalse(bool(response.data))
345+
# self.assertFalse(bool(response.data.get('name_suggest__completion')))
346+
347+
348+
@pytest.mark.django_db
349+
class TestContextSuggesters(BaseRestFrameworkTestCase, AddressesMixin):
350+
"""Test context suggesters."""
351+
352+
pytestmark = pytest.mark.django_db
353+
354+
@classmethod
355+
def setUpClass(cls):
356+
"""Set up class."""
357+
cls.books = []
358+
cls.books.append(
359+
factories.BookFactory(
360+
title='Ccccc Bbbb',
361+
summary='`Twas brillig, and the slithy toves '
362+
'Did gyre and gimble in the wabe. '
363+
'All mimsy were the borogoves '
364+
'And the mome raths outgrabe.',
365+
publisher__name='Antares',
366+
publisher__country='Armenia',
367+
)
368+
)
369+
cls.books.append(
370+
factories.BookFactory(
371+
title='Ccccc Cccc',
372+
summary='"Beware the Jabberwock, my son! '
373+
'The jaws that bite, the claws that catch! '
374+
'Beware the Jubjub bird, and shun '
375+
'The frumious Bandersnatch!',
376+
publisher__name='Antares',
377+
publisher__country='Armenia',
378+
)
379+
)
380+
cls.books.append(
381+
factories.BookFactory(
382+
title='Ccccc Dddd',
383+
summary='He took his vorpal sword in his hand,'
384+
'Long time the manxome foe he sought --'
385+
'So rested he by the Tumtum tree,'
386+
'And stood awhile in thought.',
387+
publisher__name='Antares',
388+
publisher__country='Armenia',
389+
)
390+
)
391+
cls.books.append(
392+
factories.BookFactory(
393+
title='Ccccc Eeee',
394+
summary='He took his vorpal sword in his hand,'
395+
'Long time the manxome foe he sought --'
396+
'So rested he by the Tumtum tree,'
397+
'And stood awhile in thought.',
398+
publisher__name='Mario',
399+
publisher__country='US',
400+
)
401+
)
402+
403+
cls.books += factories.BookFactory.create_batch(
404+
10,
405+
publisher__name='Oxford University Press',
406+
publisher__city='Yerevan',
407+
publisher__state_province='Ararat',
408+
publisher__country='Ireland',
409+
)
410+
411+
cls.books_suggest_context_url = reverse(
412+
'bookdocument_frontend-suggest',
413+
kwargs={}
414+
)
415+
416+
call_command('search_index', '--rebuild', '-f')
417+
418+
def _test_suggesters_completion_context(self, test_data, url):
419+
"""Test suggesters completion context."""
420+
self.authenticate()
421+
422+
data = {}
423+
424+
for _suggester_field, _test_cases in test_data.items():
425+
426+
for _test_case, _test_data in _test_cases.items():
427+
_url = url + '?' + _suggester_field + '=' + _test_case
428+
for _query_param, _value in _test_data['filters'].items():
429+
_url += '&{}={}'.format(_query_param, _value)
430+
# Check if response now is valid
431+
response = self.client.get(
432+
_url,
433+
data
434+
)
435+
self.assertEqual(response.status_code, status.HTTP_200_OK)
436+
self.assertIn(_suggester_field, response.data)
437+
_unique_options = list(set([
438+
__o['text']
439+
for __o
440+
in response.data[_suggester_field][0]['options']
441+
]))
442+
self.assertEqual(
443+
len(_unique_options),
444+
len(_test_data['expected_results']),
445+
(_url, _test_case, _test_data['expected_results'])
446+
)
447+
448+
def test_suggesters_completion_context(self):
449+
"""Test suggesters completion context."""
450+
# Testing books
451+
test_data = {
452+
'title_suggest_context': {
453+
'Ccc': {
454+
'expected_results': [
455+
'Ccccc Bbbb',
456+
'Ccccc Cccc',
457+
'Ccccc Dddd',
458+
],
459+
'filters': {
460+
'title_suggest_publisher': 'Antares',
461+
}
462+
},
463+
},
464+
}
465+
self._test_suggesters_completion_context(
466+
test_data,
467+
self.books_suggest_context_url
468+
)
343469

344470

345471
if __name__ == '__main__':

0 commit comments

Comments
 (0)