def test_haystack_update(self, mock_obj): """ Test the update method on the Haystack backend """ engine = ElasticsearchSearchEngine() es = ElasticsearchSearchBackend('default', **Data.connection_options) engine._backend = es es.setup() unified_index = engine.get_unified_index() index = unified_index.get_index(Document) iterable = Document.objects.all() es.update(index, iterable) es.conn.indices.refresh.assert_called_with(index='testproject') self.assertTrue(es.conn.bulk.called) call_args = es.conn.bulk.call_args[0][0] self.assertEqual(6, len(call_args)) self.assertEqual({'index': {'_id': 'testproject.document.1'}}, call_args[0]) self.assertEqual({'index': {'_id': 'testproject.document.2'}}, call_args[2]) self.assertEqual({'index': {'_id': 'testproject.document.3'}}, call_args[4]) self.assertIn('Republican leaders justified their policy', call_args[1]['text']) doc1 = Document.objects.get(pk=1) self.assertIn(doc1.text, call_args[1]['text']) self.assertIn('the PSA test sometimes shows erroneous results', call_args[3]['text']) self.assertIn('The announcement of the probable discovery of the Higgs boson', call_args[5]['text'])
def test_haystack_search(self, mock_es): es = ElasticsearchSearchBackend('default', **Data.connection_options) self.assertFalse(es.setup_complete) es.setup() es.search('*:*', end_offset=1) # es.conn.search.assert_called_with(**Data.search_kwargs) self.assert_called_with_search_kwargs(es.conn.search)
def test_haystack_process_results(self, mock_es): es = ElasticsearchSearchBackend('default', **Data.connection_options) es.setup() results = es._process_results(Data.raw_results) expected = { 'hits': 0, 'spelling_suggestion': None, 'results': [], 'facets': {} } self.assertEqual(expected, results)
def test_setup_on_haystack_backend(self, mock_obj): """ Tests the Setup method on the elasticsearch backend. """ es = ElasticsearchSearchBackend("default", **Data.connection_options) self.assertFalse(es.setup_complete) es.setup() self.assertTrue(es.setup_complete) self.assertEqual(es.existing_mapping, Data.existing_mapping) es.conn.indices.create.assert_called_with(index="testproject", ignore=400, body=es.DEFAULT_SETTINGS) es.conn.indices.put_mapping.assert_called_with( index="testproject", doc_type="modelresult", body=Data.existing_mapping )
def test_setup_on_haystack_backend(self, mock_obj): """ Tests the Setup method on the elasticsearch backend. """ es = ElasticsearchSearchBackend('default', **Data.connection_options) self.assertFalse(es.setup_complete) es.setup() self.assertTrue(es.setup_complete) self.assertEqual(es.existing_mapping, Data.existing_mapping) es.conn.indices.create.assert_called_with(index='testproject', ignore=400, body=es.DEFAULT_SETTINGS) es.conn.indices.put_mapping.assert_called_with(index='testproject', doc_type='modelresult', body=Data.existing_mapping)
def test_haystack_update(self, mock_obj): """ Test the update method on the Haystack backend """ engine = ElasticsearchSearchEngine() es = ElasticsearchSearchBackend("default", **Data.connection_options) engine._backend = es es.setup() unified_index = engine.get_unified_index() index = unified_index.get_index(Document) iterable = Document.objects.all() es.update(index, iterable) es.conn.indices.refresh.assert_called_with(index="testproject") self.assertTrue(es.conn.bulk.called) call_args = es.conn.bulk.call_args[0][0] self.assertEqual(6, len(call_args)) self.assertEqual({"index": {"_id": "testproject.document.1"}}, call_args[0]) self.assertEqual({"index": {"_id": "testproject.document.2"}}, call_args[2]) self.assertEqual({"index": {"_id": "testproject.document.3"}}, call_args[4]) self.assertIn("Republican leaders justified their policy", call_args[1]["text"]) doc1 = Document.objects.get(pk=1) self.assertIn(doc1.text, call_args[1]["text"]) self.assertIn("the PSA test sometimes shows erroneous results", call_args[3]["text"]) self.assertIn("The announcement of the probable discovery of the Higgs boson", call_args[5]["text"])
def test_haystack_process_results(self, mock_es): es = ElasticsearchSearchBackend('default', **Data.connection_options) es.setup() results = es._process_results(Data.raw_results) expected = {'hits': 0, 'spelling_suggestion': None, 'results': [], 'facets': {}} self.assertEqual(expected, results)
def test_haystack_clear(self, mock_obj): es = ElasticsearchSearchBackend('default', **Data.connection_options) es.setup() es.clear(models=None, commit=True) # commit is ignored anyway es.conn.indices.delete.assert_called_with(index='testproject', ignore=404)
def test_haystack_clear(self, mock_obj): es = ElasticsearchSearchBackend("default", **Data.connection_options) es.setup() es.clear(commit=True) # commit is ignored anyway es.conn.indices.delete.assert_called_with(index="testproject", ignore=404)
def test_haystack_search(self, mock_es): es = ElasticsearchSearchBackend('default', **Data.connection_options) self.assertFalse(es.setup_complete) es.setup() es.search('*:*', end_offset=1) es.conn.search.assert_called_with(**Data.search_kwargs)