Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Sep 17, 2019
1 parent 4a30430 commit 2b5d3cb
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/django_elasticsearch_dsl_drf/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import absolute_import

from time import sleep
import unittest

from django.core.management import call_command
Expand Down Expand Up @@ -37,6 +38,7 @@
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = (
'TestSearch',
'TestSearchCustomCases',
)


Expand Down Expand Up @@ -352,5 +354,79 @@ def test_schema_field_not_required(self):
self.assertFalse(field)


@pytest.mark.django_db
class TestSearchCustomCases(BaseRestFrameworkTestCase):
"""Test search."""

pytestmark = pytest.mark.django_db

def _reindex(self):
call_command('search_index', '--rebuild', '-f')

def _test_search_any_word_in_indexed_fields(self,
search_term,
url,
expected_num_results,
title_match=None,
create_factory=False):
if create_factory:
book = factories.BookWithUniqueTitleFactory(
title='This is a short indexed description'
)
other_books = factories.BookWithUniqueTitleFactory.create_batch(100)
self._reindex()

self.authenticate()
filtered_url = url + '?search={}'.format(search_term)
filtered_response = self.client.get(filtered_url)
self.assertEqual(filtered_response.status_code, status.HTTP_200_OK)
self.assertEqual(
len(filtered_response.data['results']),
expected_num_results
)
if title_match:
self.assertEqual(
filtered_response.data['results'][0]['title'],
title_match
)

def test_search_any_word_in_indexed_fields(self):
"""Test search any word in indexed fields.
:return:
"""
# Create some data to test
book = factories.BookWithUniqueTitleFactory(
title='This is a short indexed description'
)
factories.BookWithUniqueTitleFactory.create_batch(100)
self._reindex()
sleep(3) # Wait until indexed
url = reverse('bookdocument-list', kwargs={})

self._test_search_any_word_in_indexed_fields(
search_term='short indexed description',
url=url,
expected_num_results=1,
title_match=book.title,
create_factory=False
)
self._test_search_any_word_in_indexed_fields(
search_term='a description',
url=url,
expected_num_results=1,
title_match=book.title,
create_factory=False
)

self._test_search_any_word_in_indexed_fields(
search_term='short description',
url=url,
expected_num_results=1,
title_match=book.title,
create_factory=False
)


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

0 comments on commit 2b5d3cb

Please sign in to comment.