示例#1
0
    def test_remove_object(self):
        self.mi.update()
        self.assertEqual(self.sb.search('*')['hits'], 3)

        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual(self.sb.search('*')['hits'], 4)

        self.mi.remove_object(mock)
        self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [('core.mockmodel', '1'), ('core.mockmodel', '2'), ('core.mockmodel', '3')])

        # Put it back so we can test passing kwargs.
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual(self.sb.search('*')['hits'], 4)

        self.mi.remove_object(mock, commit=False)
        self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [('core.mockmodel', '1'), ('core.mockmodel', '2'), ('core.mockmodel', '3'), ('core.mockmodel', '20')])

        self.sb.clear()
    def test_remove_object(self):
        self.mi.update()
        self.assertEqual(self.sb.search('*')['hits'], 3)

        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual(self.sb.search('*')['hits'], 4)

        self.mi.remove_object(mock)
        self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [(u'core.mockmodel', u'1'), (u'core.mockmodel', u'2'), (u'core.mockmodel', u'3')])

        # Put it back so we can test passing kwargs.
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual(self.sb.search('*')['hits'], 4)

        self.mi.remove_object(mock, commit=False)
        self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [(u'core.mockmodel', u'1'), (u'core.mockmodel', u'2'), (u'core.mockmodel', u'3'), (u'core.mockmodel', u'20')])

        self.sb.clear()
    def test_remove_object(self):
        self.mi.update()
        self.assertEqual(self.sb.search("*")["hits"], 3)

        mock = MockModel()
        mock.pk = 20
        mock.author = "daniel%s" % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual(self.sb.search("*")["hits"], 4)

        self.mi.remove_object(mock)
        self.assertEqual(
            [(res.content_type(), res.pk) for res in self.sb.search("*")["results"]],
            [("core.mockmodel", "1"), ("core.mockmodel", "2"), ("core.mockmodel", "3")],
        )

        # Put it back so we can test passing kwargs.
        mock = MockModel()
        mock.pk = 20
        mock.author = "daniel%s" % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual(self.sb.search("*")["hits"], 4)

        self.mi.remove_object(mock, commit=False)
        self.assertEqual(
            [(res.content_type(), res.pk) for res in self.sb.search("*")["results"]],
            [("core.mockmodel", "1"), ("core.mockmodel", "2"), ("core.mockmodel", "3"), ("core.mockmodel", "20")],
        )

        self.sb.clear()
示例#4
0
 def setUp(self):
     super(XapianSearchBackendTestCase, self).setUp()
     
     temp_path = os.path.join('tmp', 'test_xapian_query')
     self.old_xapian_path = getattr(settings, 'HAYSTACK_XAPIAN_PATH', temp_path)
     settings.HAYSTACK_XAPIAN_PATH = temp_path
     
     self.site = XapianSearchSite()
     self.sb = SearchBackend(site=self.site)
     self.msi = XapianMockSearchIndex(MockModel, backend=self.sb)
     self.site.register(MockModel, XapianMockSearchIndex)
     
     self.sample_objs = []
     
     for i in xrange(1, 4):
         mock = MockModel()
         mock.id = i
         mock.author = 'david%s' % i
         mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
         mock.value = i * 5
         mock.flag = bool(i % 2)
         mock.slug = 'http://example.com/%d' % i
         self.sample_objs.append(mock)
         
     self.sample_objs[0].popularity = 834.0
     self.sample_objs[1].popularity = 35.0
     self.sample_objs[2].popularity = 972.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)
示例#6
0
    def test_all_cases(self, mock_send_request, mock_log):
        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)

        # Setup the rest of the bits.
        ui = UnifiedIndex()
        smmi = SolrMockSearchIndex()
        ui.build(indexes=[smmi])
        connections['default']._index = ui
        sb = connections['default'].get_backend()

        # Prior to the addition of the try/except bits, these would all fail miserably.
        sb.update(smmi, self.sample_objs)
        self.assertEqual(mock_log.call_count, 1)

        sb.remove(self.sample_objs[0])
        self.assertEqual(mock_log.call_count, 2)

        sb.search('search')
        self.assertEqual(mock_log.call_count, 3)

        sb.more_like_this(self.sample_objs[0])
        self.assertEqual(mock_log.call_count, 4)

        sb.clear([MockModel])
        self.assertEqual(mock_log.call_count, 5)

        sb.clear()
        self.assertEqual(mock_log.call_count, 6)
 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.sb = SearchBackend()
     self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
     self.site = SolrSearchSite()
     self.site.register(MockModel, SolrMockSearchIndex)
     
     # Stow.
     self.old_site = sites.site
     sites.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(LiveWhooshSearchQuerySetTestCase, self).setUp()

        # Stow.
        temp_path = os.path.join('tmp', 'test_whoosh_query')
        self.old_whoosh_path = settings.HAYSTACK_CONNECTIONS['default']['PATH']
        settings.HAYSTACK_CONNECTIONS['default']['PATH'] = temp_path

        self.old_ui = connections['default'].get_unified_index()
        self.ui = UnifiedIndex()
        self.wmmi = WhooshMockSearchIndex()
        self.ui.build(indexes=[self.wmmi])
        self.sb = connections['default'].get_backend()
        connections['default']._index = self.ui

        # Stow.
        self.old_debug = settings.DEBUG
        settings.DEBUG = True

        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 = connections['default'].get_query()
        self.sqs = SearchQuerySet()
示例#9
0
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(
            settings.HAYSTACK_CONNECTIONS['default']['URL'])
        clear_solr_index()

        # Stow.
        self.old_ui = connections['default'].get_unified_index()
        self.ui = UnifiedIndex()
        self.smmi = SolrMockSearchIndex()
        self.smtmmi = SolrMaintainTypeMockSearchIndex()
        self.ui.build(indexes=[self.smmi])
        connections['default']._index = self.ui
        self.sb = connections['default'].get_backend()

        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)
示例#10
0
    def setUp(self):
        super(LiveWhooshSearchQueryTestCase, self).setUp()

        # Stow.
        temp_path = os.path.join('tmp', 'test_whoosh_query')
        self.old_whoosh_path = settings.HAYSTACK_CONNECTIONS['default']['PATH']
        settings.HAYSTACK_CONNECTIONS['default']['PATH'] = temp_path

        self.old_ui = connections['default'].get_unified_index()
        self.ui = UnifiedIndex()
        self.wmmi = WhooshMockSearchIndex()
        self.wmtmmi = WhooshMaintainTypeMockSearchIndex()
        self.ui.build(indexes=[self.wmmi])
        self.sb = connections['default'].get_backend()
        connections['default']._index = self.ui

        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 range(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 = connections['default'].get_query()
    def setUp(self):
        super(ElasticsearchSearchBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_es = pyelasticsearch.ElasticSearch(settings.HAYSTACK_CONNECTIONS['default']['URL'])
        clear_elasticsearch_index()

        # Stow.
        self.old_ui = connections['default'].get_unified_index()
        self.ui = UnifiedIndex()
        self.smmi = ElasticsearchMockSearchIndex()
        self.smtmmi = ElasticsearchMaintainTypeMockSearchIndex()
        self.ui.build(indexes=[self.smmi])
        connections['default']._index = self.ui
        self.sb = connections['default'].get_backend()

        # Force the backend to rebuild the mapping each time.
        self.sb.existing_mapping = {}
        self.sb.setup()

        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)
示例#12
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)
示例#13
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 = WhooshSearchSite()
        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 = []

        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)
 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)
    def setUp(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.
        self.old_es_url = settings.HAYSTACK_CONNECTIONS['default']['URL']
        settings.HAYSTACK_CONNECTIONS['default']['URL'] = "%s/foo/" % self.old_es_url
        self.cap = CaptureHandler()
        logging.getLogger('haystack').addHandler(self.cap)
        import haystack
        logging.getLogger('haystack').removeHandler(haystack.stream)

        # Setup the rest of the bits.
        self.old_ui = connections['default'].get_unified_index()
        ui = UnifiedIndex()
        self.smmi = ElasticsearchMockSearchIndex()
        ui.build(indexes=[self.smmi])
        connections['default']._index = ui
        self.sb = connections['default'].get_backend()
    def setUp(self):
        super(ElasticsearchSearchBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_es = pyelasticsearch.ElasticSearch(settings.HAYSTACK_CONNECTIONS['default']['URL'])
        clear_elasticsearch_index()

        # Stow.
        self.old_ui = connections['default'].get_unified_index()
        self.ui = UnifiedIndex()
        self.smmi = ElasticsearchMockSearchIndex()
        self.smtmmi = ElasticsearchMaintainTypeMockSearchIndex()
        self.ui.build(indexes=[self.smmi])
        connections['default']._index = self.ui
        self.sb = connections['default'].get_backend()

        # Force the backend to rebuild the mapping each time.
        self.sb.existing_mapping = {}
        self.sb.setup()

        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)
示例#17
0
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_CONNECTIONS['default']['URL'])
        clear_solr_index()

        # Stow.
        self.old_ui = connections['default'].get_unified_index()
        self.ui = UnifiedIndex()
        self.smmi = SolrMockSearchIndex()
        self.smtmmi = SolrMaintainTypeMockSearchIndex()
        self.ui.build(indexes=[self.smmi])
        connections['default']._index = self.ui
        self.sb = connections['default'].get_backend()
        self.sq = connections['default'].get_query()

        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(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)
示例#19
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):
        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.
        self.old_es_url = settings.HAYSTACK_CONNECTIONS['default']['URL']
        settings.HAYSTACK_CONNECTIONS['default']['URL'] = "%s/foo/" % self.old_es_url
        self.cap = CaptureHandler()
        logging.getLogger('haystack').addHandler(self.cap)
        import haystack
        logging.getLogger('haystack').removeHandler(haystack.stream)

        # Setup the rest of the bits.
        self.old_ui = connections['default'].get_unified_index()
        ui = UnifiedIndex()
        self.smmi = ElasticsearchMockSearchIndex()
        ui.build(indexes=[self.smmi])
        connections['default']._index = ui
        self.sb = connections['default'].get_backend()
示例#21
0
    def test_thread_safety(self):
        # This is a regression. ``SearchIndex`` used to write to
        # ``self.prepared_data``, which would leak between threads if things
        # went too fast.
        exceptions = []

        def threaded_prepare(index_queue, index, model):
            try:
                index.queue = index_queue
                prepped = index.prepare(model)
            except Exception as e:
                exceptions.append(e)
                raise

        class ThreadedSearchIndex(GoodMockSearchIndex):
            def prepare_author(self, obj):
                if obj.pk == 20:
                    time.sleep(0.1)
                else:
                    time.sleep(0.5)

                index_queue.put(self.prepared_data['author'])
                return self.prepared_data['author']

        tmi = ThreadedSearchIndex()
        index_queue = queue.Queue()
        mock_1 = MockModel()
        mock_1.pk = 20
        mock_1.author = 'foo'
        mock_1.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
        mock_2 = MockModel()
        mock_2.pk = 21
        mock_2.author = 'daniel%s' % mock_2.id
        mock_2.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        th1 = Thread(target=threaded_prepare, args=(index_queue, tmi, mock_1))
        th2 = Thread(target=threaded_prepare, args=(index_queue, tmi, mock_2))

        th1.start()
        th2.start()
        th1.join()
        th2.join()

        mock_1_result = index_queue.get()
        mock_2_result = index_queue.get()
        self.assertEqual(mock_1_result, u'foo')
        self.assertEqual(mock_2_result, u'daniel21')
示例#22
0
    def test_thread_safety(self):
        # This is a regression. ``SearchIndex`` used to write to
        # ``self.prepared_data``, which would leak between threads if things
        # went too fast.
        exceptions = []

        def threaded_prepare(index_queue, index, model):
            try:
                index.queue = index_queue
                prepped = index.prepare(model)
            except Exception as e:
                exceptions.append(e)
                raise

        class ThreadedSearchIndex(GoodMockSearchIndex):
            def prepare_author(self, obj):
                if obj.pk == 20:
                    time.sleep(0.1)
                else:
                    time.sleep(0.5)

                index_queue.put(self.prepared_data['author'])
                return self.prepared_data['author']

        tmi = ThreadedSearchIndex()
        index_queue = queue.Queue()
        mock_1 = MockModel()
        mock_1.pk = 20
        mock_1.author = 'foo'
        mock_1.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
        mock_2 = MockModel()
        mock_2.pk = 21
        mock_2.author = 'daniel%s' % mock_2.id
        mock_2.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        th1 = Thread(target=threaded_prepare, args=(index_queue, tmi, mock_1))
        th2 = Thread(target=threaded_prepare, args=(index_queue, tmi, mock_2))

        th1.start()
        th2.start()
        th1.join()
        th2.join()

        mock_1_result = index_queue.get()
        mock_2_result = index_queue.get()
        self.assertEqual(mock_1_result, 'foo')
        self.assertEqual(mock_2_result, 'daniel21')
示例#23
0
 def test_custom_prepare(self):
     mock = MockModel()
     mock.pk = 20
     mock.author = 'daniel%s' % mock.id
     mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
     
     self.assertEqual(len(self.cmi.prepare(mock)), 6)
     self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'content', 'extra', 'hello', 'pub_date', 'whee'])
示例#24
0
    def test_prepare(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.assertEqual(len(self.mi.prepare(mock)), 7)
        self.assertEqual(sorted(self.mi.prepare(mock).keys()), ['author', 'django_ct', 'django_id', 'extra', 'id', 'pub_date', 'text'])
示例#25
0
 def test_prepare(self):
     mock = MockModel()
     mock.pk = 20
     mock.author = 'daniel%s' % mock.id
     mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
     
     self.assertEqual(len(self.mi.prepare(mock)), 7)
     self.assertEqual(sorted(self.mi.prepare(mock).keys()), ['author', 'content', 'django_ct', 'django_id', 'extra', 'id', 'pub_date'])
示例#26
0
 def test_custom_model_attr(self):
     mock = MockModel()
     mock.pk = 20
     mock.author = 'daniel%s' % mock.id
     mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
     
     self.assertEqual(len(self.cmi.prepare(mock)), 9)
     self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'whee'])
     self.assertEqual(self.cmi.prepared_data['hello'], u'World!')
示例#27
0
 def test_nullable(self):
     mock = MockModel()
     mock.pk = 20
     mock.author = None
     mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
     
     prepared_data = self.cnmi.prepare(mock)
     self.assertEqual(len(prepared_data), 4)
     self.assertEqual(sorted(prepared_data.keys()), ['content', 'django_ct', 'django_id', 'id'])
示例#28
0
 def test_update_object(self):
     self.assertEqual(self.msb.docs, {})
     
     mock = MockModel()
     mock.pk = 20
     mock.author = 'daniel%s' % mock.id
     mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
     
     self.mi.update_object(mock)
     self.assertEqual(self.msb.docs, {'core.mockmodel.20': {'django_id': u'20', 'django_ct': u'core.mockmodel', 'author': u'daniel20', 'extra': u'Stored!\n20', 'content': u'Indexed!\n20', 'pub_date': datetime.datetime(2009, 1, 31, 4, 19), 'id': 'core.mockmodel.20'}})
     self.msb.clear()
示例#29
0
    def test_custom_prepare(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.assertEqual(len(self.cmi.prepare(mock)), 11)
        self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'author_exact', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'text', 'whee'])

        self.assertEqual(len(self.cmi.full_prepare(mock)), 11)
        self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), ['author', 'author_exact', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'text', 'whee'])
    def test_custom_prepare(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.assertEqual(len(self.cmi.prepare(mock)), 11)
        self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'author_exact', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'text', 'whee'])

        self.assertEqual(len(self.cmi.full_prepare(mock)), 11)
        self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), ['author', 'author_exact', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'text', 'whee'])
示例#31
0
    def test_custom_index_fieldname(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        cofnmi = GoodOverriddenFieldNameMockSearchIndex()
        self.assertEqual(len(cofnmi.prepare(mock)), 6)
        self.assertEqual(sorted(cofnmi.prepare(mock).keys()), ['django_ct', 'django_id', 'hello', 'id', 'more_content', 'name_s'])
        self.assertEqual(cofnmi.prepared_data['name_s'], 'daniel20')
        self.assertEqual(cofnmi.get_content_field(), 'more_content')
示例#32
0
 def test_custom_index_fieldname(self):
     mock = MockModel()
     mock.pk = 20
     mock.author = 'daniel%s' % mock.id
     mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
     
     cofnmi = GoodOverriddenFieldNameMockSearchIndex(MockModel, backend=self.msb)
     self.assertEqual(len(cofnmi.prepare(mock)), 6)
     self.assertEqual(sorted(cofnmi.prepare(mock).keys()), ['django_ct', 'django_id', 'hello', 'id', 'more_content', 'name_s'])
     self.assertEqual(cofnmi.prepared_data['name_s'], u'daniel20')
     self.assertEqual(cofnmi.get_content_field(), 'more_content')
示例#33
0
    def test_prepare(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = "daniel%s" % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.assertEqual(len(self.mi.prepare(mock)), 7)
        self.assertEqual(
            sorted(self.mi.prepare(mock).keys()),
            ["author", "content", "django_ct", "django_id", "extra", "id", "pub_date"],
        )
示例#34
0
    def test_update_object(self):
        self.sb.clear()
        self.assertEqual(self.sb.search('*')['hits'], 0)

        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [('core.mockmodel', '20')])
        self.sb.clear()
    def test_update_object(self):
        self.sb.clear()
        self.assertEqual(self.sb.search('*')['hits'], 0)

        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual([(res.content_type(), res.pk) for res in self.sb.search('*')['results']], [(u'core.mockmodel', u'20')])
        self.sb.clear()
示例#36
0
    def test_nullable(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = None
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        prepared_data = self.cnmi.prepare(mock)
        self.assertEqual(len(prepared_data), 6)
        self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_exact', 'django_ct', 'django_id', 'id', 'text'])

        prepared_data = self.cnmi.full_prepare(mock)
        self.assertEqual(len(prepared_data), 4)
        self.assertEqual(sorted(prepared_data.keys()), ['django_ct', 'django_id', 'id', 'text'])
    def test_proper_field_resolution(self):
        mrofsc = MROFieldsSearchChild()
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
        mock.test_a = 'This is A'
        mock.test_b = 'This is B'

        self.assertEqual(len(mrofsc.fields), 1)
        prepped_data = mrofsc.prepare(mock)
        self.assertEqual(len(prepped_data), 4)
        self.assertEqual(prepped_data['text'], 'This is A')
示例#38
0
    def test_custom_index_fieldname(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = "daniel%s" % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        cofnmi = GoodOverriddenFieldNameMockSearchIndex(MockModel, backend=self.msb)
        self.assertEqual(len(cofnmi.prepare(mock)), 6)
        self.assertEqual(
            sorted(cofnmi.prepare(mock).keys()), ["django_ct", "django_id", "hello", "id", "more_content", "name_s"]
        )
        self.assertEqual(cofnmi.prepared_data["name_s"], u"daniel20")
        self.assertEqual(cofnmi.get_content_field(), "more_content")
示例#39
0
    def test_proper_field_resolution(self):
        mrofsc = MROFieldsSearchChild()
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
        mock.test_a = 'This is A'
        mock.test_b = 'This is B'

        self.assertEqual(len(mrofsc.fields), 1)
        prepped_data = mrofsc.prepare(mock)
        self.assertEqual(len(prepped_data), 4)
        self.assertEqual(prepped_data['text'], 'This is A')
示例#40
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_CONNECTIONS['default']['URL']
        settings.HAYSTACK_CONNECTIONS['default'][
            'URL'] = "%s/foo/" % old_solr_url
        cap = CaptureHandler()
        logging.getLogger('haystack').addHandler(cap)
        import haystack
        logging.getLogger('haystack').removeHandler(haystack.stream)

        # Setup the rest of the bits.
        old_ui = connections['default'].get_unified_index()
        ui = UnifiedIndex()
        smmi = SolrMockSearchIndex()
        ui.build(indexes=[smmi])
        connections['default']._index = ui
        sb = connections['default'].get_backend()

        # 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_CONNECTIONS['default']['URL'] = old_solr_url
        connections['default']._index = old_ui
        logging.getLogger('haystack').removeHandler(cap)
        logging.getLogger('haystack').addHandler(haystack.stream)
示例#41
0
    def test_update_object(self):
        self.sb.clear()
        self.assertEqual(self.sb.search("*")["hits"], 0)

        mock = MockModel()
        mock.pk = 20
        mock.author = "daniel%s" % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual(
            [(res.content_type(), res.pk) for res in self.sb.search("*")["results"]], [(u"core.mockmodel", u"20")]
        )
        self.sb.clear()
示例#42
0
    def test_nullable(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = None
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        prepared_data = self.cnmi.prepare(mock)
        self.assertEqual(len(prepared_data), 6)
        self.assertEqual(
            sorted(prepared_data.keys()), ["author", "author_exact", "django_ct", "django_id", "id", "text"]
        )

        prepared_data = self.cnmi.full_prepare(mock)
        self.assertEqual(len(prepared_data), 4)
        self.assertEqual(sorted(prepared_data.keys()), ["django_ct", "django_id", "id", "text"])
示例#43
0
 def test_custom_facet_fields(self):
     mock = MockModel()
     mock.pk = 20
     mock.author = 'daniel'
     mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)
     
     prepared_data = self.gfmsi.prepare(mock)
     self.assertEqual(len(prepared_data), 8)
     self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_foo', 'content', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact'])
     
     prepared_data = self.gfmsi.full_prepare(mock)
     self.assertEqual(len(prepared_data), 8)
     self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_foo', 'content', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact'])
     self.assertEqual(prepared_data['author_foo'], u"Hi, I'm daniel")
     self.assertEqual(prepared_data['pub_date_exact'], '2010-10-26T01:54:32')
示例#44
0
    def test_custom_facet_fields(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel'
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        prepared_data = self.gfmsi.prepare(mock)
        self.assertEqual(len(prepared_data), 8)
        self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_foo', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact', 'text'])

        prepared_data = self.gfmsi.full_prepare(mock)
        self.assertEqual(len(prepared_data), 8)
        self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_foo', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact', 'text'])
        self.assertEqual(prepared_data['author_foo'], "Hi, I'm daniel")
        self.assertEqual(prepared_data['pub_date_exact'], '2010-10-26T01:54:32')
    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_CONNECTIONS['default']['URL']
        settings.HAYSTACK_CONNECTIONS['default']['URL'] = "%s/foo/" % old_solr_url
        cap = CaptureHandler()
        logging.getLogger('haystack').addHandler(cap)
        import haystack
        logging.getLogger('haystack').removeHandler(haystack.stream)

        # Setup the rest of the bits.
        old_ui = connections['default'].get_unified_index()
        ui = UnifiedIndex()
        smmi = SolrMockSearchIndex()
        ui.build(indexes=[smmi])
        connections['default']._index = ui
        sb = connections['default'].get_backend()

        # 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_CONNECTIONS['default']['URL'] = old_solr_url
        connections['default']._index = old_ui
        logging.getLogger('haystack').removeHandler(cap)
        logging.getLogger('haystack').addHandler(haystack.stream)
示例#46
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)
示例#47
0
    def test_custom_prepare(self):
        mock = MockModel()
        mock.pk = 20
        mock.author = "daniel%s" % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.assertEqual(len(self.cmi.prepare(mock)), 9)
        self.assertEqual(
            sorted(self.cmi.prepare(mock).keys()),
            ["author", "content", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "whee"],
        )

        self.assertEqual(len(self.cmi.full_prepare(mock)), 10)
        self.assertEqual(
            sorted(self.cmi.full_prepare(mock).keys()),
            ["author", "author_exact", "content", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "whee"],
        )
    def test_count(self):
        more_samples = []

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

        self.sb.update(self.smmi, more_samples)

        backends.reset_search_queries()
        self.assertEqual(len(backends.queries), 0)
        results = self.sqs.all()
        self.assertEqual(len(results), 49)
        self.assertEqual(results._cache_is_full(), False)
        self.assertEqual(len(backends.queries), 1)
示例#49
0
    def test_update_object(self):
        self.assertEqual(self.msb.docs, {})

        mock = MockModel()
        mock.pk = 20
        mock.author = 'daniel%s' % mock.id
        mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0)

        self.mi.update_object(mock)
        self.assertEqual(
            self.msb.docs, {
                'core.mockmodel.20': {
                    'django_id': u'20',
                    'django_ct': u'core.mockmodel',
                    'author': u'daniel20',
                    'extra': u'Stored!\n20',
                    'content': u'Indexed!\n20',
                    'pub_date': datetime.datetime(2009, 1, 31, 4, 19),
                    'id': 'core.mockmodel.20'
                }
            })
        self.msb.clear()
示例#50
0
    def test_all_cases(self, mock_send_request, mock_log):
        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)

        # Setup the rest of the bits.
        ui = UnifiedIndex()
        smmi = SolrMockSearchIndex()
        ui.build(indexes=[smmi])
        connections['default']._index = ui
        sb = connections['default'].get_backend()

        # Prior to the addition of the try/except bits, these would all fail miserably.
        sb.update(smmi, self.sample_objs)
        self.assertEqual(mock_log.call_count, 1)

        sb.remove(self.sample_objs[0])
        self.assertEqual(mock_log.call_count, 2)

        sb.search('search')
        self.assertEqual(mock_log.call_count, 3)

        sb.more_like_this(self.sample_objs[0])
        self.assertEqual(mock_log.call_count, 4)

        sb.clear([MockModel])
        self.assertEqual(mock_log.call_count, 5)

        sb.clear()
        self.assertEqual(mock_log.call_count, 6)