Merge pull request #1178 from kaedroho/search-tests

Search tests improvements
pull/1173/merge
Karl Hobley 2015-04-14 10:45:29 +01:00
commit 239ee67994
2 zmienionych plików z 30 dodań i 46 usunięć

Wyświetl plik

@ -1,5 +1,6 @@
from six import StringIO
import unittest
import time
from django.test import TestCase
from django.test.utils import override_settings
@ -46,110 +47,89 @@ class BackendTests(WagtailTestUtils):
testb.live = True
testb.save()
self.backend.add(testb)
self.testb = testb
testc = models.SearchTestChild()
testc.title = "Hello"
testc.live = True
testc.save()
self.backend.add(testc)
self.testc = testc
testd = models.SearchTestChild()
testd.title = "World"
testd.save()
self.backend.add(testd)
self.testd = testd
# Refresh the index
self.backend.refresh_index()
def test_blank_search(self):
# Get results for blank terms
results = self.backend.search("", models.SearchTest)
# Should return no results
self.assertEqual(len(results), 0)
self.assertEqual(set(results), set())
def test_search(self):
# Get results for "Hello"
results = self.backend.search("Hello", models.SearchTest)
self.assertEqual(set(results), {self.testa, self.testb, self.testc.searchtest_ptr})
# Should return three results
self.assertEqual(len(results), 3)
# Get results for "World"
results = self.backend.search("World", models.SearchTest)
# Should return two results
self.assertEqual(len(results), 2)
self.assertEqual(set(results), {self.testa, self.testd.searchtest_ptr})
def test_callable_indexed_field(self):
# Get results
results = self.backend.search("Callable", models.SearchTest)
# Should get all 4 results as they all have the callable indexed field
self.assertEqual(len(results), 4)
self.assertEqual(set(results), {self.testa, self.testb, self.testc.searchtest_ptr, self.testd.searchtest_ptr})
def test_filters(self):
# Get only results with live=True set
results = self.backend.search("Hello", models.SearchTest, filters=dict(live=True))
# Should return two results
self.assertEqual(len(results), 2)
results = self.backend.search(None, models.SearchTest, filters=dict(live=True))
self.assertEqual(set(results), {self.testb, self.testc.searchtest_ptr})
def test_filters_with_in_lookup(self):
live_page_titles = models.SearchTest.objects.filter(live=True).values_list('title', flat=True)
results = self.backend.search("Hello", models.SearchTest, filters=dict(title__in=live_page_titles))
# Should return two results
self.assertEqual(len(results), 2)
results = self.backend.search(None, models.SearchTest, filters=dict(title__in=live_page_titles))
self.assertEqual(set(results), {self.testb, self.testc.searchtest_ptr})
def test_single_result(self):
# Get a single result
result = self.backend.search("Hello", models.SearchTest)[0]
# Check that the result is a SearchTest object
result = self.backend.search(None, models.SearchTest)[0]
self.assertIsInstance(result, models.SearchTest)
def test_sliced_results(self):
# Get results and slice them
sliced_results = self.backend.search("Hello", models.SearchTest)[1:3]
sliced_results = self.backend.search(None, models.SearchTest)[1:3]
# Slice must have a length of 2
self.assertEqual(len(sliced_results), 2)
# Check that the results are SearchTest objects
for result in sliced_results:
self.assertIsInstance(result, models.SearchTest)
def test_child_model(self):
# Get results for child model
results = self.backend.search("Hello", models.SearchTestChild)
# Should return one object
self.assertEqual(len(results), 1)
results = self.backend.search(None, models.SearchTestChild)
self.assertEqual(set(results), {self.testc, self.testd})
def test_delete(self):
# Delete one of the objects
self.backend.delete(self.testa)
self.testa.delete()
# Refresh index
self.backend.refresh_index()
# Check that there are only two results
results = self.backend.search("Hello", models.SearchTest)
self.assertEqual(len(results), 2)
results = self.backend.search(None, models.SearchTest)
self.assertEqual(set(results), {self.testb, self.testc.searchtest_ptr, self.testd.searchtest_ptr})
def test_update_index_command(self):
# Reset the index, this should clear out the index
self.backend.reset_index()
# Give Elasticsearch some time to catch up...
time.sleep(1)
results = self.backend.search(None, models.SearchTest)
self.assertEqual(set(results), set())
# Run update_index command
with self.ignore_deprecation_warnings(): # ignore any DeprecationWarnings thrown by models with old-style indexed_fields definitions
management.call_command('update_index', backend_name=self.backend_name, interactive=False, stdout=StringIO())
# Check that there are still 3 results
results = self.backend.search("Hello", models.SearchTest)
self.assertEqual(len(results), 3)
results = self.backend.search(None, models.SearchTest)
self.assertEqual(set(results), {self.testa, self.testb, self.testc.searchtest_ptr, self.testd.searchtest_ptr})
@override_settings(WAGTAILSEARCH_BACKENDS={

Wyświetl plik

@ -11,3 +11,7 @@ class TestDBBackend(BackendTests, TestCase):
@unittest.expectedFailure
def test_callable_indexed_field(self):
super(TestDBBackend, self).test_callable_indexed_field()
@unittest.expectedFailure
def test_update_index_command(self):
super(TestDBBackend, self).test_update_index_command()