Skip to content

Commit 36be716

Browse files
committed
Added a spelling test case for ElasticSearch.
1 parent 35d75f8 commit 36be716

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/elasticsearch_tests/tests/elasticsearch_backend.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ def get_model(self):
5050
return MockModel
5151

5252

53+
class ElasticsearchMockSpellingIndex(indexes.SearchIndex, indexes.Indexable):
54+
text = indexes.CharField(document=True)
55+
name = indexes.CharField(model_attr='author', faceted=True)
56+
pub_date = indexes.DateField(model_attr='pub_date')
57+
58+
def get_model(self):
59+
return MockModel
60+
61+
def prepare_text(self, obj):
62+
return obj.foo
63+
64+
5365
class ElasticsearchMaintainTypeMockSearchIndex(indexes.SearchIndex, indexes.Indexable):
5466
text = indexes.CharField(document=True, use_template=True)
5567
month = indexes.CharField(indexed=False)
@@ -906,6 +918,49 @@ def test_result_class(self):
906918
self.assertTrue(isinstance(sqs[0], SearchResult))
907919

908920

921+
class LiveElasticsearchSpellingTestCase(TestCase):
922+
"""Used to test actual implementation details of the SearchQuerySet."""
923+
fixtures = ['bulk_data.json']
924+
925+
def setUp(self):
926+
super(LiveElasticsearchSpellingTestCase, self).setUp()
927+
928+
# Stow.
929+
self.old_debug = settings.DEBUG
930+
settings.DEBUG = True
931+
self.old_ui = connections['default'].get_unified_index()
932+
self.ui = UnifiedIndex()
933+
self.smmi = ElasticsearchMockSpellingIndex()
934+
self.ui.build(indexes=[self.smmi])
935+
connections['default']._index = self.ui
936+
937+
self.sqs = SearchQuerySet()
938+
939+
# Ugly but not constantly reindexing saves us almost 50% runtime.
940+
global lssqstc_all_loaded
941+
942+
if lssqstc_all_loaded is None:
943+
lssqstc_all_loaded = True
944+
945+
# Wipe it clean.
946+
clear_elasticsearch_index()
947+
948+
# Force indexing of the content.
949+
self.smmi.update()
950+
951+
def tearDown(self):
952+
# Restore.
953+
connections['default']._index = self.old_ui
954+
settings.DEBUG = self.old_debug
955+
super(LiveElasticsearchSpellingTestCase, self).tearDown()
956+
957+
def test_spelling(self):
958+
self.assertEqual(self.sqs.auto_query('structurd').spelling_suggestion(), 'structured')
959+
self.assertEqual(self.sqs.spelling_suggestion('structurd'), 'structured')
960+
self.assertEqual(self.sqs.auto_query('srchindex instanc').spelling_suggestion(), 'searchindex instance')
961+
self.assertEqual(self.sqs.spelling_suggestion('srchindex instanc'), 'searchindex instance')
962+
963+
909964
class LiveElasticsearchMoreLikeThisTestCase(TestCase):
910965
fixtures = ['bulk_data.json']
911966

0 commit comments

Comments
 (0)