More improvements to unit tests

pull/29/head
Karl Hobley 2014-02-11 14:29:36 +00:00
rodzic cb1a412c3c
commit 9db3e1ce5b
2 zmienionych plików z 39 dodań i 5 usunięć

Wyświetl plik

@ -96,10 +96,18 @@ class SearchTest(models.Model, Indexed):
title = models.CharField(max_length=255)
content = models.TextField()
indexed_fields = ("title", "content")
indexed_fields = ("title", "content", "callable_indexed_field")
title_search = Searcher(["title"])
def object_indexed(self):
if self.title == "Don't index me!":
return False
return True
def callable_indexed_field(self):
return "Callable"
class SearchTestChild(SearchTest):
extra_content = models.TextField()

Wyświetl plik

@ -1,15 +1,18 @@
from django.test import TestCase
from django.utils import timezone
from django.core import management
from django.conf import settings
import datetime
import unittest
import models
from wagtail.wagtailsearch import models
from wagtail.wagtailsearch.backends import get_search_backend
from wagtail.wagtailsearch.backends.base import InvalidSearchBackendError
from wagtail.wagtailsearch.backends.db import DBSearch
from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch
from django.conf import settings
def find_backend(cls):
if not hasattr(settings, 'WAGTAILSEARCH_BACKENDS') and cls == DBSearch:
@ -21,6 +24,11 @@ def find_backend(cls):
class TestSearch(TestCase):
def __init__(self, *args, **kwargs):
super(TestSearch, self).__init__(*args, **kwargs)
self.backends_tested = []
def test_backend_loader(self):
# Test DB backend import
db = get_search_backend(backend='wagtail.wagtailsearch.backends.db.DBSearch')
@ -34,6 +42,11 @@ class TestSearch(TestCase):
self.assertRaises(InvalidSearchBackendError, get_search_backend, backend='wagtail.wagtailsearch.backends.doesntexist.DoesntExist')
def test_search(self, backend='default'):
# Don't test the same backend more than once!
if backend in self.backends_tested:
return
self.backends_tested.append(backend)
# Get search backend and reset the index
s = get_search_backend(backend=backend)
s.reset_index()
@ -61,7 +74,7 @@ class TestSearch(TestCase):
results = s.search("Hello", models.SearchTest)
self.assertEqual(len(results), 3)
# Ordinary search on "World"
# Ordinary search on "World"
results = s.search("World", models.SearchTest)
self.assertEqual(len(results), 1)
@ -77,6 +90,16 @@ class TestSearch(TestCase):
results = models.SearchTestChild.title_search("Hello")
self.assertEqual(len(results), 1)
# Reset the index, this should clear out the index (but doesn't have to!)
s.reset_index()
# Run update_index command
management.call_command('update_index', backend, interactive=False, quiet=True)
# Should have results again now
results = s.search("Hello", models.SearchTest)
self.assertEqual(len(results), 3)
def test_db_backend(self):
self.test_search(backend='wagtail.wagtailsearch.backends.db.DBSearch')
@ -191,4 +214,7 @@ class TestSearch(TestCase):
pass
def test_garbage_collect(self):
pass
pass
def test_suggestions(self):
pass