Added a failing test to demonstrate that respecting the current site isn't working.
This commit is contained in:
parent
8d3bc1a4ee
commit
8112cf383d
|
@ -310,6 +310,36 @@ class XapianSearchBackendTestCase(TestCase):
|
|||
# Ensure that swapping the ``result_class`` works.
|
||||
self.assertTrue(isinstance(self.backend.more_like_this(self.sample_objs[0], result_class=MockSearchResult)['results'][0], MockSearchResult))
|
||||
|
||||
def test_use_correct_site(self):
|
||||
test_site = SearchSite()
|
||||
test_site.register(XapianMockModel, XapianMockSearchIndex)
|
||||
self.backend.update(self.index, self.sample_objs)
|
||||
|
||||
# Make sure that ``_process_results`` uses the right ``site``.
|
||||
self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 3)
|
||||
self.assertEqual([result.pk for result in self.backend.search(xapian.Query('indexed'))['results']], [1, 2, 3])
|
||||
|
||||
self.site.unregister(XapianMockModel)
|
||||
self.assertEqual(len(self.site.get_indexed_models()), 0)
|
||||
self.backend.site = test_site
|
||||
self.assertTrue(len(self.backend.site.get_indexed_models()) > 0)
|
||||
|
||||
# Should still be there, despite the main ``site`` not having that model
|
||||
# registered any longer.
|
||||
self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 3)
|
||||
self.assertEqual([result.pk for result in self.backend.search(xapian.Query('indexed'))['results']], [1, 2, 3])
|
||||
|
||||
# Unregister it on the backend & make sure it takes effect.
|
||||
self.backend.site.unregister(XapianMockModel)
|
||||
self.assertEqual(len(self.backend.site.get_indexed_models()), 0)
|
||||
self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 0)
|
||||
|
||||
# Nuke it & fallback on the main ``site``.
|
||||
self.backend.site = haystack.site
|
||||
self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 0)
|
||||
self.site.register(XapianMockModel, XapianMockSearchIndex)
|
||||
self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 3)
|
||||
|
||||
def test_order_by(self):
|
||||
self.backend.update(self.index, self.sample_objs)
|
||||
self.assertEqual(self.backend.document_count(), 3)
|
||||
|
|
|
@ -354,6 +354,11 @@ class SearchBackend(BaseSearchBackend):
|
|||
and any suggestions for spell correction will be returned as well as
|
||||
the results.
|
||||
"""
|
||||
if not self.site:
|
||||
from haystack import site
|
||||
else:
|
||||
site = self.site
|
||||
|
||||
if xapian.Query.empty(query):
|
||||
return {
|
||||
'results': [],
|
||||
|
@ -476,6 +481,11 @@ class SearchBackend(BaseSearchBackend):
|
|||
|
||||
Finally, processes the resulting matches and returns.
|
||||
"""
|
||||
if not self.site:
|
||||
from haystack import site
|
||||
else:
|
||||
site = self.site
|
||||
|
||||
database = self._database()
|
||||
|
||||
if result_class is None:
|
||||
|
@ -527,7 +537,7 @@ class SearchBackend(BaseSearchBackend):
|
|||
for match in matches:
|
||||
app_label, module_name, pk, model_data = pickle.loads(self._get_document_data(database, match.document))
|
||||
results.append(
|
||||
result_class(app_label, module_name, pk, match.percent, **model_data)
|
||||
result_class(app_label, module_name, pk, match.percent, searchsite=site, **model_data)
|
||||
)
|
||||
|
||||
return {
|
||||
|
|
Loading…
Reference in New Issue
Block a user