kopia lustrzana https://github.com/wagtail/wagtail
Merge branch 'searchchanges/tests' of https://github.com/kaedroho/wagtail into kaedroho-searchchanges/tests
commit
f5107f7d44
|
@ -11,6 +11,7 @@ from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
|
|||
from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel
|
||||
from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField
|
||||
from wagtail.wagtailsnippets.models import register_snippet
|
||||
from wagtail.wagtailsearch import indexed
|
||||
|
||||
|
||||
EVENT_AUDIENCE_CHOICES = (
|
||||
|
@ -331,3 +332,60 @@ class BusinessSubIndex(Page):
|
|||
|
||||
class BusinessChild(Page):
|
||||
subpage_types = []
|
||||
|
||||
|
||||
class SearchTest(models.Model, indexed.Indexed):
|
||||
title = models.CharField(max_length=255)
|
||||
content = models.TextField()
|
||||
live = models.BooleanField(default=False)
|
||||
published_date = models.DateField(null=True)
|
||||
|
||||
search_fields = [
|
||||
indexed.SearchField('title', partial_match=True),
|
||||
indexed.SearchField('content'),
|
||||
indexed.SearchField('callable_indexed_field'),
|
||||
indexed.FilterField('title'),
|
||||
indexed.FilterField('live'),
|
||||
indexed.FilterField('published_date'),
|
||||
]
|
||||
|
||||
def callable_indexed_field(self):
|
||||
return "Callable"
|
||||
|
||||
|
||||
class SearchTestChild(SearchTest):
|
||||
subtitle = models.CharField(max_length=255, null=True, blank=True)
|
||||
extra_content = models.TextField()
|
||||
|
||||
search_fields = SearchTest.search_fields + [
|
||||
indexed.SearchField('subtitle', partial_match=True),
|
||||
indexed.SearchField('extra_content'),
|
||||
]
|
||||
|
||||
|
||||
class SearchTestOldConfig(models.Model, indexed.Indexed):
|
||||
"""
|
||||
This tests that the Indexed class can correctly handle models that
|
||||
use the old "indexed_fields" configuration format.
|
||||
"""
|
||||
indexed_fields = {
|
||||
# A search field with predictive search and boosting
|
||||
'title': {
|
||||
'type': 'string',
|
||||
'analyzer': 'edgengram_analyzer',
|
||||
'boost': 100,
|
||||
},
|
||||
|
||||
# A filter field
|
||||
'live': {
|
||||
'type': 'boolean',
|
||||
'index': 'not_analyzed',
|
||||
},
|
||||
}
|
||||
|
||||
class SearchTestOldConfigList(models.Model, indexed.Indexed):
|
||||
"""
|
||||
This tests that the Indexed class can correctly handle models that
|
||||
use the old "indexed_fields" configuration format using a list.
|
||||
"""
|
||||
indexed_fields = ['title', 'content']
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from south.utils import datetime_utils as datetime
|
||||
from south.db import db
|
||||
from south.v2 import SchemaMigration
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Migration(SchemaMigration):
|
||||
|
||||
def forwards(self, orm):
|
||||
# Deleting model 'SearchTestChild'
|
||||
db.delete_table('wagtailsearch_searchtestchild')
|
||||
|
||||
# Deleting model 'SearchTest'
|
||||
db.delete_table('wagtailsearch_searchtest')
|
||||
|
||||
|
||||
def backwards(self, orm):
|
||||
# Adding model 'SearchTestChild'
|
||||
db.create_table('wagtailsearch_searchtestchild', (
|
||||
('extra_content', self.gf('django.db.models.fields.TextField')()),
|
||||
('searchtest_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['wagtailsearch.SearchTest'], unique=True, primary_key=True)),
|
||||
))
|
||||
db.send_create_signal('wagtailsearch', ['SearchTestChild'])
|
||||
|
||||
# Adding model 'SearchTest'
|
||||
db.create_table('wagtailsearch_searchtest', (
|
||||
('content', self.gf('django.db.models.fields.TextField')()),
|
||||
('live', self.gf('django.db.models.fields.BooleanField')(default=False)),
|
||||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
|
||||
('title', self.gf('django.db.models.fields.CharField')(max_length=255)),
|
||||
))
|
||||
db.send_create_signal('wagtailsearch', ['SearchTest'])
|
||||
|
||||
|
||||
models = {
|
||||
'auth.group': {
|
||||
'Meta': {'object_name': 'Group'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
|
||||
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
|
||||
},
|
||||
'auth.permission': {
|
||||
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
|
||||
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
|
||||
},
|
||||
'auth.user': {
|
||||
'Meta': {'object_name': 'User'},
|
||||
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
|
||||
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'user_set'", 'blank': 'True', 'to': "orm['auth.Group']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
|
||||
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
|
||||
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'user_set'", 'blank': 'True', 'to': "orm['auth.Permission']"}),
|
||||
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
|
||||
},
|
||||
'contenttypes.contenttype': {
|
||||
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
|
||||
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
|
||||
},
|
||||
'wagtailcore.page': {
|
||||
'Meta': {'object_name': 'Page'},
|
||||
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'pages'", 'to': "orm['contenttypes.ContentType']"}),
|
||||
'depth': ('django.db.models.fields.PositiveIntegerField', [], {}),
|
||||
'has_unpublished_changes': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'live': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
|
||||
'numchild': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
|
||||
'owner': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'owned_pages'", 'null': 'True', 'to': "orm['auth.User']"}),
|
||||
'path': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
|
||||
'search_description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
|
||||
'seo_title': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
|
||||
'show_in_menus': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}),
|
||||
'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'url_path': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'})
|
||||
},
|
||||
'wagtailsearch.editorspick': {
|
||||
'Meta': {'ordering': "('sort_order',)", 'object_name': 'EditorsPick'},
|
||||
'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'page': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['wagtailcore.Page']"}),
|
||||
'query': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'editors_picks'", 'to': "orm['wagtailsearch.Query']"}),
|
||||
'sort_order': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'})
|
||||
},
|
||||
'wagtailsearch.query': {
|
||||
'Meta': {'object_name': 'Query'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'query_string': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
|
||||
},
|
||||
'wagtailsearch.querydailyhits': {
|
||||
'Meta': {'unique_together': "(('query', 'date'),)", 'object_name': 'QueryDailyHits'},
|
||||
'date': ('django.db.models.fields.DateField', [], {}),
|
||||
'hits': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'query': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'daily_hits'", 'to': "orm['wagtailsearch.Query']"})
|
||||
}
|
||||
}
|
||||
|
||||
complete_apps = ['wagtailsearch']
|
|
@ -81,29 +81,3 @@ class EditorsPick(models.Model):
|
|||
|
||||
class Meta:
|
||||
ordering = ('sort_order', )
|
||||
|
||||
|
||||
# Used for tests
|
||||
|
||||
class SearchTest(models.Model, indexed.Indexed):
|
||||
title = models.CharField(max_length=255)
|
||||
content = models.TextField()
|
||||
live = models.BooleanField(default=False)
|
||||
|
||||
search_fields = (
|
||||
indexed.SearchField('title'),
|
||||
indexed.SearchField('content'),
|
||||
indexed.SearchField('callable_indexed_field'),
|
||||
indexed.FilterField('live'),
|
||||
)
|
||||
|
||||
def callable_indexed_field(self):
|
||||
return "Callable"
|
||||
|
||||
|
||||
class SearchTestChild(SearchTest):
|
||||
extra_content = models.TextField()
|
||||
|
||||
search_fields = SearchTest.search_fields + (
|
||||
indexed.SearchField('extra_content'),
|
||||
)
|
||||
|
|
|
@ -6,9 +6,9 @@ from django.conf import settings
|
|||
from django.core import management
|
||||
|
||||
from wagtail.tests.utils import unittest
|
||||
from wagtail.wagtailsearch import models, get_search_backend
|
||||
from wagtail.tests import models
|
||||
from wagtail.wagtailsearch.backends import get_search_backend, InvalidSearchBackendError
|
||||
from wagtail.wagtailsearch.backends.db import DBSearch
|
||||
from wagtail.wagtailsearch.backends import InvalidSearchBackendError
|
||||
|
||||
|
||||
class BackendTests(object):
|
||||
|
@ -151,27 +151,6 @@ class BackendTests(object):
|
|||
self.assertEqual(len(results), 3)
|
||||
|
||||
|
||||
class TestDBBackend(BackendTests, TestCase):
|
||||
backend_path = 'wagtail.wagtailsearch.backends.db.DBSearch'
|
||||
|
||||
@unittest.expectedFailure
|
||||
def test_callable_indexed_field(self):
|
||||
super(TestDBBackend, self).test_callable_indexed_field()
|
||||
|
||||
|
||||
class TestElasticSearchBackend(BackendTests, TestCase):
|
||||
backend_path = 'wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch'
|
||||
|
||||
def test_search_with_spaces_only(self):
|
||||
# Search for some space characters and hope it doesn't crash
|
||||
results = self.backend.search(" ", models.SearchTest)
|
||||
|
||||
# Queries are lazily evaluated, force it to run
|
||||
list(results)
|
||||
|
||||
# Didn't crash, yay!
|
||||
|
||||
|
||||
@override_settings(WAGTAILSEARCH_BACKENDS={
|
||||
'default': {'BACKEND': 'wagtail.wagtailsearch.backends.db.DBSearch'}
|
||||
})
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
from wagtail.tests.utils import unittest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from .test_backends import BackendTests
|
||||
|
||||
|
||||
class TestDBBackend(BackendTests, TestCase):
|
||||
backend_path = 'wagtail.wagtailsearch.backends.db.DBSearch'
|
||||
|
||||
@unittest.expectedFailure
|
||||
def test_callable_indexed_field(self):
|
||||
super(TestDBBackend, self).test_callable_indexed_field()
|
|
@ -0,0 +1,376 @@
|
|||
from wagtail.tests.utils import unittest
|
||||
import datetime
|
||||
import json
|
||||
|
||||
from django.test import TestCase
|
||||
from django.db.models import Q
|
||||
|
||||
from wagtail.tests import models
|
||||
from .test_backends import BackendTests
|
||||
|
||||
|
||||
class TestElasticSearchBackend(BackendTests, TestCase):
|
||||
backend_path = 'wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch'
|
||||
|
||||
def test_search_with_spaces_only(self):
|
||||
# Search for some space characters and hope it doesn't crash
|
||||
results = self.backend.search(" ", models.SearchTest)
|
||||
|
||||
# Queries are lazily evaluated, force it to run
|
||||
list(results)
|
||||
|
||||
# Didn't crash, yay!
|
||||
|
||||
def test_partial_search(self):
|
||||
# Reset the index
|
||||
self.backend.reset_index()
|
||||
self.backend.add_type(models.SearchTest)
|
||||
self.backend.add_type(models.SearchTestChild)
|
||||
|
||||
# Add some test data
|
||||
obj = models.SearchTest()
|
||||
obj.title = "HelloWorld"
|
||||
obj.live = True
|
||||
obj.save()
|
||||
self.backend.add(obj)
|
||||
|
||||
# Refresh the index
|
||||
self.backend.refresh_index()
|
||||
|
||||
# Search and check
|
||||
results = self.backend.search("HelloW", models.SearchTest.objects.all())
|
||||
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results[0].id, obj.id)
|
||||
|
||||
def test_child_partial_search(self):
|
||||
# Reset the index
|
||||
self.backend.reset_index()
|
||||
self.backend.add_type(models.SearchTest)
|
||||
self.backend.add_type(models.SearchTestChild)
|
||||
|
||||
obj = models.SearchTestChild()
|
||||
obj.title = "WorldHello"
|
||||
obj.subtitle = "HelloWorld"
|
||||
obj.live = True
|
||||
obj.save()
|
||||
self.backend.add(obj)
|
||||
|
||||
# Refresh the index
|
||||
self.backend.refresh_index()
|
||||
|
||||
# Search and check
|
||||
results = self.backend.search("HelloW", models.SearchTest.objects.all())
|
||||
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results[0].id, obj.id)
|
||||
|
||||
|
||||
class TestElasticSearchQuery(TestCase):
|
||||
def assertDictEqual(self, a, b):
|
||||
default = self.JSONSerializer().default
|
||||
self.assertEqual(json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default))
|
||||
|
||||
def setUp(self):
|
||||
# Import using a try-catch block to prevent crashes if the elasticsearch-py
|
||||
# module is not installed
|
||||
try:
|
||||
from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchQuery
|
||||
from elasticsearch.serializer import JSONSerializer
|
||||
except ImportError:
|
||||
raise unittest.SkipTest("elasticsearch-py not installed")
|
||||
|
||||
self.ElasticSearchQuery = ElasticSearchQuery
|
||||
self.JSONSerializer = JSONSerializer
|
||||
|
||||
def test_simple(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.all(), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'tests_searchtest'}}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_none_query_string(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.all(), None)
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'tests_searchtest'}}, 'query': {'match_all': {}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_filter(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'term': {'title_filter': 'Test'}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_and_filter(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title="Test", live=True), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'and': [{'term': {'live_filter': True}}, {'term': {'title_filter': 'Test'}}]}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
|
||||
# Make sure field filters are sorted (as they can be in any order which may cause false positives)
|
||||
query = query.to_es()
|
||||
field_filters = query['filtered']['filter']['and'][1]['and']
|
||||
field_filters[:] = sorted(field_filters, key=lambda f: list(f['term'].keys())[0])
|
||||
|
||||
self.assertDictEqual(query, expected_result)
|
||||
|
||||
def test_or_filter(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(Q(title="Test") | Q(live=True)), "Hello")
|
||||
|
||||
# Make sure field filters are sorted (as they can be in any order which may cause false positives)
|
||||
query = query.to_es()
|
||||
field_filters = query['filtered']['filter']['and'][1]['or']
|
||||
field_filters[:] = sorted(field_filters, key=lambda f: list(f['term'].keys())[0])
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'or': [{'term': {'live_filter': True}}, {'term': {'title_filter': 'Test'}}]}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query, expected_result)
|
||||
|
||||
def test_negated_filter(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.exclude(live=True), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'not': {'term': {'live_filter': True}}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_fields(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.all(), "Hello", fields=['title'])
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'prefix': {'content_type': 'tests_searchtest'}}, 'query': {'query_string': {'query': 'Hello', 'fields': ['title']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_exact_lookup(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title__exact="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'term': {'title_filter': 'Test'}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_none_lookup(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title=None), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'missing': {'field': 'title_filter'}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_isnull_true_lookup(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title__isnull=True), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'missing': {'field': 'title_filter'}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_isnull_false_lookup(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title__isnull=False), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'not': {'missing': {'field': 'title_filter'}}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_startswith_lookup(self):
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(title__startswith="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'prefix': {'title_filter': 'Test'}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_gt_lookup(self):
|
||||
# This shares the same code path as gte, lt and lte so theres no need to test those
|
||||
# This also tests conversion of python dates to strings
|
||||
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(published_date__gt=datetime.datetime(2014, 4, 29)), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'range': {'published_date_filter': {'gt': '2014-04-29'}}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
def test_range_lookup(self):
|
||||
start_date = datetime.datetime(2014, 4, 29)
|
||||
end_date = datetime.datetime(2014, 8, 19)
|
||||
|
||||
# Create a query
|
||||
query = self.ElasticSearchQuery(models.SearchTest.objects.filter(published_date__range=(start_date, end_date)), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [{'prefix': {'content_type': 'tests_searchtest'}}, {'range': {'published_date_filter': {'gte': '2014-04-29', 'lte': '2014-08-19'}}}]}, 'query': {'query_string': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.to_es(), expected_result)
|
||||
|
||||
|
||||
class TestElasticSearchMapping(TestCase):
|
||||
def assertDictEqual(self, a, b):
|
||||
default = self.JSONSerializer().default
|
||||
self.assertEqual(json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default))
|
||||
|
||||
def setUp(self):
|
||||
# Import using a try-catch block to prevent crashes if the elasticsearch-py
|
||||
# module is not installed
|
||||
try:
|
||||
from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchMapping
|
||||
from elasticsearch.serializer import JSONSerializer
|
||||
except ImportError:
|
||||
raise unittest.SkipTest("elasticsearch-py not installed")
|
||||
|
||||
self.JSONSerializer = JSONSerializer
|
||||
|
||||
# Create ES mapping
|
||||
self.es_mapping = ElasticSearchMapping(models.SearchTest)
|
||||
|
||||
# Create ES document
|
||||
self.obj = models.SearchTest(title="Hello")
|
||||
self.obj.save()
|
||||
|
||||
def test_get_document_type(self):
|
||||
self.assertEqual(self.es_mapping.get_document_type(), 'tests_searchtest')
|
||||
|
||||
def test_get_mapping(self):
|
||||
# Build mapping
|
||||
mapping = self.es_mapping.get_mapping()
|
||||
|
||||
# Check
|
||||
expected_result = {
|
||||
'tests_searchtest': {
|
||||
'properties': {
|
||||
'pk': {'index': 'not_analyzed', 'type': 'string', 'store': 'yes', 'include_in_all': False},
|
||||
'content_type': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'_partials': {'analyzer': 'edgengram_analyzer', 'include_in_all': False, 'type': 'string'},
|
||||
'live_filter': {'index': 'not_analyzed', 'type': 'boolean', 'include_in_all': False},
|
||||
'published_date_filter': {'index': 'not_analyzed', 'type': 'date', 'include_in_all': False},
|
||||
'title': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer'},
|
||||
'title_filter': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'content': {'type': 'string', 'include_in_all': True},
|
||||
'callable_indexed_field': {'type': 'string', 'include_in_all': True}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.assertDictEqual(mapping, expected_result)
|
||||
|
||||
def test_get_document_id(self):
|
||||
self.assertEqual(self.es_mapping.get_document_id(self.obj), 'tests_searchtest:' + str(self.obj.pk))
|
||||
|
||||
def test_get_document(self):
|
||||
# Get document
|
||||
document = self.es_mapping.get_document(self.obj)
|
||||
|
||||
# Check
|
||||
expected_result = {
|
||||
'pk': str(self.obj.pk),
|
||||
'content_type': 'tests_searchtest',
|
||||
'_partials': ['Hello'],
|
||||
'live_filter': False,
|
||||
'published_date_filter': None,
|
||||
'title': 'Hello',
|
||||
'title_filter': 'Hello',
|
||||
'callable_indexed_field': 'Callable',
|
||||
'content': '',
|
||||
}
|
||||
|
||||
self.assertDictEqual(document, expected_result)
|
||||
|
||||
|
||||
class TestElasticSearchMappingInheritance(TestCase):
|
||||
def assertDictEqual(self, a, b):
|
||||
default = self.JSONSerializer().default
|
||||
self.assertEqual(json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default))
|
||||
|
||||
def setUp(self):
|
||||
# Import using a try-catch block to prevent crashes if the elasticsearch-py
|
||||
# module is not installed
|
||||
try:
|
||||
from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearchMapping
|
||||
from elasticsearch.serializer import JSONSerializer
|
||||
except ImportError:
|
||||
raise unittest.SkipTest("elasticsearch-py not installed")
|
||||
|
||||
self.JSONSerializer = JSONSerializer
|
||||
|
||||
# Create ES mapping
|
||||
self.es_mapping = ElasticSearchMapping(models.SearchTestChild)
|
||||
|
||||
# Create ES document
|
||||
self.obj = models.SearchTestChild(title="Hello", subtitle="World")
|
||||
self.obj.save()
|
||||
|
||||
def test_get_document_type(self):
|
||||
self.assertEqual(self.es_mapping.get_document_type(), 'tests_searchtest_tests_searchtestchild')
|
||||
|
||||
def test_get_mapping(self):
|
||||
# Build mapping
|
||||
mapping = self.es_mapping.get_mapping()
|
||||
|
||||
# Check
|
||||
expected_result = {
|
||||
'tests_searchtest_tests_searchtestchild': {
|
||||
'properties': {
|
||||
# New
|
||||
'extra_content': {'type': 'string', 'include_in_all': True},
|
||||
'subtitle': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer'},
|
||||
|
||||
# Inherited
|
||||
'pk': {'index': 'not_analyzed', 'type': 'string', 'store': 'yes', 'include_in_all': False},
|
||||
'content_type': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'_partials': {'analyzer': 'edgengram_analyzer', 'include_in_all': False, 'type': 'string'},
|
||||
'live_filter': {'index': 'not_analyzed', 'type': 'boolean', 'include_in_all': False},
|
||||
'published_date_filter': {'index': 'not_analyzed', 'type': 'date', 'include_in_all': False},
|
||||
'title': {'type': 'string', 'include_in_all': True, 'analyzer': 'edgengram_analyzer'},
|
||||
'title_filter': {'index': 'not_analyzed', 'type': 'string', 'include_in_all': False},
|
||||
'content': {'type': 'string', 'include_in_all': True},
|
||||
'callable_indexed_field': {'type': 'string', 'include_in_all': True}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.assertDictEqual(mapping, expected_result)
|
||||
|
||||
def test_get_document_id(self):
|
||||
# This must be tests_searchtest instead of 'tests_searchtest_tests_searchtestchild'
|
||||
# as it uses the contents base content type name.
|
||||
# This prevents the same object being accidentally indexed twice.
|
||||
self.assertEqual(self.es_mapping.get_document_id(self.obj), 'tests_searchtest:' + str(self.obj.pk))
|
||||
|
||||
def test_get_document(self):
|
||||
# Build document
|
||||
document = self.es_mapping.get_document(self.obj)
|
||||
|
||||
# Sort partials
|
||||
if '_partials' in document:
|
||||
document['_partials'].sort()
|
||||
|
||||
# Check
|
||||
expected_result = {
|
||||
# New
|
||||
'extra_content': '',
|
||||
'subtitle': 'World',
|
||||
|
||||
# Changed
|
||||
'content_type': 'tests_searchtest_tests_searchtestchild',
|
||||
|
||||
# Inherited
|
||||
'pk': str(self.obj.pk),
|
||||
'_partials': ['Hello', 'World'],
|
||||
'live_filter': False,
|
||||
'published_date_filter': None,
|
||||
'title': 'Hello',
|
||||
'title_filter': 'Hello',
|
||||
'callable_indexed_field': 'Callable',
|
||||
'content': '',
|
||||
}
|
||||
|
||||
self.assertDictEqual(document, expected_result)
|
|
@ -0,0 +1,49 @@
|
|||
from django.test import TestCase
|
||||
from wagtail.tests import models
|
||||
import json
|
||||
|
||||
from wagtail.wagtailsearch import indexed
|
||||
|
||||
|
||||
class TestContentTypeNames(TestCase):
|
||||
def test_base_content_type_name(self):
|
||||
name = models.SearchTestChild.indexed_get_toplevel_content_type()
|
||||
self.assertEqual(name, 'tests_searchtest')
|
||||
|
||||
def test_qualified_content_type_name(self):
|
||||
name = models.SearchTestChild.indexed_get_content_type()
|
||||
self.assertEqual(name, 'tests_searchtest_tests_searchtestchild')
|
||||
|
||||
|
||||
class TestIndexedFieldsBackwardsCompatibility(TestCase):
|
||||
def test_indexed_fields_backwards_compatibility(self):
|
||||
# Get search fields
|
||||
search_fields = models.SearchTestOldConfig.get_search_fields()
|
||||
|
||||
search_fields_dict = dict(
|
||||
((field.field_name, type(field)), field)
|
||||
for field in search_fields
|
||||
)
|
||||
|
||||
# Check that the fields were found
|
||||
self.assertEqual(len(search_fields_dict), 2)
|
||||
self.assertIn(('title', indexed.SearchField), search_fields_dict.keys())
|
||||
self.assertIn(('live', indexed.FilterField), search_fields_dict.keys())
|
||||
|
||||
# Check that the title field has the correct settings
|
||||
self.assertTrue(search_fields_dict[('title', indexed.SearchField)].partial_match)
|
||||
self.assertEqual(search_fields_dict[('title', indexed.SearchField)].boost, 100)
|
||||
|
||||
def test_indexed_fields_backwards_compatibility_list(self):
|
||||
# Get search fields
|
||||
search_fields = models.SearchTestOldConfigList.get_search_fields()
|
||||
|
||||
search_fields_dict = dict(
|
||||
((field.field_name, type(field)), field)
|
||||
for field in search_fields
|
||||
)
|
||||
|
||||
# Check that the fields were found
|
||||
self.assertEqual(len(search_fields_dict), 2)
|
||||
self.assertIn(('title', indexed.SearchField), search_fields_dict.keys())
|
||||
self.assertIn(('content', indexed.SearchField), search_fields_dict.keys())
|
Ładowanie…
Reference in New Issue