djangocms_blog/tests/test_search.py

50 lines
2.1 KiB
Python
Raw Normal View History

2015-10-15 20:29:32 +00:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
2015-10-20 17:27:48 +00:00
from haystack.constants import DEFAULT_ALIAS
from haystack.query import SearchQuerySet
2015-10-15 20:29:32 +00:00
2015-10-19 21:26:05 +00:00
from djangocms_blog.models import Post
2015-10-20 17:27:48 +00:00
from djangocms_blog.search_indexes import PostIndex
2015-10-19 21:26:05 +00:00
2015-10-20 17:27:48 +00:00
from . import BaseTest
2015-10-15 20:29:32 +00:00
2015-10-19 20:27:05 +00:00
class BlogIndexingTests(BaseTest):
2015-10-15 20:29:32 +00:00
def setUp(self):
self.index = PostIndex()
def test_blog_post_is_indexed_using_prepare(self):
"""This tests the indexing path way used by update_index mgmt command"""
post = self._get_post(self._post_data[0]['en'])
post = self._get_post(self._post_data[0]['it'], post, 'it')
index = self.get_post_index()
index.index_queryset(DEFAULT_ALIAS) # initialises index._backend_alias
indexed = index.prepare(post)
2015-10-19 07:50:39 +00:00
2015-10-16 10:54:55 +00:00
self.assertEqual(post.get_title(), indexed['title'])
self.assertEqual(post.get_description(), indexed['description'])
2015-10-16 11:32:00 +00:00
self.assertEqual('First post First post first line This is the description category 1', indexed['text'])
2015-10-16 10:54:55 +00:00
self.assertEqual(post.get_absolute_url(), indexed['url'])
self.assertEqual(post.date_published.strftime("%Y-%m-%d %H:%M:%S"), indexed['pub_date'])
2015-10-15 20:29:32 +00:00
def test_blog_post_is_indexed_using_update_object(self):
"""This tests the indexing path way used by the RealTimeSignalProcessor"""
post = self._get_post(self._post_data[0]['en'])
post = self._get_post(self._post_data[0]['it'], post, 'it')
index = self.get_post_index()
index.update_object(post, using=DEFAULT_ALIAS)
indexed = index.prepared_data
2015-10-19 07:50:39 +00:00
2015-10-16 10:54:55 +00:00
self.assertEqual(post.get_title(), indexed['title'])
self.assertEqual(post.get_description(), indexed['description'])
2015-10-16 11:32:00 +00:00
self.assertEqual('First post First post first line This is the description category 1', indexed['text'])
2015-10-16 10:54:55 +00:00
self.assertEqual(post.get_absolute_url(), indexed['url'])
self.assertEqual(post.date_published.strftime("%Y-%m-%d %H:%M:%S"), indexed['pub_date'])
2015-10-15 20:29:32 +00:00
2015-10-19 21:26:05 +00:00
def test_searchqueryset(self):
posts = self.get_posts()
all_results = SearchQuerySet().all()
self.assertEqual(len(posts), len(all_results))