Beispiel #1
0
    def test_database_filter(self):
        class DatabaseFilterView(SearchView):
            filter_backends = (DatabaseFilterBackend,)

        view = DatabaseFilterView.as_view()
        request = factory.get('/en-US/search?topic=tagged')
        response = view(request)
        self.assertEqual(response.data['count'], 2)
        self.assertEqual(len(response.data['documents']), 2)
        self.assertEqual(response.data['documents'][0]['slug'], 'article-title')
        self.assertEqual(response.data['filters'], [
            {
                'name': 'Group',
                'options': [{
                    'name': 'Tagged',
                    'slug': 'tagged',
                    'count': 2,
                    'active': True,
                    'urls': {
                        'active': '/en-US/search?topic=tagged',
                        'inactive': '/en-US/search',
                    },
                }],
            },
        ])

        request = factory.get('/en-US/search?topic=non-existent')
        response = view(request)
        self.assertEqual(response.data['count'], 6)
        self.assertEqual(len(response.data['documents']), 6)
        self.assertEqual(response.data['documents'][0]['slug'], 'le-title')
Beispiel #2
0
    def test_paginate_by_param(self):
        request = factory.get('/en-US/search')
        LocaleURLMiddleware().process_request(request)
        view = SearchView.as_view(paginate_by=1)
        response = view(request)
        eq_(response.data['pages'], 5)

        request = factory.get('/en-US/search?per_page=4')
        LocaleURLMiddleware().process_request(request)
        response = view(request)
        eq_(response.data['pages'], 2)
Beispiel #3
0
    def test_queryset(self):

        class QuerysetSearchView(SearchView):
            def list(self, *args, **kwargs):
                response = super(QuerysetSearchView, self).list(*args, **kwargs)
                # queryset content
                eq_(self.object_list[0].title, 'an article title')
                eq_(self.object_list[0].locale, 'en-US')

                # metadata
                eq_(self.object_list.current_page, 1)
                eq_(len(self.object_list.serialized_filters), 1)
                eq_(self.object_list.topics, ['tagged'])
                eq_(self.object_list.url, self.request.get_full_path())

                # facets
                faceted_filters = self.object_list.faceted_filters()
                eq_(len(faceted_filters), 1)
                eq_(faceted_filters[0].name, 'Group')
                eq_(faceted_filters[0].options[0].name, 'Tagged')
                eq_(faceted_filters[0].options[0].count, 2)
                eq_(faceted_filters[0].options[0].active, True)
                return response

        view = QuerysetSearchView.as_view()
        request = factory.get('/en-US/search?q=article&topic=tagged')
        # setting request.locale correctly
        LocaleURLMiddleware().process_request(request)
        view(request)
Beispiel #4
0
    def test_current_topics(self):

        class TopicSearchView(SearchView):
            expected = None
            filter_backends = ()

            def dispatch(self, *args, **kwargs):
                super(TopicSearchView, self).dispatch(*args, **kwargs)
                eq_(self.current_topics, self.expected)

        view = TopicSearchView.as_view(expected=['spam'])
        view(factory.get('/?topic=spam'))

        # the topics are deduplicated
        view = TopicSearchView.as_view(expected=['spam', 'eggs'])
        view(factory.get('/?topic=spam&topic=eggs'))
        view(factory.get('/?topic=spam&topic=eggs&topic=spam'))
Beispiel #5
0
    def test_highlight_filter(self):

        class HighlightView(SearchView):
            filter_backends = (SearchQueryBackend, HighlightFilterBackend)

        view = HighlightView.as_view()
        request = factory.get('/en-US/search?q=article')
        response = view(request)
        ok_('<em>article</em>' in response.data['documents'][0]['excerpt'])
Beispiel #6
0
    def test_SearchQueryField(self):
        fake_request = factory.get('/?q=test')
        # APIRequestFactory doesn't actually return APIRequest objects
        # but standard HttpRequest objects due to the way it initializes
        # the request when APIViews are called
        fake_request.QUERY_PARAMS = fake_request.GET

        field = SearchQueryField()
        field.context = {'request': fake_request}
        eq_(field.to_native(None), 'test')
Beispiel #7
0
    def test_language_filter(self):
        class LanguageView(SearchView):
            filter_backends = (LanguageFilterBackend,)

        view = LanguageView.as_view()
        request = factory.get('/fr/search?q=article')
        # setting request.locale correctly
        LocaleURLMiddleware().process_request(request)
        response = view(request)
        self.assertEqual(response.data['count'], 1)
        self.assertEqual(len(response.data['documents']), 1)
        self.assertEqual(response.data['documents'][0]['locale'], 'fr')

        request = factory.get('/en-US/search?q=article')
        # setting request.locale correctly
        LocaleURLMiddleware().process_request(request)
        response = view(request)
        self.assertEqual(response.data['count'], 5)
        self.assertEqual(len(response.data['documents']), 5)
        self.assertEqual(response.data['documents'][0]['locale'], 'en-US')
Beispiel #8
0
    def test_search_query(self):
        class SearchQueryView(SearchView):
            filter_backends = (SearchQueryBackend,)

        view = SearchQueryView.as_view()
        request = factory.get('/en-US/search?q=article')
        response = view(request)
        self.assertEqual(response.data['count'], 4)
        self.assertEqual(len(response.data['documents']), 4)
        self.assertEqual(response.data['documents'][0]['slug'], 'article-title')
        self.assertEqual(response.data['documents'][0]['locale'], 'en-US')
Beispiel #9
0
    def test_serialized_filters(self):

        class Test1SearchView(SearchView):
            filter_backends = ()

            def dispatch(self, *args, **kwargs):
                super(Test1SearchView, self).dispatch(*args, **kwargs)
                eq_(self.serialized_filters,
                    [{'name': 'Tagged',
                      'slug': 'tagged',
                      'tags': ['tagged'],
                      'operator': 'OR',
                      'group': {'name': 'Group', 'order': 1}}])

        test_view1 = Test1SearchView.as_view()
        test_view1(factory.get('/'))

        group = FilterGroup.objects.create(name='Group')
        Filter.objects.create(name='Serializer', slug='serializer', group=group)

        class Test2SearchView(SearchView):
            filter_backends = ()

            def dispatch(self, *args, **kwargs):
                super(Test2SearchView, self).dispatch(*args, **kwargs)
                eq_(self.serialized_filters,
                    [{'name': 'Tagged',
                      'slug': 'tagged',
                      'tags': ['tagged'],
                      'operator': 'OR',
                      'group': {'name': 'Group', 'order': 1}},
                     {'name': 'Serializer',
                      'slug': 'serializer',
                      'tags': [],
                      'operator': 'OR',
                      'group': {'name': 'Group', 'order': 1}}])

        test_view2 = Test2SearchView.as_view()
        test_view2(factory.get('/'))