File: test_forms.py

package info (click to toggle)
django-haystack 2.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,220 kB
  • sloc: python: 15,512; xml: 1,708; makefile: 71; sh: 45
file content (37 lines) | stat: -rw-r--r-- 1,399 bytes parent folder | download | duplicates (2)
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
# encoding: utf-8
"""Tests for Whoosh spelling suggestions"""
from __future__ import absolute_import, division, print_function, unicode_literals

from django.conf import settings
from django.http import HttpRequest

from haystack.forms import SearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView

from .test_whoosh_backend import LiveWhooshRoundTripTestCase


class SpellingSuggestionTestCase(LiveWhooshRoundTripTestCase):
    fixtures = ['base_data']

    def setUp(self):
        self.old_spelling_setting = settings.HAYSTACK_CONNECTIONS['whoosh'].get('INCLUDE_SPELLING', False)
        settings.HAYSTACK_CONNECTIONS['whoosh']['INCLUDE_SPELLING'] = True

        super(SpellingSuggestionTestCase, self).setUp()

    def tearDown(self):
        settings.HAYSTACK_CONNECTIONS['whoosh']['INCLUDE_SPELLING'] = self.old_spelling_setting
        super(SpellingSuggestionTestCase, self).tearDown()

    def test_form_suggestion(self):
        form = SearchForm({'q': 'exampl'}, searchqueryset=SearchQuerySet('whoosh'))
        self.assertEqual(form.get_suggestion(), 'example')

    def test_view_suggestion(self):
        view = SearchView(template='test_suggestion.html', searchqueryset=SearchQuerySet('whoosh'))
        mock = HttpRequest()
        mock.GET['q'] = 'exampl'
        resp = view(mock)
        self.assertEqual(resp.content, b'Suggestion: example')