Permalink
Browse files

Basic tests for filtering index_querysets based on using value

  • Loading branch information...
1 parent a0a6254 commit 0ede9ade55aa0f38470c85e7519bf52ad64fa756 @acdha committed Mar 12, 2012
@@ -19,7 +19,7 @@ class DogIndex(indexes.SearchIndex, indexes.Indexable):
def get_model(self):
return Dog
- def index_queryset(self):
+ def index_queryset(self, using=None):
return self.get_model().objects.filter(public=True)
def prepare_toys(self, obj):
View
@@ -76,7 +76,7 @@ class NoteIndex(indexes.SearchIndex, indexes.Indexable):
def get_model(self):
return Note
- def index_queryset(self):
+ def index_queryset(self, using=None):
return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())
"""
@@ -67,10 +67,10 @@ def load_all_queryset(self):
def get_model(self):
return MockModel
- def index_queryset(self):
+ def index_queryset(self, using=None):
return MockModel.objects.all()
- def read_queryset(self):
+ def read_queryset(self, using=None):
return MockModel.objects.filter(author__in=['daniel1', 'daniel3'])
def build_queryset(self, start_date=None, end_date=None):
@@ -525,11 +525,11 @@ class GhettoAFifthMockModelSearchIndex(indexes.SearchIndex, indexes.Indexable):
def get_model(self):
return AFifthMockModel
- def index_queryset(self):
+ def index_queryset(self, using=None):
# Index everything,
return self.get_model().objects.complete_set()
- def read_queryset(self):
+ def read_queryset(self, using=None):
return self.get_model().objects.all()
@@ -539,7 +539,7 @@ class ReadQuerySetTestSearchIndex(indexes.SearchIndex, indexes.Indexable):
def get_model(self):
return AFifthMockModel
- def read_queryset(self):
+ def read_queryset(self, using=None):
return self.get_model().objects.complete_set()
@@ -549,7 +549,7 @@ class TextReadQuerySetTestSearchIndex(indexes.SearchIndex, indexes.Indexable):
def get_model(self):
return AFifthMockModel
- def read_queryset(self):
+ def read_queryset(self, using=None):
return self.get_model().objects.complete_set()
@@ -4,14 +4,14 @@
class Foo(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
-
+
def __unicode__(self):
return self.title
class Bar(models.Model):
author = models.CharField(max_length=255)
content = models.TextField()
-
+
def __unicode__(self):
return self.author
@@ -11,7 +11,11 @@ def get_model(self):
class FooIndex(BaseIndex, indexes.Indexable):
- pass
+ def index_queryset(self, using=None):
+ qs = super(FooIndex, self).index_queryset(using=using)
+ if using == "filtered_whoosh":
+ qs = qs.filter(body__contains="1")
+ return qs
# Import the old way & make sure things don't explode.
@@ -1,13 +1,16 @@
import os
import shutil
+
from django.conf import settings
from django.db import models
from django.test import TestCase
+
from haystack import connections, connection_router
from haystack.exceptions import NotHandled
from haystack.query import SearchQuerySet
from haystack.signals import BaseSignalProcessor, RealtimeSignalProcessor
from haystack.utils.loading import UnifiedIndex
+
from multipleindex.search_indexes import FooIndex
from multipleindex.models import Foo, Bar
@@ -29,6 +32,7 @@ def setUp(self):
self.bi = self.ui.get_index(Bar)
self.solr_backend = connections['default'].get_backend()
self.whoosh_backend = connections['whoosh'].get_backend()
+ self.filtered_whoosh_backend = connections['filtered_whoosh'].get_backend()
foo_1 = Foo.objects.create(
title='Haystack test',
@@ -184,6 +188,17 @@ def test_excluded_indexes(self):
# Should error, since it's not present.
self.assertRaises(NotHandled, wui.get_index, Bar)
+ def test_filtered_index_update(self):
+ for i in ('whoosh', 'filtered_whoosh'):
+ self.fi.clear(using=i)
+ self.fi.update(using=i)
+
+ results = self.whoosh_backend.search('foo')
+ self.assertEqual(results['hits'], 2)
+
+ results = self.filtered_whoosh_backend.search('foo')
+ self.assertEqual(results['hits'], 1, "Filtered backend should only contain one record")
+
class TestSignalProcessor(BaseSignalProcessor):
def setup(self):
@@ -15,6 +15,11 @@
'PATH': mkdtemp(prefix='haystack-multipleindex-whoosh-tests-'),
'EXCLUDED_INDEXES': ['multipleindex.search_indexes.BarIndex'],
},
+ 'filtered_whoosh': {
+ 'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
+ 'PATH': mkdtemp(prefix='haystack-multipleindex-filtered-whoosh-tests-'),
+ 'EXCLUDED_INDEXES': ['multipleindex.search_indexes.BarIndex'],
+ },
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

0 comments on commit 0ede9ad

Please sign in to comment.