def test_routing_save_docs(self):
        """Test that for a plugin that specifies routing_id field will
        end up with "_routing" set while indexing.
        """
        mock_engine = mock.Mock()
        plugin = fake_plugins.FakeSimpleRoutingPlugin(es_engine=mock_engine)
        indexing_helper = helper.IndexingHelper(plugin)

        bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk'
        with mock.patch(bulk_name) as mock_bulk:
            count = len(plugin.get_objects())
            fake_versions = range(1, count + 1)
            indexing_helper.save_documents(plugin.get_objects(), fake_versions)
            self.assertEqual(1, len(mock_bulk.call_args_list))
            actions = list(mock_bulk.call_args_list[0][1]['actions'])

        # '_routing' is added to action only if set in the plugin property
        # FakeSimpleRoutingPlugin has it defined.
        self.assertIs(True, '_routing' in actions[0])
        self.assertEqual('tenant1', actions[0]['_routing'])
    def test_routing_delete(self):
        """Test that deletion for a routing based plugin deletes docs"""
        mock_engine = mock.Mock()
        plugin = fake_plugins.FakeSimpleRoutingPlugin(es_engine=mock_engine)
        indexing_helper = helper.IndexingHelper(plugin)

        bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk'
        with mock.patch(bulk_name) as mock_bulk:
            indexing_helper.delete_document({
                '_id': 'id_for_routing_plugin-fake1',
                '_routing': 'tenant1'
            })

            expected_delete_actions = [{
                '_op_type': 'delete',
                '_id': 'id_for_routing_plugin-fake1',
                '_routing': 'tenant1'
            }]
            mock_bulk.assert_called_once_with(client=plugin.engine,
                                              index=plugin.alias_name_listener,
                                              doc_type=plugin.document_type,
                                              actions=expected_delete_actions)