0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Rewrite Elasticsearch specific tests

This commit is contained in:
Karl Hobley 2017-10-19 13:17:49 +01:00
parent 6a52ae0494
commit f52c8c4433
4 changed files with 132 additions and 3 deletions

View File

@ -0,0 +1,126 @@
from datetime import date
from django.core import management
from django.utils.six import StringIO
from wagtail.tests.search import models
class ElasticsearchCommonSearchBackendTests(object):
def test_search_with_spaces_only(self):
# Search for some space characters and hope it doesn't crash
results = self.backend.search(" ", models.Book)
# Queries are lazily evaluated, force it to run
list(results)
# Didn't crash, yay!
def test_filter_with_unsupported_lookup_type(self):
"""
Not all lookup types are supported by the Elasticsearch backends
"""
from wagtail.wagtailsearch.backends.base import FilterError
with self.assertRaises(FilterError):
list(self.backend.search("Hello", models.Book.objects.filter(title__iregex='h(ea)llo')))
def test_partial_search(self):
results = self.backend.search("Java", models.Book)
self.assertEqual(set(r.title for r in results), {
"JavaScript: The Definitive Guide",
"JavaScript: The good parts"
})
def test_child_partial_search(self):
# Note: Expands to "Westeros". Which is in a field on Novel.setting
results = self.backend.search("Wes", models.Book)
self.assertEqual(set(r.title for r in results), {
"A Game of Thrones",
"A Storm of Swords",
"A Clash of Kings"
})
def test_ascii_folding(self):
book = models.Book.objects.create(
title="Ĥéllø",
publication_date=date(2017, 10, 19),
number_of_pages=1
)
index = self.backend.get_index_for_model(models.Book)
index.add_item(book)
index.refresh()
results = self.backend.search("Hello", models.Book)
self.assertEqual(set(r.title for r in results), {
"Ĥéllø"
})
def test_query_analyser(self):
# This is testing that fields that use edgengram_analyzer as their index analyser do not
# have it also as their query analyser
results = self.backend.search("JavaScript", models.Book)
self.assertEqual(set(r.title for r in results), {
"JavaScript: The Definitive Guide",
"JavaScript: The good parts"
})
# Even though they both start with "Java", this should not match the "JavaScript" books
results = self.backend.search("JavaBeans", models.Book)
self.assertEqual(set(r.title for r in results), {})
def test_search_with_hyphen(self):
"""
This tests that punctuation characters are treated the same
way in both indexing and querying.
See: https://github.com/wagtail/wagtail/issues/937
"""
book = models.Book.objects.create(
title="Harry Potter and the Half-Blood Prince",
publication_date=date(2009, 7, 15),
number_of_pages=607
)
index = self.backend.get_index_for_model(models.Book)
index.add_item(book)
index.refresh()
results = self.backend.search("Half-Blood", models.Book)
self.assertEqual(set(r.title for r in results), {
"Harry Potter and the Half-Blood Prince",
})
def test_and_operator_with_single_field(self):
# Testing for bug #1859
results = self.backend.search("JavaScript", models.Book, operator='and', fields=['title'])
self.assertEqual(set(r.title for r in results), {
"JavaScript: The Definitive Guide",
"JavaScript: The good parts"
})
def test_update_index_command_schema_only(self):
management.call_command(
'update_index', backend_name=self.backend_name, schema_only=True, interactive=False, stdout=StringIO()
)
# This should not give any results
results = self.backend.search(None, models.Book)
self.assertEqual(set(results), set())
def test_annotate_score(self):
results = self.backend.search("JavaScript", models.Book).annotate_score('_score')
for result in results:
self.assertIsInstance(result._score, float)
def test_annotate_score_with_slice(self):
# #3431 - Annotate score wasn't being passed to new queryset when slicing
results = self.backend.search("JavaScript", models.Book).annotate_score('_score')[:10]
for result in results:
self.assertIsInstance(result._score, float)

View File

@ -19,10 +19,11 @@ from wagtail.wagtailsearch.backends import get_search_backend
from wagtail.wagtailsearch.backends.elasticsearch2 import (
Elasticsearch2SearchBackend, get_model_root)
from .elasticsearch_common_tests import ElasticsearchCommonSearchBackendTests
from .test_backends import BackendTests
class TestElasticsearch2SearchBackend(BackendTests, TestCase):
class TestElasticsearch2SearchBackend(BackendTests, ElasticsearchCommonSearchBackendTests, TestCase):
backend_path = 'wagtail.wagtailsearch.backends.elasticsearch2'
# Broken

View File

@ -18,10 +18,11 @@ from wagtail.tests.search import models
from wagtail.wagtailsearch.backends import get_search_backend
from wagtail.wagtailsearch.backends.elasticsearch5 import Elasticsearch5SearchBackend
from .elasticsearch_common_tests import ElasticsearchCommonSearchBackendTests
from .test_backends import BackendTests
class TestElasticsearch5SearchBackend(BackendTests, TestCase):
class TestElasticsearch5SearchBackend(BackendTests, ElasticsearchCommonSearchBackendTests, TestCase):
backend_path = 'wagtail.wagtailsearch.backends.elasticsearch5'
# Broken

View File

@ -18,10 +18,11 @@ from wagtail.tests.search import models
from wagtail.wagtailsearch.backends import get_search_backend
from wagtail.wagtailsearch.backends.elasticsearch import ElasticsearchSearchBackend
from .elasticsearch_common_tests import ElasticsearchCommonSearchBackendTests
from .test_backends import BackendTests
class TestElasticsearchSearchBackend(BackendTests, TestCase):
class TestElasticsearchSearchBackend(BackendTests, ElasticsearchCommonSearchBackendTests, TestCase):
backend_path = 'wagtail.wagtailsearch.backends.elasticsearch'
# Broken