Skip to content
This repository has been archived by the owner on Oct 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #258 from czpython/feature/search-tests
Browse files Browse the repository at this point in the history
adds search tests
  • Loading branch information
czpython committed Jul 13, 2015
2 parents 716058c + c8d5ac5 commit 2b43012
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions aldryn_newsblog/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from django.conf import settings

from haystack.constants import DEFAULT_ALIAS

from aldryn_search.utils import get_index_base

from .models import Article
Expand All @@ -17,6 +19,11 @@ class ArticleIndex(get_index_base()):
def get_title(self, obj):
return obj.title

def get_url(self, obj):
using = getattr(self, '_backend_alias', DEFAULT_ALIAS)
language = self.get_current_language(using=using, obj=obj)
return obj.get_absolute_url(language)

def get_description(self, obj):
return obj.lead_in

Expand Down
2 changes: 2 additions & 0 deletions aldryn_newsblog/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def create_article(self, content=None, **kwargs):
fields.update(kwargs)

article = Article.objects.create(**fields)
# save again to calculate article search_data.
article.save()

if content:
api.add_plugin(article.content, 'TextPlugin',
Expand Down
53 changes: 53 additions & 0 deletions aldryn_newsblog/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
from django.utils.timezone import now
from django.utils.translation import activate

from aldryn_newsblog.models import Article
from aldryn_newsblog.search_indexes import ArticleIndex

from . import NewsBlogTestCase


class ArticleIndexingTests(NewsBlogTestCase):

def get_index(self):
from haystack.constants import DEFAULT_ALIAS

index = ArticleIndex()
index._backend_alias = DEFAULT_ALIAS
return index

def test_article_is_indexed_using_prepare(self):
activate(self.language)

lead_in = 'Hello! this text will be searchable.'

article = self.create_article(lead_in=lead_in)

index = self.get_index()

data = index.prepare(article)

count = data['text'].count(lead_in)

self.assertTrue(count != 0, "Couldn't find %s in text" % lead_in)

def test_translated_article_is_indexed_using_prepare(self):
activate(self.language)

lead_in = 'Hello! this text will be searchable.'

# create english article
article = self.create_article(lead_in=lead_in)

# create german translation for article
article.set_current_language('de')
article.title = '%s [de]' % self.rand_str()
article.save()

index = self.get_index()

data = index.prepare(article)

self.assertEquals(data['language'], 'de')
self.assertEquals(data['url'], article.get_absolute_url('de'))
1 change: 1 addition & 0 deletions test_requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ coverage>=3.7.1
dj-database-url
django-classy-tags
django-mptt<0.7.0 # rq.filter: <0.7.0
django-haystack
django-sekizai
django-treebeard>=2.0
djangocms-admin-style
Expand Down
1 change: 1 addition & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
# that were previously used, which reveals this issue.
#
'PARLER_ENABLE_CACHING': False,
'ALDRYN_SEARCH_DEFAULT_LANGUAGE': 'en',
'HAYSTACK_CONNECTIONS': {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
Expand Down

0 comments on commit 2b43012

Please sign in to comment.