diff --git a/tests/xapian_tests/tests/test_xapian_backend.py b/tests/xapian_tests/tests/test_xapian_backend.py index a12296c..58334d9 100644 --- a/tests/xapian_tests/tests/test_xapian_backend.py +++ b/tests/xapian_tests/tests/test_xapian_backend.py @@ -12,7 +12,7 @@ from django.test import TestCase from haystack import connections, reset_search_queries from haystack import indexes -from haystack.backends.xapian_backend import _marshal_value +from haystack.backends.xapian_backend import InvalidIndexError, _marshal_value from haystack.models import SearchResult from haystack.query import SearchQuerySet, SQ from haystack.utils.loading import UnifiedIndex @@ -454,6 +454,24 @@ class XapianSearchBackendTestCase(HaystackBackendTestCase, TestCase): self.assertEqual(str(self.backend.parse_query('popularity:25.5..100.0')), b'Xapian::Query(VALUE_RANGE 7 \xb2` \xba@)') + def test_more_like_this_with_unindexed_model(self): + """ + Tests that more_like_this raises an error when it is called + with an unindexed model and if silently_fail is True. + Also tests the other way around. + """ + mock = XapianMockModel() + mock.id = 10 + mock.author = 'david10' + + try: + self.assertEqual(self.backend.more_like_this(mock)['results'], []) + except InvalidIndexError: + self.fail("InvalidIndexError raised when silently_fail is True") + + self.backend.silently_fail = False + self.assertRaises(InvalidIndexError, self.backend.more_like_this, mock) + class LiveXapianMockSearchIndex(indexes.SearchIndex): text = indexes.CharField(document=True, use_template=True) diff --git a/xapian_backend.py b/xapian_backend.py index cf6ad0e..6ed71c4 100755 --- a/xapian_backend.py +++ b/xapian_backend.py @@ -530,9 +530,18 @@ class XapianSearchBackend(BaseSearchBackend): if not end_offset: end_offset = database.get_doccount() + match = None for match in self._get_enquire_mset(database, enquire, 0, end_offset): rset.add_document(match.docid) + if match is None: + if not self.silently_fail: + raise InvalidIndexError('Instance %s with id "%d" not indexed' % + (get_identifier(model_instance), model_instance.id)) + else: + return {'results': [], + 'hits': 0} + query = xapian.Query( xapian.Query.OP_ELITE_SET, [expand.term for expand in enquire.get_eset(match.document.termlist_count(), rset, XHExpandDecider())],