class LiveSimpleSearchQuerySetTestCase(TestCase):
    fixtures = ['bulk_data.json']
    
    def setUp(self):
        super(LiveSimpleSearchQuerySetTestCase, self).setUp()
        
        import haystack
        
        # Stow.
        self.old_debug = settings.DEBUG
        settings.DEBUG = True
        self.old_site = haystack.site
        self.site = SearchSite()
        self.backend = SearchBackend(site=self.site)
        self.index = SimpleMockSearchIndex(MockModel, backend=self.backend)
        self.site.register(MockModel, SimpleMockSearchIndex)
        haystack.site = self.site
        
        self.sample_objs = MockModel.objects.all()
        self.sqs = SearchQuerySet()

    def tearDown(self):
        # Restore.
        import haystack
        haystack.site = self.old_site
        settings.DEBUG = self.old_debug
        super(LiveSimpleSearchQuerySetTestCase, self).tearDown()
    
    def test_general_queries(self):
        # For now, just make sure these don't throw an exception.
        # They won't work until the simple backend is improved.
        self.assertTrue(len(self.sqs.auto_query('daniel')) > 0)
        self.assertTrue(len(self.sqs.filter(text='index')) > 0)
        self.assertTrue(len(self.sqs.exclude(name='daniel')) > 0)
        self.assertTrue(len(self.sqs.order_by('-pub_date')) > 0)
Ejemplo n.º 2
0
 def test_use_correct_site(self):
     test_site = SearchSite()
     test_site.register(XapianMockModel, XapianMockSearchIndex)
     self.backend.update(self.index, self.sample_objs)
     
     # Make sure that ``_process_results`` uses the right ``site``.
     self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 3)
     self.assertEqual([result.pk for result in self.backend.search(xapian.Query('indexed'))['results']], [1, 2, 3])
     
     self.site.unregister(XapianMockModel)
     self.assertEqual(len(self.site.get_indexed_models()), 0)
     self.backend.site = test_site
     self.assertTrue(len(self.backend.site.get_indexed_models()) > 0)
     
     # Should still be there, despite the main ``site`` not having that model
     # registered any longer.
     self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 3)
     self.assertEqual([result.pk for result in self.backend.search(xapian.Query('indexed'))['results']], [1, 2, 3])
     
     # Unregister it on the backend & make sure it takes effect.
     self.backend.site.unregister(XapianMockModel)
     self.assertEqual(len(self.backend.site.get_indexed_models()), 0)
     self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 0)
     
     # Nuke it & fallback on the main ``site``.
     self.backend.site = haystack.site
     self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 0)
     self.site.register(XapianMockModel, XapianMockSearchIndex)
     self.assertEqual(self.backend.search(xapian.Query('indexed'))['hits'], 3)
class LiveWhooshMultiSearchQuerySetTestCase(TestCase):
    fixtures = ['bulk_data.json']
    
    def setUp(self):
        super(LiveWhooshMultiSearchQuerySetTestCase, self).setUp()
        
        # Stow.
        temp_path = os.path.join('tmp', 'test_whoosh_query')
        self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
        settings.HAYSTACK_WHOOSH_PATH = temp_path
        
        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
        self.sammi = WhooshAnotherMockSearchIndex(AnotherMockModel, backend=self.sb)
        self.site.register(MockModel, WhooshMockSearchIndex)
        self.site.register(AnotherMockModel, WhooshAnotherMockSearchIndex)
        
        # Stow.
        import haystack
        self.old_debug = settings.DEBUG
        settings.DEBUG = True
        self.old_site = haystack.site
        haystack.site = self.site
        
        self.sb.setup()
        self.raw_whoosh = self.sb.index
        self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
        self.sb.delete_index()
        
        self.sq = SearchQuery(backend=self.sb)
        self.sqs = SearchQuerySet(site=self.site)
        
        self.smmi.update()
        self.sammi.update()
    
    def tearDown(self):
        if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
            shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)
        
        settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path
        
        import haystack
        haystack.site = self.old_site
        settings.DEBUG = self.old_debug
        
        super(LiveWhooshMultiSearchQuerySetTestCase, self).tearDown()
    
    def test_searchquerysets_with_models(self):
        sqs = self.sqs.all()
        self.assertEqual(sqs.query.build_query(), u'*')
        self.assertEqual(len(sqs), 25)
        
        sqs = self.sqs.models(MockModel)
        self.assertEqual(sqs.query.build_query(), u'django_ct:core.mockmodel')
        self.assertEqual(len(sqs), 23)
        
        sqs = self.sqs.models(AnotherMockModel)
        self.assertEqual(sqs.query.build_query(), u'django_ct:core.anothermockmodel')
        self.assertEqual(len(sqs), 2)
Ejemplo n.º 4
0
 def test_regression_site_kwarg(self):
     mock_index_site = SearchSite()
     mock_index_site.register(MockModel)
     mock_index_site.register(AnotherMockModel)
     
     bsqs = SearchQuerySet(site=mock_index_site)
     self.assertEqual(set(bsqs.query.backend.site.get_indexed_models()), set([MockModel, AnotherMockModel]))
Ejemplo n.º 5
0
    def setUp(self):
        super(LiveSolrMoreLikeThisTestCase, self).setUp()
        self.sqs = SearchQuerySet()

        # Wipe it clean.
        self.sqs.query.backend.clear()

        # With the models registered, you get the proper bits.
        import haystack
        from haystack.sites import SearchSite

        # Stow.
        self.old_site = haystack.site
        test_site = SearchSite()
        test_site.register(MockModel, SolrMockModelSearchIndex)
        test_site.register(AnotherMockModel, SolrAnotherMockModelSearchIndex)
        haystack.site = test_site

        # Force indexing of the content.
        for mock in MockModel.objects.all():
            mock.save()

        # Force indexing of the content.
        for mock in AnotherMockModel.objects.all():
            mock.save()

        self.sqs = SearchQuerySet()
Ejemplo n.º 6
0
 def test_regression_site_kwarg(self):
     # Stow.
     test_site = SearchSite()
     test_site.register(MockModel)
     
     msq = MockSearchQuery(site=test_site)
     self.assertEqual(msq.backend.site.get_indexed_models(), [MockModel])
Ejemplo n.º 7
0
 def setUp(self):
     super(LiveSolrSearchQuerySetTestCase, self).setUp()
     
     # With the models registered, you get the proper bits.
     import haystack
     from haystack.sites import SearchSite
     
     # Stow.
     self.old_debug = settings.DEBUG
     settings.DEBUG = True
     self.old_site = haystack.site
     test_site = SearchSite()
     test_site.register(MockModel, SolrMockModelSearchIndex)
     haystack.site = test_site
     
     self.sqs = SearchQuerySet()
     self.rsqs = RelatedSearchQuerySet()
     
     # Ugly but not constantly reindexing saves us almost 50% runtime.
     global lssqstc_all_loaded
     
     if lssqstc_all_loaded is None:
         print 'Reloading data...'
         lssqstc_all_loaded = True
         
         # Wipe it clean.
         clear_solr_index()
         
         # Force indexing of the content.
         mockmodel_index = test_site.get_index(MockModel)
         mockmodel_index.update()
Ejemplo n.º 8
0
 def setUp(self):
     super(LiveWhooshRoundTripTestCase, self).setUp()
     
     # Stow.
     temp_path = os.path.join('tmp', 'test_whoosh_query')
     self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
     settings.HAYSTACK_WHOOSH_PATH = temp_path
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.wrtsi = WhooshRoundTripSearchIndex(MockModel, backend=self.sb)
     self.site.register(MockModel, WhooshRoundTripSearchIndex)
     
     # Stow.
     import haystack
     self.old_debug = settings.DEBUG
     settings.DEBUG = True
     self.old_site = haystack.site
     haystack.site = self.site
     
     self.sb.setup()
     self.raw_whoosh = self.sb.index
     self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
     self.sb.delete_index()
     
     self.sqs = SearchQuerySet(site=self.site)
     
     # Wipe it clean.
     self.sqs.query.backend.clear()
     
     # Fake indexing.
     mock = MockModel()
     mock.id = 1
     self.sb.update(self.wrtsi, [mock])
Ejemplo n.º 9
0
 def test_log_query(self):
     backends.reset_search_queries()
     self.assertEqual(len(backends.queries), 0)
     
     # Stow.
     old_site = haystack.site
     old_debug = settings.DEBUG
     test_site = SearchSite()
     test_site.register(MockModel)
     haystack.site = test_site
     settings.DEBUG = False
     
     msq = MockSearchQuery(backend=MockSearchBackend())
     self.assertEqual(len(msq.get_results()), 100)
     self.assertEqual(len(backends.queries), 0)
     
     settings.DEBUG = True
     # Redefine it to clear out the cached results.
     msq2 = MockSearchQuery(backend=MockSearchBackend())
     self.assertEqual(len(msq2.get_results()), 100)
     self.assertEqual(len(backends.queries), 1)
     self.assertEqual(backends.queries[0]['query_string'], '')
     
     msq3 = MockSearchQuery(backend=MockSearchBackend())
     msq3.add_filter(SQ(foo='bar'))
     len(msq3.get_results())
     self.assertEqual(len(backends.queries), 2)
     self.assertEqual(backends.queries[0]['query_string'], '')
     self.assertEqual(backends.queries[1]['query_string'], '')
     
     # Restore.
     haystack.site = old_site
     settings.DEBUG = old_debug
Ejemplo n.º 10
0
    def setUp(self):
        super(XapianSearchBackendTestCase, self).setUp()

        self.site = SearchSite()
        self.backend = SearchBackend(site=self.site)
        self.index = XapianMockSearchIndex(XapianMockModel,
                                           backend=self.backend)
        self.site.register(XapianMockModel, XapianMockSearchIndex)

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = XapianMockModel()
            mock.id = i
            mock.author = 'david%s' % i
            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            mock.exp_date = datetime.date(2009, 2,
                                          23) + datetime.timedelta(days=i)
            mock.value = i * 5
            mock.flag = bool(i % 2)
            mock.slug = 'http://example.com/%d/' % i
            mock.url = 'http://example.com/%d/' % i
            self.sample_objs.append(mock)

        self.sample_objs[0].popularity = 834.0
        self.sample_objs[1].popularity = 35.5
        self.sample_objs[2].popularity = 972.0
Ejemplo n.º 11
0
 def test_use_correct_site(self):
     import haystack
     test_site = SearchSite()
     test_site.register(MockModel, WhooshMockSearchIndex)
     self.sb.update(self.smmi, self.sample_objs)
     
     # Make sure that ``_process_results`` uses the right ``site``.
     self.assertEqual(self.sb.search('*:*')['hits'], 23)
     self.assertEqual([result.pk for result in self.sb.search('*:*')['results']], [u'%s' % i for i in xrange(1, 24)])
     
     haystack.site.unregister(MockModel)
     self.assertEqual(len(haystack.site.get_indexed_models()), 0)
     self.sb.site = test_site
     self.assertTrue(len(self.sb.site.get_indexed_models()) > 0)
     
     # Should still be there, despite the main ``site`` not having that model
     # registered any longer.
     self.assertEqual(self.sb.search('*:*')['hits'], 23)
     self.assertEqual([result.pk for result in self.sb.search('*:*')['results']], [u'%s' % i for i in xrange(1, 24)])
     
     # Unregister it on the backend & make sure it takes effect.
     self.sb.site.unregister(MockModel)
     self.assertEqual(len(self.sb.site.get_indexed_models()), 0)
     self.assertEqual(self.sb.search('*:*')['hits'], 0)
     
     # Nuke it & fallback on the main ``site``.
     self.sb.site = haystack.site
     self.assertEqual(self.sb.search('*:*')['hits'], 0)
     haystack.site.register(MockModel, WhooshMockSearchIndex)
     self.assertEqual(self.sb.search('*:*')['hits'], 23)
Ejemplo n.º 12
0
    def setUp(self):
        super(LiveWhooshSearchQueryTestCase, self).setUp()

        # Stow.
        temp_path = os.path.join('tmp', 'test_whoosh_query')
        self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH',
                                       temp_path)
        settings.HAYSTACK_WHOOSH_PATH = temp_path

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, WhooshMockSearchIndex)

        self.sb.setup()
        self.raw_whoosh = self.sb.index
        self.parser = QueryParser(self.sb.content_field_name,
                                  schema=self.sb.schema)
        self.sb.delete_index()

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = 'daniel%s' % i
            mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
            self.sample_objs.append(mock)

        self.sq = SearchQuery(backend=self.sb)
Ejemplo n.º 13
0
    def setUp(self):
        super(LiveSolrSearchQuerySetTestCase, self).setUp()

        # With the models registered, you get the proper bits.
        import haystack
        from haystack.sites import SearchSite

        # Stow.
        self.old_debug = settings.DEBUG
        settings.DEBUG = True
        self.old_site = haystack.site
        test_site = SearchSite()
        test_site.register(MockModel, SolrMockModelSearchIndex)
        haystack.site = test_site

        self.sqs = SearchQuerySet()
        self.rsqs = RelatedSearchQuerySet()

        # Ugly but not constantly reindexing saves us almost 50% runtime.
        global lssqstc_all_loaded

        if lssqstc_all_loaded is None:
            print 'Reloading data...'
            lssqstc_all_loaded = True

            # Wipe it clean.
            clear_solr_index()

            # Force indexing of the content.
            mockmodel_index = test_site.get_index(MockModel)
            mockmodel_index.update()
Ejemplo n.º 14
0
 def test_log_query(self):
     backends.reset_search_queries()
     self.assertEqual(len(backends.queries), 0)
     
     # Stow.
     old_site = haystack.site
     old_debug = settings.DEBUG
     test_site = SearchSite()
     test_site.register(MockModel)
     haystack.site = test_site
     settings.DEBUG = False
     
     msq = MockSearchQuery(backend=MockSearchBackend())
     self.assertEqual(len(msq.get_results()), 100)
     self.assertEqual(len(backends.queries), 0)
     
     settings.DEBUG = True
     # Redefine it to clear out the cached results.
     msq2 = MockSearchQuery(backend=MockSearchBackend())
     self.assertEqual(len(msq2.get_results()), 100)
     self.assertEqual(len(backends.queries), 1)
     self.assertEqual(backends.queries[0]['query_string'], '')
     
     msq3 = MockSearchQuery(backend=MockSearchBackend())
     msq3.add_filter(SQ(foo='bar'))
     len(msq3.get_results())
     self.assertEqual(len(backends.queries), 2)
     self.assertEqual(backends.queries[0]['query_string'], '')
     self.assertEqual(backends.queries[1]['query_string'], '')
     
     # Restore.
     haystack.site = old_site
     settings.DEBUG = old_debug
Ejemplo n.º 15
0
 def test_regression_site_kwarg(self):
     # Stow.
     test_site = SearchSite()
     test_site.register(MockModel)
     
     msq = MockSearchQuery(site=test_site)
     self.assertEqual(msq.backend.site.get_indexed_models(), [MockModel])
Ejemplo n.º 16
0
    def setUp(self):
        super(WhooshSearchBackendTestCase, self).setUp()

        # Stow.
        temp_path = os.path.join('tmp', 'test_whoosh_query')
        self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH',
                                       temp_path)
        settings.HAYSTACK_WHOOSH_PATH = temp_path

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
        self.wmtmmi = WhooshMaintainTypeMockSearchIndex(MockModel,
                                                        backend=self.sb)
        self.site.register(MockModel, WhooshMockSearchIndex)

        # With the models registered, you get the proper bits.
        import haystack

        # Stow.
        self.old_site = haystack.site
        haystack.site = self.site

        self.sb.setup()
        self.raw_whoosh = self.sb.index
        self.parser = QueryParser(self.sb.content_field_name,
                                  schema=self.sb.schema)
        self.sb.delete_index()

        self.sample_objs = MockModel.objects.all()
Ejemplo n.º 17
0
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel,
                                                      backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = 'daniel%s' % i
            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
Ejemplo n.º 18
0
    def setUp(self):
        super(SolrBoostBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
        self.site.register(AFourthMockModel, SolrBoostMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site
        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i

            if i % 2:
                mock.author = 'daniel'
                mock.editor = 'david'
            else:
                mock.author = 'david'
                mock.editor = 'daniel'

            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
Ejemplo n.º 19
0
    def setUp(self):
        super(LiveWhooshAutocompleteTestCase, self).setUp()

        # Stow.
        temp_path = os.path.join('tmp', 'test_whoosh_query')
        self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH',
                                       temp_path)
        settings.HAYSTACK_WHOOSH_PATH = temp_path

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.wacsi = WhooshAutocompleteMockModelSearchIndex(MockModel,
                                                            backend=self.sb)
        self.site.register(MockModel, WhooshAutocompleteMockModelSearchIndex)

        # Stow.
        import haystack
        self.old_debug = settings.DEBUG
        settings.DEBUG = True
        self.old_site = haystack.site
        haystack.site = self.site

        self.sb.setup()
        self.sqs = SearchQuerySet(site=self.site)

        # Wipe it clean.
        self.sqs.query.backend.clear()

        for mock in MockModel.objects.all():
            self.wacsi.update_object(mock)
Ejemplo n.º 20
0
    def setUp(self):
        super(LiveWhooshRamStorageTestCase, self).setUp()

        # Stow.
        self.old_whoosh_storage = getattr(settings, 'HAYSTACK_WHOOSH_STORAGE',
                                          'file')
        settings.HAYSTACK_WHOOSH_STORAGE = 'ram'

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.wrtsi = WhooshRoundTripSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, WhooshRoundTripSearchIndex)

        # Stow.
        import haystack
        self.old_debug = settings.DEBUG
        settings.DEBUG = True
        self.old_site = haystack.site
        haystack.site = self.site

        self.sb.setup()
        self.raw_whoosh = self.sb.index
        self.parser = QueryParser(self.sb.content_field_name,
                                  schema=self.sb.schema)

        self.sqs = SearchQuerySet(site=self.site)

        # Wipe it clean.
        self.sqs.query.backend.clear()

        # Fake indexing.
        mock = MockModel()
        mock.id = 1
        self.sb.update(self.wrtsi, [mock])
Ejemplo n.º 21
0
 def setUp(self):
     super(SearchModelAdminTestCase, self).setUp()
     
     # With the models registered, you get the proper bits.
     import haystack
     from haystack.sites import SearchSite
     
     # Stow.
     self.old_debug = settings.DEBUG
     settings.DEBUG = True
     self.old_site = haystack.site
     test_site = SearchSite()
     test_site.register(MockModel, SolrMockModelSearchIndex)
     haystack.site = test_site
     
     # Wipe it clean.
     clear_solr_index()
     
     # Force indexing of the content.
     mockmodel_index = test_site.get_index(MockModel)
     mockmodel_index.update()
     
     superuser = User.objects.create_superuser(
         username='******',
         password='******',
         email='*****@*****.**',
     )
Ejemplo n.º 22
0
    def setUp(self):
        super(SearchModelAdminTestCase, self).setUp()

        # With the models registered, you get the proper bits.
        import haystack
        from haystack.sites import SearchSite

        # Stow.
        self.old_debug = settings.DEBUG
        settings.DEBUG = True
        self.old_site = haystack.site
        test_site = SearchSite()
        test_site.register(MockModel, SolrMockModelSearchIndex)
        haystack.site = test_site

        # Wipe it clean.
        clear_solr_index()

        # Force indexing of the content.
        mockmodel_index = test_site.get_index(MockModel)
        mockmodel_index.update()

        superuser = User.objects.create_superuser(
            username='******',
            password='******',
            email='*****@*****.**',
        )
Ejemplo n.º 23
0
    def setUp(self):
        super(XapianBoostBackendTestCase, self).setUp()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = XapianBoostMockSearchIndex(AFourthMockModel,
                                               backend=self.sb)
        self.site.register(AFourthMockModel, XapianBoostMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i
            if i % 2:
                mock.author = 'daniel'
                mock.editor = 'david'
            else:
                mock.author = 'david'
                mock.editor = 'daniel'
            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
class ManagementCommandTestCase(TestCase):
    fixtures = ['bulk_data.json']
    
    def setUp(self):
        super(ManagementCommandTestCase, self).setUp()
        self.solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        
        self.site = SearchSite()
        self.site.register(MockModel, SolrMockSearchIndex)
        
        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site
    
    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        super(ManagementCommandTestCase, self).tearDown()
    
    def test_basic_commands(self):
        call_command('clear_index', interactive=False, verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 0)
        
        call_command('update_index', verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 23)
        
        call_command('clear_index', interactive=False, verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 0)
        
        call_command('rebuild_index', interactive=False, verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 23)
    
    def test_remove(self):
        call_command('clear_index', interactive=False, verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 0)
        
        call_command('update_index', verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 23)
        
        # Remove a model instance.
        MockModel.objects.get(pk=1).delete()
        self.assertEqual(self.solr.search('*:*').hits, 23)
        
        # Plain ``update_index`` doesn't fix it.
        call_command('update_index', verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 23)
        
        # With the remove flag, it's gone.
        call_command('update_index', remove=True, verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 22)
    
    def test_multiprocessing(self):
        call_command('clear_index', interactive=False, verbosity=0)
        self.assertEqual(self.solr.search('*:*').hits, 0)
        
        # Watch the output, make sure there are multiple pids.
        call_command('update_index', verbosity=2, workers=2, batchsize=5)
        self.assertEqual(self.solr.search('*:*').hits, 23)
Ejemplo n.º 25
0
class SimpleSearchBackendTestCase(TestCase):
    fixtures = ['bulk_data.json']

    def setUp(self):
        super(SimpleSearchBackendTestCase, self).setUp()

        self.site = SearchSite()
        self.backend = SearchBackend(site=self.site)
        self.index = SimpleMockSearchIndex(MockModel, backend=self.backend)
        self.site.register(MockModel, SimpleMockSearchIndex)

        self.sample_objs = MockModel.objects.all()

    def test_update(self):
        self.backend.update(self.index, self.sample_objs)
    
    def test_remove(self):
        self.backend.remove(self.sample_objs[0])
    
    def test_clear(self):
        self.backend.clear()
    
    def test_search(self):
        # No query string should always yield zero results.
        self.assertEqual(self.backend.search(u''), {'hits': 0, 'results': []})
    
        self.assertEqual(self.backend.search(u'*')['hits'], 23)
        self.assertEqual([result.pk for result in self.backend.search(u'*')['results']], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])

        self.assertEqual(self.backend.search(u'daniel')['hits'], 23)
        self.assertEqual([result.pk for result in self.backend.search(u'daniel')['results']], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
    
        self.assertEqual(self.backend.search(u'should be a string')['hits'], 1)
        self.assertEqual([result.pk for result in self.backend.search(u'should be a string')['results']], [8])
    
        self.assertEqual(self.backend.search(u'index document')['hits'], 6)
        self.assertEqual([result.pk for result in self.backend.search(u'index document')['results']], [2, 3, 15, 16, 17, 18])
    
        # No support for spelling suggestions
        self.assertEqual(self.backend.search(u'Indx')['hits'], 0)
        self.assertFalse(self.backend.search(u'Indx').get('spelling_suggestion'))
    
        # No support for facets
        self.assertEqual(self.backend.search(u'', facets=['name']), {'hits': 0, 'results': []})
        self.assertEqual(self.backend.search(u'daniel', facets=['name'])['hits'], 23)
        self.assertEqual(self.backend.search(u'', date_facets={'pub_date': {'start_date': date(2008, 2, 26), 'end_date': date(2008, 2, 26), 'gap': '/MONTH'}}), {'hits': 0, 'results': []})
        self.assertEqual(self.backend.search(u'daniel', date_facets={'pub_date': {'start_date': date(2008, 2, 26), 'end_date': date(2008, 2, 26), 'gap': '/MONTH'}})['hits'], 23)
        self.assertEqual(self.backend.search(u'', query_facets={'name': '[* TO e]'}), {'hits': 0, 'results': []})
        self.assertEqual(self.backend.search(u'daniel', query_facets={'name': '[* TO e]'})['hits'], 23)
        self.assertFalse(self.backend.search(u'').get('facets'))
        self.assertFalse(self.backend.search(u'daniel').get('facets'))
        
    def test_more_like_this(self):
        self.backend.update(self.index, self.sample_objs)
        self.assertEqual(self.backend.search(u'*')['hits'], 23)
        
        # Unsupported by 'dummy'. Should see empty results.
        self.assertEqual(self.backend.more_like_this(self.sample_objs[0])['hits'], 0)
class LiveWhooshAutocompleteTestCase(TestCase):
    fixtures = ['bulk_data.json']

    def setUp(self):
        super(LiveWhooshAutocompleteTestCase, self).setUp()

        # Stow.
        temp_path = os.path.join('tmp', 'test_whoosh_query')
        self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
        settings.HAYSTACK_WHOOSH_PATH = temp_path

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.wacsi = WhooshAutocompleteMockModelSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, WhooshAutocompleteMockModelSearchIndex)

        # Stow.
        import haystack
        self.old_debug = settings.DEBUG
        settings.DEBUG = True
        self.old_site = haystack.site
        haystack.site = self.site

        self.sb.setup()
        self.sqs = SearchQuerySet(site=self.site)

        # Wipe it clean.
        self.sqs.query.backend.clear()

        for mock in MockModel.objects.all():
            self.wacsi.update_object(mock)

    def tearDown(self):
        if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
            shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)

        settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path

        import haystack
        haystack.site = self.old_site
        settings.DEBUG = self.old_debug

        super(LiveWhooshAutocompleteTestCase, self).tearDown()

    def test_autocomplete(self):
        autocomplete = self.sqs.autocomplete(text_auto='mod')
        self.assertEqual(autocomplete.count(), 5)
        self.assertEqual([result.pk for result in autocomplete], [u'1', u'12', u'6', u'7', u'14'])
        self.assertTrue('mod' in autocomplete[0].text.lower())
        self.assertTrue('mod' in autocomplete[1].text.lower())
        self.assertTrue('mod' in autocomplete[2].text.lower())
        self.assertTrue('mod' in autocomplete[3].text.lower())
        self.assertTrue('mod' in autocomplete[4].text.lower())
        self.assertEqual(len([result.pk for result in autocomplete]), 5)

    def test_edgengram_regression(self):
        autocomplete = self.sqs.autocomplete(text_auto='ngm')
        self.assertEqual(autocomplete.count(), 0)
Ejemplo n.º 27
0
    def setUp(self):
        super(SimpleSearchBackendTestCase, self).setUp()

        self.site = SearchSite()
        self.backend = SearchBackend(site=self.site)
        self.index = SimpleMockSearchIndex(MockModel, backend=self.backend)
        self.site.register(MockModel, SimpleMockSearchIndex)

        self.sample_objs = MockModel.objects.all()
Ejemplo n.º 28
0
class LiveWhooshAutocompleteTestCase(TestCase):
    fixtures = ['bulk_data.json']

    def setUp(self):
        super(LiveWhooshAutocompleteTestCase, self).setUp()

        # Stow.
        temp_path = os.path.join('tmp', 'test_whoosh_query')
        self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH',
                                       temp_path)
        settings.HAYSTACK_WHOOSH_PATH = temp_path

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.wacsi = WhooshAutocompleteMockModelSearchIndex(MockModel,
                                                            backend=self.sb)
        self.site.register(MockModel, WhooshAutocompleteMockModelSearchIndex)

        # Stow.
        import haystack
        self.old_debug = settings.DEBUG
        settings.DEBUG = True
        self.old_site = haystack.site
        haystack.site = self.site

        self.sb.setup()
        self.sqs = SearchQuerySet(site=self.site)

        # Wipe it clean.
        self.sqs.query.backend.clear()

        for mock in MockModel.objects.all():
            self.wacsi.update_object(mock)

    def tearDown(self):
        if os.path.exists(settings.HAYSTACK_WHOOSH_PATH):
            shutil.rmtree(settings.HAYSTACK_WHOOSH_PATH)

        settings.HAYSTACK_WHOOSH_PATH = self.old_whoosh_path

        import haystack
        haystack.site = self.old_site
        settings.DEBUG = self.old_debug

        super(LiveWhooshAutocompleteTestCase, self).tearDown()

    def test_autocomplete(self):
        autocomplete = self.sqs.autocomplete(text_auto='mod')
        self.assertEqual(autocomplete.count(), 5)
        self.assertEqual([result.pk for result in autocomplete],
                         [u'1', u'12', u'14', u'7', u'6'])
        self.assertTrue('mod' in autocomplete[0].text.lower())
        self.assertTrue('mod' in autocomplete[1].text.lower())
        self.assertTrue('mod' in autocomplete[2].text.lower())
        self.assertTrue('mod' in autocomplete[3].text.lower())
        self.assertTrue('mod' in autocomplete[4].text.lower())
        self.assertEqual(len([result.pk for result in autocomplete]), 5)
Ejemplo n.º 29
0
 def setUp(self):
     super(LiveXapianSearchQueryTestCase, self).setUp()
     
     site = SearchSite()
     backend = SearchBackend(site=site)
     index = LiveXapianMockSearchIndex(MockModel, backend=backend)
     site.register(MockModel, LiveXapianMockSearchIndex)
     backend.update(index, MockModel.objects.all())
     
     self.sq = SearchQuery(backend=backend)
Ejemplo n.º 30
0
    def setUp(self):
        super(LiveXapianSearchQueryTestCase, self).setUp()

        site = SearchSite()
        backend = SearchBackend(site=site)
        index = LiveXapianMockSearchIndex(MockModel, backend=backend)
        site.register(MockModel, LiveXapianMockSearchIndex)
        backend.update(index, MockModel.objects.all())

        self.sq = SearchQuery(backend=backend)
Ejemplo n.º 31
0
 def setUp(self):
     super(SearchQuerySetTestCase, self).setUp()
     self.bsqs = SearchQuerySet(query=DummySearchQuery(backend=DummySearchBackend()))
     self.msqs = SearchQuerySet(query=MockSearchQuery(backend=MockSearchBackend()))
     
     # Stow.
     self.old_site = haystack.site
     test_site = SearchSite()
     test_site.register(MockModel)
     haystack.site = test_site
Ejemplo n.º 32
0
class MoreLikeThisTagTestCase(TestCase):
    fixtures = ["bulk_data.json"]

    def setUp(self):
        super(MoreLikeThisTagTestCase, self).setUp()

        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        self.raw_solr.delete(q="*:*")

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = MLTSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, MLTSearchIndex)

        # Stow.
        import haystack

        self.old_site = haystack.site
        haystack.site = self.site

        # Force indexing of the content.
        for mock in MockModel.objects.all():
            mock.save()

    def tearDown(self):
        import haystack

        haystack.site = self.old_site
        super(MoreLikeThisTagTestCase, self).tearDown()

    def render(self, template, context):
        # Why on Earth does Django not have a TemplateTestCase yet?
        t = Template(template)
        c = Context(context)
        return t.render(c)

    def test_more_like_this_with_limit(self):
        mock = MockModel.objects.get(pk=3)
        template = """{% load more_like_this %}{% more_like_this entry as related_content limit 5 %}{% for rc in related_content %}{{ rc.id }} {% endfor %}"""
        context = {"entry": mock}
        self.assertEqual(
            set(self.render(template, context).split()),
            set(u"core.mockmodel.2 core.mockmodel.18 core.mockmodel.23 core.mockmodel.15 core.mockmodel.21 ".split()),
        )

    def test_more_like_this_without_limit(self):
        mock = MockModel.objects.get(pk=3)
        template = """{% load more_like_this %}{% more_like_this entry as related_content %}{% for rc in related_content %}{{ rc.id }} {% endfor %}"""
        context = {"entry": mock}
        self.assertEqual(
            set(self.render(template, context).split()),
            set(
                u"core.mockmodel.2 core.mockmodel.18 core.mockmodel.23 core.mockmodel.15 core.mockmodel.21 core.mockmodel.13 core.mockmodel.17 core.mockmodel.16 core.mockmodel.20 core.mockmodel.1 core.mockmodel.22 core.mockmodel.19 core.mockmodel.8 core.mockmodel.6 core.mockmodel.4 core.mockmodel.11 core.mockmodel.14 core.mockmodel.12 core.mockmodel.9 core.mockmodel.7 core.mockmodel.10 core.mockmodel.5".split()
            ),
        )
Ejemplo n.º 33
0
class SolrBoostBackendTestCase(TestCase):
    def setUp(self):
        super(SolrBoostBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
        self.site.register(AFourthMockModel, SolrBoostMockSearchIndex)

        # Stow.
        import haystack

        self.old_site = haystack.site
        haystack.site = self.site
        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i

            if i % 2:
                mock.author = "daniel"
                mock.editor = "david"
            else:
                mock.author = "david"
                mock.editor = "daniel"

            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack

        haystack.site = self.old_site
        super(SolrBoostBackendTestCase, self).tearDown()

    def test_boost(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 4)

        results = SearchQuerySet().filter(SQ(author="daniel") | SQ(editor="daniel"))

        self.assertEqual(
            [result.id for result in results],
            [
                "core.afourthmockmodel.1",
                "core.afourthmockmodel.3",
                "core.afourthmockmodel.2",
                "core.afourthmockmodel.4",
            ],
        )
Ejemplo n.º 34
0
class MoreLikeThisTagTestCase(TestCase):
    fixtures = ['bulk_data.json']

    def setUp(self):
        super(MoreLikeThisTagTestCase, self).setUp()

        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        self.raw_solr.delete(q='*:*')

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = MLTSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, MLTSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        # Force indexing of the content.
        for mock in MockModel.objects.all():
            mock.save()

    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        super(MoreLikeThisTagTestCase, self).tearDown()

    def render(self, template, context):
        # Why on Earth does Django not have a TemplateTestCase yet?
        t = Template(template)
        c = Context(context)
        return t.render(c)

    def test_more_like_this_with_limit(self):
        mock = MockModel.objects.get(pk=3)
        template = """{% load more_like_this %}{% more_like_this entry as related_content limit 5 %}{% for rc in related_content %}{{ rc.id }} {% endfor %}"""
        context = {
            'entry': mock,
        }
        self.assertEqual(
            set(self.render(template, context).split()),
            set(u'core.mockmodel.2 core.mockmodel.18 core.mockmodel.23 core.mockmodel.15 core.mockmodel.21 '
                .split()))

    def test_more_like_this_without_limit(self):
        mock = MockModel.objects.get(pk=3)
        template = """{% load more_like_this %}{% more_like_this entry as related_content %}{% for rc in related_content %}{{ rc.id }} {% endfor %}"""
        context = {
            'entry': mock,
        }
        self.assertEqual(
            set(self.render(template, context).split()),
            set(u'core.mockmodel.2 core.mockmodel.18 core.mockmodel.23 core.mockmodel.15 core.mockmodel.21 core.mockmodel.13 core.mockmodel.17 core.mockmodel.16 core.mockmodel.20 core.mockmodel.1 core.mockmodel.22 core.mockmodel.19 core.mockmodel.8 core.mockmodel.6 core.mockmodel.4 core.mockmodel.11 core.mockmodel.14 core.mockmodel.12 core.mockmodel.9 core.mockmodel.7 core.mockmodel.10 core.mockmodel.5'
                .split()))
Ejemplo n.º 35
0
 def setUp(self):
     super(FacetedSearchFormTestCase, self).setUp()
     mock_index_site = SearchSite()
     mock_index_site.register(MockModel)
     mock_index_site.register(AnotherMockModel)
     
     # Stow.
     self.old_site = haystack.site
     haystack.site = mock_index_site
     
     self.sqs = SearchQuerySet(query=DummySearchQuery(backend=DummySearchBackend()), site=mock_index_site)
Ejemplo n.º 36
0
 def setUp(self):
     super(LiveXapianAutocompleteTestCase, self).setUp()
     
     site = SearchSite()
     backend = SearchBackend(site=site)
     index = XapianAutocompleteMockModelSearchIndex(MockModel, backend=backend)
     site.register(MockModel, XapianAutocompleteMockModelSearchIndex)
     backend.update(index, MockModel.objects.all())
     
     self.sq = SearchQuery(backend=backend)
     self.sqs = SearchQuerySet(query=self.sq)
Ejemplo n.º 37
0
 def setUp(self):
     super(SearchViewTestCase, self).setUp()
     mock_index_site = SearchSite()
     mock_index_site.register(MockModel)
     mock_index_site.register(AnotherMockModel)
     
     # Stow.
     self.old_site = haystack.site
     haystack.site = mock_index_site
     
     self.old_engine = getattr(settings, 'HAYSTACK_SEARCH_ENGINE')
     settings.HAYSTACK_SEARCH_ENGINE = 'dummy'
Ejemplo n.º 38
0
 def test_run(self):
     # Stow.
     old_site = haystack.site
     test_site = SearchSite()
     test_site.register(MockModel)
     haystack.site = test_site
     
     msq = MockSearchQuery(backend=MockSearchBackend())
     self.assertEqual(len(msq.get_results()), 100)
     self.assertEqual(msq.get_results()[0], MOCK_SEARCH_RESULTS[0])
     
     # Restore.
     haystack.site = old_site
Ejemplo n.º 39
0
 def test_run(self):
     # Stow.
     old_site = haystack.site
     test_site = SearchSite()
     test_site.register(MockModel)
     haystack.site = test_site
     
     msq = MockSearchQuery(backend=MockSearchBackend())
     self.assertEqual(len(msq.get_results()), 100)
     self.assertEqual(msq.get_results()[0], MOCK_SEARCH_RESULTS[0])
     
     # Restore.
     haystack.site = old_site
Ejemplo n.º 40
0
class SolrBoostBackendTestCase(TestCase):
    def setUp(self):
        super(SolrBoostBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
        self.site.register(AFourthMockModel, SolrBoostMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site
        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i

            if i % 2:
                mock.author = 'daniel'
                mock.editor = 'david'
            else:
                mock.author = 'david'
                mock.editor = 'daniel'

            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        super(SolrBoostBackendTestCase, self).tearDown()

    def test_boost(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 4)

        results = SearchQuerySet().filter(
            SQ(author='daniel') | SQ(editor='daniel'))

        self.assertEqual([result.id for result in results], [
            'core.afourthmockmodel.1', 'core.afourthmockmodel.3',
            'core.afourthmockmodel.2', 'core.afourthmockmodel.4'
        ])
Ejemplo n.º 41
0
 def setUp(self):
     super(SearchQuerySetTestCase, self).setUp()
     self.bsqs = SearchQuerySet(query=DummySearchQuery(backend=DummySearchBackend()))
     self.msqs = SearchQuerySet(query=MockSearchQuery(backend=MockSearchBackend()))
     self.mmsqs = SearchQuerySet(query=MockSearchQuery(backend=MixedMockSearchBackend()))
     
     # Stow.
     self.old_debug = settings.DEBUG
     settings.DEBUG = True
     self.old_site = haystack.site
     test_site = SearchSite()
     test_site.register(MockModel)
     haystack.site = test_site
     
     backends.reset_search_queries()
Ejemplo n.º 42
0
class XapianBoostBackendTestCase(TestCase):
    def setUp(self):
        super(XapianBoostBackendTestCase, self).setUp()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = XapianBoostMockSearchIndex(AFourthMockModel,
                                               backend=self.sb)
        self.site.register(AFourthMockModel, XapianBoostMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i
            if i % 2:
                mock.author = 'daniel'
                mock.editor = 'david'
            else:
                mock.author = 'david'
                mock.editor = 'daniel'
            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        super(XapianBoostBackendTestCase, self).tearDown()

    def test_boost(self):
        self.sb.update(self.smmi, self.sample_objs)

        sqs = SearchQuerySet()

        self.assertEqual(len(sqs.all()), 4)

        results = sqs.filter(SQ(author='daniel') | SQ(editor='daniel'))

        self.assertEqual([result.id for result in results], [
            'core.afourthmockmodel.1', 'core.afourthmockmodel.3',
            'core.afourthmockmodel.2', 'core.afourthmockmodel.4'
        ])
Ejemplo n.º 43
0
class XapianBoostBackendTestCase(TestCase):
    def setUp(self):
        super(XapianBoostBackendTestCase, self).setUp()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = XapianBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
        self.site.register(AFourthMockModel, XapianBoostMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i
            if i % 2:
                mock.author = 'daniel'
                mock.editor = 'david'
            else:
                mock.author = 'david'
                mock.editor = 'daniel'
            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        super(XapianBoostBackendTestCase, self).tearDown()

    def test_boost(self):
        self.sb.update(self.smmi, self.sample_objs)
        
        sqs = SearchQuerySet()
        
        self.assertEqual(len(sqs.all()), 4)

        results = sqs.filter(SQ(author='daniel') | SQ(editor='daniel'))

        self.assertEqual([result.id for result in results], [
            'core.afourthmockmodel.1',
            'core.afourthmockmodel.3',
            'core.afourthmockmodel.2',
            'core.afourthmockmodel.4'
        ])
 def setUp(self):
     super(LiveWhooshRamStorageTestCase, self).setUp()
     
     # Stow.
     self.old_whoosh_storage = getattr(settings, 'HAYSTACK_WHOOSH_STORAGE', 'file')
     settings.HAYSTACK_WHOOSH_STORAGE = 'ram'
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.wrtsi = WhooshRoundTripSearchIndex(MockModel, backend=self.sb)
     self.site.register(MockModel, WhooshRoundTripSearchIndex)
     
     # Stow.
     import haystack
     self.old_debug = settings.DEBUG
     settings.DEBUG = True
     self.old_site = haystack.site
     haystack.site = self.site
     
     self.sb.setup()
     self.raw_whoosh = self.sb.index
     self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
     
     self.sqs = SearchQuerySet(site=self.site)
     
     # Wipe it clean.
     self.sqs.query.backend.clear()
     
     # Fake indexing.
     mock = MockModel()
     mock.id = 1
     self.sb.update(self.wrtsi, [mock])
Ejemplo n.º 45
0
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Stow.
        self.old_solr_url = getattr(settings, "HAYSTACK_SOLR_URL", "http://localhost:9001/solr/test_default")
        settings.HAYSTACK_SOLR_URL = "http://localhost:9001/solr/test_default"

        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        self.raw_solr.delete(q="*:*")

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)

        # Stow.
        import haystack

        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = "daniel%s" % i
            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
 def setUp(self):
     super(LiveWhooshAutocompleteTestCase, self).setUp()
     
     # Stow.
     temp_path = os.path.join('tmp', 'test_whoosh_query')
     self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
     settings.HAYSTACK_WHOOSH_PATH = temp_path
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.wacsi = WhooshAutocompleteMockModelSearchIndex(MockModel, backend=self.sb)
     self.site.register(MockModel, WhooshAutocompleteMockModelSearchIndex)
     
     # Stow.
     import haystack
     self.old_debug = settings.DEBUG
     settings.DEBUG = True
     self.old_site = haystack.site
     haystack.site = self.site
     
     self.sb.setup()
     self.sqs = SearchQuerySet(site=self.site)
     
     # Wipe it clean.
     self.sqs.query.backend.clear()
     
     for mock in MockModel.objects.all():
         self.wacsi.update_object(mock)
 def setUp(self):
     super(LiveWhooshRoundTripTestCase, self).setUp()
     
     # Stow.
     temp_path = os.path.join('tmp', 'test_whoosh_query')
     self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
     settings.HAYSTACK_WHOOSH_PATH = temp_path
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.wrtsi = WhooshRoundTripSearchIndex(MockModel, backend=self.sb)
     self.site.register(MockModel, WhooshRoundTripSearchIndex)
     
     # Stow.
     import haystack
     self.old_debug = settings.DEBUG
     settings.DEBUG = True
     self.old_site = haystack.site
     haystack.site = self.site
     
     self.sb.setup()
     self.raw_whoosh = self.sb.index
     self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
     self.sb.delete_index()
     
     self.sqs = SearchQuerySet(site=self.site)
     
     # Wipe it clean.
     self.sqs.query.backend.clear()
     
     # Fake indexing.
     mock = MockModel()
     mock.id = 1
     self.sb.update(self.wrtsi, [mock])
 def setUp(self):
     super(LiveWhooshSearchQueryTestCase, self).setUp()
     
     # Stow.
     temp_path = os.path.join('tmp', 'test_whoosh_query')
     self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
     settings.HAYSTACK_WHOOSH_PATH = temp_path
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
     self.site.register(MockModel, WhooshMockSearchIndex)
     
     self.sb.setup()
     self.raw_whoosh = self.sb.index
     self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
     self.sb.delete_index()
     
     self.sample_objs = []
     
     for i in xrange(1, 4):
         mock = MockModel()
         mock.id = i
         mock.author = 'daniel%s' % i
         mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
         self.sample_objs.append(mock)
     
     self.sq = SearchQuery(backend=self.sb)
 def setUp(self):
     super(WhooshSearchBackendTestCase, self).setUp()
     
     # Stow.
     temp_path = os.path.join('tmp', 'test_whoosh_query')
     self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
     settings.HAYSTACK_WHOOSH_PATH = temp_path
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.smmi = WhooshMockSearchIndex(MockModel, backend=self.sb)
     self.wmtmmi = WhooshMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
     self.site.register(MockModel, WhooshMockSearchIndex)
     
     # With the models registered, you get the proper bits.
     import haystack
     
     # Stow.
     self.old_site = haystack.site
     haystack.site = self.site
     
     self.sb.setup()
     self.raw_whoosh = self.sb.index
     self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
     self.sb.delete_index()
     
     self.sample_objs = MockModel.objects.all()
Ejemplo n.º 50
0
    def setUp(self):
        super(LiveSolrSearchQueryTestCase, self).setUp()

        # Wipe it clean.
        clear_solr_index()

        site = SearchSite()
        site.register(MockModel, SolrMockSearchIndex)
        sb = SearchBackend(site=site)
        smmi = SolrMockSearchIndex(MockModel, backend=sb)

        self.sq = SearchQuery(backend=sb)

        # Force indexing of the content.
        mockmodel_index = site.get_index(MockModel)
        mockmodel_index.update()
Ejemplo n.º 51
0
    def test_stored_fields(self):
        # Stow.
        import haystack
        from haystack.sites import SearchSite
        old_site = haystack.site
        test_site = SearchSite()
        haystack.site = test_site

        # Without registering, we should receive an empty dict.
        self.assertEqual(self.no_data_sr.get_stored_fields(), {})
        self.assertEqual(self.extra_data_sr.get_stored_fields(), {})
        self.assertEqual(self.no_overwrite_data_sr.get_stored_fields(), {})

        from haystack.indexes import SearchIndex, CharField

        class TestSearchIndex(SearchIndex):
            stored = CharField(model_attr='author', document=True)

        # Register the index & try again.
        haystack.site.register(MockModel, TestSearchIndex)

        self.assertEqual(self.no_data_sr.get_stored_fields(), {'stored': None})
        self.assertEqual(self.extra_data_sr.get_stored_fields(),
                         {'stored': 'I am stored data. How fun.'})
        self.assertEqual(self.no_overwrite_data_sr.get_stored_fields(),
                         {'stored': 'I am stored data. How fun.'})

        # Restore.
        haystack.site = old_site
Ejemplo n.º 52
0
    def setUp(self):
        super(XapianBoostBackendTestCase, self).setUp()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = XapianBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
        self.site.register(AFourthMockModel, XapianBoostMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i
            if i % 2:
                mock.author = 'daniel'
                mock.editor = 'david'
            else:
                mock.author = 'david'
                mock.editor = 'daniel'
            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
Ejemplo n.º 53
0
    def test_all_cases(self):
        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = 'daniel%s' % i
            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

        # Stow.
        # Point the backend at a URL that doesn't exist so we can watch the
        # sparks fly.
        old_solr_url = settings.HAYSTACK_SOLR_URL
        settings.HAYSTACK_SOLR_URL = "%s/foo/" % settings.HAYSTACK_SOLR_URL
        cap = CaptureHandler()
        logging.getLogger('haystack').addHandler(cap)
        import haystack
        logging.getLogger('haystack').removeHandler(haystack.stream)

        # Setup the rest of the bits.
        site = SearchSite()
        site.register(MockModel, SolrMockSearchIndex)
        sb = SearchBackend(site=site)
        smmi = SolrMockSearchIndex(MockModel, backend=sb)

        # Prior to the addition of the try/except bits, these would all fail miserably.
        self.assertEqual(len(CaptureHandler.logs_seen), 0)
        sb.update(smmi, self.sample_objs)
        self.assertEqual(len(CaptureHandler.logs_seen), 1)
        sb.remove(self.sample_objs[0])
        self.assertEqual(len(CaptureHandler.logs_seen), 2)
        sb.search('search')
        self.assertEqual(len(CaptureHandler.logs_seen), 3)
        sb.more_like_this(self.sample_objs[0])
        self.assertEqual(len(CaptureHandler.logs_seen), 4)
        sb.clear([MockModel])
        self.assertEqual(len(CaptureHandler.logs_seen), 5)
        sb.clear()
        self.assertEqual(len(CaptureHandler.logs_seen), 6)

        # Restore.
        settings.HAYSTACK_SOLR_URL = old_solr_url
        logging.getLogger('haystack').removeHandler(cap)
        logging.getLogger('haystack').addHandler(haystack.stream)
Ejemplo n.º 54
0
    def setUp(self):
        super(MoreLikeThisTagTestCase, self).setUp()

        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        self.raw_solr.delete(q='*:*')

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = MLTSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, MLTSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        # Force indexing of the content.
        for mock in MockModel.objects.all():
            mock.save()
Ejemplo n.º 55
0
    def setUp(self):
        super(LiveSolrMoreLikeThisTestCase, self).setUp()

        # Wipe it clean.
        clear_solr_index()

        # With the models registered, you get the proper bits.
        import haystack
        from haystack.sites import SearchSite

        # Stow.
        self.old_site = haystack.site
        test_site = SearchSite()
        test_site.register(MockModel, SolrMockModelSearchIndex)
        test_site.register(AnotherMockModel, SolrAnotherMockModelSearchIndex)
        haystack.site = test_site

        self.sqs = SearchQuerySet()

        test_site.get_index(MockModel).update()
        test_site.get_index(AnotherMockModel).update()
Ejemplo n.º 56
0
 def test_regression_site_kwarg(self):
     mock_index_site = SearchSite()
     mock_index_site.register(MockModel)
     mock_index_site.register(AnotherMockModel)
     
     bsqs = SearchQuerySet(site=mock_index_site)
     self.assertEqual(set(bsqs.query.backend.site.get_indexed_models()), set([MockModel, AnotherMockModel]))
Ejemplo n.º 57
0
 def test_model_choices(self):
     mis = SearchSite()
     mis.register(MockModel)
     mis.register(AnotherMockModel)
     self.assertEqual(len(model_choices(site=mis)), 2)
     self.assertEqual([option[1] for option in model_choices(site=mis)],
                      [u'Another mock models', u'Mock models'])
Ejemplo n.º 58
0
    def test_use_correct_site(self):
        test_site = SearchSite()
        test_site.register(XapianMockModel, XapianMockSearchIndex)
        self.backend.update(self.index, self.sample_objs)

        # Make sure that ``_process_results`` uses the right ``site``.
        self.assertEqual(
            self.backend.search(xapian.Query('indexed'))['hits'], 3)
        self.assertEqual([
            result.pk for result in self.backend.search(xapian.Query(
                'indexed'))['results']
        ], [1, 2, 3])

        self.site.unregister(XapianMockModel)
        self.assertEqual(len(self.site.get_indexed_models()), 0)
        self.backend.site = test_site
        self.assertTrue(len(self.backend.site.get_indexed_models()) > 0)

        # Should still be there, despite the main ``site`` not having that model
        # registered any longer.
        self.assertEqual(
            self.backend.search(xapian.Query('indexed'))['hits'], 3)
        self.assertEqual([
            result.pk for result in self.backend.search(xapian.Query(
                'indexed'))['results']
        ], [1, 2, 3])

        # Unregister it on the backend & make sure it takes effect.
        self.backend.site.unregister(XapianMockModel)
        self.assertEqual(len(self.backend.site.get_indexed_models()), 0)
        self.assertEqual(
            self.backend.search(xapian.Query('indexed'))['hits'], 0)

        # Nuke it & fallback on the main ``site``.
        self.backend.site = haystack.site
        self.assertEqual(
            self.backend.search(xapian.Query('indexed'))['hits'], 0)
        self.site.register(XapianMockModel, XapianMockSearchIndex)
        self.assertEqual(
            self.backend.search(xapian.Query('indexed'))['hits'], 3)
Ejemplo n.º 59
0
 def setUp(self):
     super(WhooshBoostBackendTestCase, self).setUp()
     
     # Stow.
     temp_path = os.path.join('tmp', 'test_whoosh_query')
     self.old_whoosh_path = getattr(settings, 'HAYSTACK_WHOOSH_PATH', temp_path)
     settings.HAYSTACK_WHOOSH_PATH = temp_path
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.smmi = WhooshBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
     self.site.register(AFourthMockModel, WhooshBoostMockSearchIndex)
     
     # With the models registered, you get the proper bits.
     import haystack
     
     # Stow.
     self.old_site = haystack.site
     haystack.site = self.site
     
     self.sb.setup()
     self.raw_whoosh = self.sb.index
     self.parser = QueryParser(self.sb.content_field_name, schema=self.sb.schema)
     self.sb.delete_index()
     self.sample_objs = []
     
     for i in xrange(1, 5):
         mock = AFourthMockModel()
         mock.id = i
         
         if i % 2:
             mock.author = 'daniel'
             mock.editor = 'david'
         else:
             mock.author = 'david'
             mock.editor = 'daniel'
         
         mock.pub_date = date(2009, 2, 25) - timedelta(days=i)
         self.sample_objs.append(mock)