Added tests for querying and indexing django.contrib.contenttype.

This commit is contained in:
Jorge C. Leitão 2017-03-01 11:20:27 +01:00
parent 390d6344cf
commit de7a59689d
5 changed files with 39 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import os
from .settings import *
INSTALLED_APPS = [
'django.contrib.contenttypes',
'test_haystack.core',
'test_haystack.xapian_tests',
]

View File

@ -1,4 +1,5 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType
from ..core.models import MockTag, AnotherMockModel, MockModel, AFourthMockModel
@ -34,3 +35,7 @@ class BlogEntry(models.Model):
number = models.IntegerField()
float_number = models.FloatField()
decimal_number = models.DecimalField(max_digits=4, decimal_places=2)
class DjangoContentType(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)

View File

@ -122,6 +122,13 @@ class XapianEdgeNGramIndex(indexes.SearchIndex):
return models.BlogEntry
class DjangoContentTypeIndex(indexes.SearchIndex):
text = indexes.CharField(document=True)
def get_model(self):
return models.DjangoContentType
class MockSearchIndex(indexes.SearchIndex):
text = indexes.CharField(document=True, use_template=True)
name = indexes.CharField(model_attr='author', faceted=True)

View File

@ -8,6 +8,7 @@ import subprocess
import os
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.utils.encoding import force_text
@ -17,8 +18,8 @@ from haystack.models import SearchResult
from haystack.utils.loading import UnifiedIndex
from ..search_indexes import XapianNGramIndex, XapianEdgeNGramIndex, \
CompleteBlogEntryIndex, BlogSearchIndex
from ..models import BlogEntry, AnotherMockModel, MockTag
CompleteBlogEntryIndex, BlogSearchIndex, DjangoContentTypeIndex
from ..models import BlogEntry, AnotherMockModel, MockTag, DjangoContentType
XAPIAN_VERSION = [int(x) for x in xapian.__version__.split('.')]
@ -772,3 +773,22 @@ class IndexationEdgeNGramTestCase(HaystackBackendTestCase, TestCase):
[1])
self.assertEqual(pks(self.backend.search(xapian.Query('da1'))['results']),
[2])
class IndexationDjangoContentTypeTestCase(HaystackBackendTestCase, TestCase):
def get_index(self):
return DjangoContentTypeIndex()
def setUp(self):
super(IndexationDjangoContentTypeTestCase, self).setUp()
entry1 = ContentType(model='DjangoContentType')
entry1.save()
entry2 = DjangoContentType(content_type=entry1)
entry2.save()
self.backend.update(self.index, [entry2])
def test_basic(self):
terms = get_terms(self.backend, '-a')
self.assertTrue('CONTENTTYPExapian_tests.djangocontenttype' in terms)

View File

@ -244,6 +244,10 @@ class XapianSearchQueryTestCase(HaystackBackendTestCase, TestCase):
'(XTITLE^ PHRASE 3 XTITLE2 PHRASE 3 XTITLE$) OR '
'(XTITLE^ PHRASE 3 XTITLE3 PHRASE 3 XTITLE$)))')
def test_content_type(self):
self.sq.add_filter(SQ(django_ct='time'))
self.assertExpectedQuery(self.sq.build_query(), 'CONTENTTYPEtime')
class SearchQueryTestCase(HaystackBackendTestCase, TestCase):
"""