summaryrefslogtreecommitdiffstats
path: root/askbot/tests/haystack_search_tests.py
blob: 7a8bfcfd1c10252c1c35b8ab3d448057d14e0dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Tests haystack indexes and queries"""
from django.core import exceptions
from django.conf import settings
from django.contrib.auth.models import User
from askbot.tests.utils import AskbotTestCase, skipIf
from askbot import models
import datetime

class HaystackSearchTests(AskbotTestCase):
    """tests methods on User object,
    that were added for askbot
    """
    def setUp(self):
        self._old_value = getattr(settings, 'ENABLE_HAYSTACK_SEARCH', False)
        setattr(settings, "ENABLE_HAYSTACK_SEARCH", True)

        self.user = self.create_user(username='gepeto')
        self.other_user = self.create_user(username = 'pinocho')
        self.other_user.location = 'Managua'
        self.other_user.about = "I'm made of wood, gepeto made me"
        self.other_user.save()
        body_1 = '''Lorem turpis purus? Amet mattis eu et sociis phasellus
        montes elementum proin ut urna enim, velit, tincidunt quis ut,
        et integer mus? Nunc! Vut sed? Ac tincidunt egestas adipiscing,
        magna et pulvinar mid est urna ultricies, turpis tristique nisi,
        cum. Urna. Purus elit porttitor nisi porttitor ridiculus tincidunt
        amet duis, gepeto'''
        #from Baldy of Nome by Esther Birdsall Darling
        body_2 = ''' With unseeing eyes and dragging steps, the boy trudged along the snowy
        trail, dreading the arrival at Golconda Camp. For there was the House of
        Judgment, where all of the unfortunate events of that most unhappy day
        would be reviewed sternly, lorem'''
        self.question1 = self.post_question(
                                           user=self.user,
                                           body_text=body_1,
                                           title='Test title 1'
                                          )
        self.question2 = self.post_question(
                                           user=self.other_user,
                                           body_text=body_2,
                                           title='Test title 2, Baldy of Nome'
                                           )
        self.answer1 = self.post_answer(
                                        user=self.user,
                                        question = self.question1,
                                        body_text="This is a answer for question 1"
                                       )
        self.answer1 = self.post_answer(
                                        user=self.other_user,
                                        question = self.question2,
                                        body_text="Just a random text to fill the space"
                                       )

    def tearDown(self):
        setattr(settings, "ENABLE_HAYSTACK_SEARCH", self._old_value)

    @skipIf('haystack' not in settings.INSTALLED_APPS,
        'Haystack not setup')
    def test_title_search(self):
        #title search
        title_search_qs = models.Thread.objects.get_for_query('title')
        title_search_qs_2  = models.Thread.objects.get_for_query('Nome')
        self.assertEquals(title_search_qs.count(), 2)
        self.assertEquals(title_search_qs_2.count(), 1)

    @skipIf('haystack' not in settings.INSTALLED_APPS,
        'Haystack not setup')
    def test_body_search(self):

        #bodysearch
        body_search_qs = models.Thread.objects.get_for_query('Lorem')
        self.assertEquals(body_search_qs.count(), 2)
        body_search_qs_2 = models.Thread.objects.get_for_query('steps')
        self.assertEquals(body_search_qs_2.count(), 1)

    @skipIf('haystack' not in settings.INSTALLED_APPS,
        'Haystack not setup')
    def test_user_profile_search(self):
        #must return pinocho
        user_profile_qs = models.get_users_by_text_query('wood')
        self.assertEquals(user_profile_qs.count(), 1)

        #returns both gepeto and pinocho because gepeto nickname
        #and gepeto name in pinocho's profile
        user_profile_qs = models.get_users_by_text_query('gepeto')
        self.assertEquals(user_profile_qs.count(), 2)

    @skipIf('haystack' not in settings.INSTALLED_APPS,
        'Haystack not setup')
    def test_get_django_queryset(self):
        '''makes a query that can return multiple models and test
        get_django_queryset() method from AskbotSearchQuerySet'''
        #gepeto is present in profile and in question
        from askbot.search.haystack import AskbotSearchQuerySet
        qs = AskbotSearchQuerySet().filter(content='gepeto').get_django_queryset(User)
        for instance in qs:
           self.assertTrue(isinstance(instance, User))

        qs = AskbotSearchQuerySet().filter(content='gepeto').get_django_queryset(models.Thread)
        for instance in qs:
           self.assertTrue(isinstance(instance, models.Thread))