import unittest
import os.path as osp

import tempfile
import shutil
from contextlib import contextmanager
from docido_sdk.core import (
    Component,
    implements,
)
from docido_sdk.env import Environment
from docido_sdk.index import IndexAPIConfigurationProvider
from docido_sdk.index.pipeline import IndexPipelineProvider
import docido_sdk.config as docido_config

from docido_sdk.toolbox.collections_ext import Configuration
from docido_sdk.index.processor import Elasticsearch


class TestEsAPI(unittest.TestCase):
    TEST_DOC = {'id': 'a_test_id'}
    TEST_THUMB = ('thumb', '\x13', 'png')

    @contextmanager
    def index(self):
        """ Create new environment, fill it, and create an IndexAPI
        """
        from docido_sdk.index.config import YamlPullCrawlersIndexingConfig
        config_yaml = osp.splitext(__file__)[0] + '.yml'
        with docido_config:
            docido_config.clear()
            docido_config.update(Configuration.from_file(config_yaml))
            env = Environment()
            env.temp_dir = tempfile.mkdtemp()
            test_components = self._setup_test_components(env)
            pipeline = env[IndexPipelineProvider]
            env[Elasticsearch]
            try:
                # build and provide an IndexAPI
                env[YamlPullCrawlersIndexingConfig]
                yield pipeline.get_index_api(None, None, None)
            finally:
                # Hide from Environment the Component classes defined
                # for this test only.
                for test_component in test_components:
                    test_component.unregister()
                    # Remove temporary directory previously created
                    if osp.isdir(env.temp_dir):
                        shutil.rmtree(env.temp_dir)

    @classmethod
    def _setup_test_components(cls, env):
        class DumbIndexAPIConfiguration(Component):
            implements(IndexAPIConfigurationProvider)

            def get_index_api_conf(self, service, docido_user_id,
                                   account_login):
                return {
                    'service': service,
                    'docido_user_id': docido_user_id,
                    'account_login': account_login,
                    'elasticsearch': {
                        'routing': 'id'
                    }
                }

        return [
            DumbIndexAPIConfiguration
        ]

    def test_push_cards(self):
        with self.index() as index:
            result = index.push_cards([self.TEST_DOC])
            self.assertEqual(result, [])

    def test_delete_cards(self):
        with self.index() as index:
            index.delete_cards({'query': {'match_all': {}}})

    def test_push_and_get(self):
        with self.index() as index:
            index.push_cards([self.TEST_DOC])
            cards_gen = index.search_cards({'query': {'match_all': {}}})
            cards = list(cards_gen)
            self.assertIn(self.TEST_DOC, cards)

    def test_push_thumbnails(self):
        with self.index() as index:
            index.push_thumbnails([self.TEST_THUMB])

    def test_delete_thumbnails(self):
        with self.index() as index:
            index.delete_thumbnails({'query': {'match_all': {}}})

    def test_ping(self):
        with self.index() as index:
            index.ping()

@@ -32,16 +32,28 @@
 as osp%0A%0A
+import time%0A
 import t
@@ -2449,19 +2449,117 @@
 f test_p
-ush
+ing(self):%0A        with self.index() as index:%0A            index.ping()%0A%0A    def test_push_and_delete
 _cards(s
@@ -2699,78 +2699,8 @@
  %5B%5D)
-%0A%0A    def test_delete_cards(self):%0A        with self.index() as index:
 %0A   
@@ -2783,16 +2783,21 @@
 _and_get
+_card
 (self):%0A
@@ -3034,16 +3034,77 @@
 , cards)
+%0A            index.delete_cards(%7B'query': %7B'match_all': %7B%7D%7D%7D)
 %0A%0A    de
@@ -3111,24 +3111,35 @@
 f test_push_
+and_delete_
 thumbnails(s
@@ -3237,232 +3237,70 @@
 B%5D)%0A
-%0A    def test_delete_thumbnails(self):%0A        with self.index() as index:%0A            index.delete_thumbnails(%7B'query': %7B'match_all': %7B%7D%7D%7D)%0A%0A    def test_ping(self):%0A        with self.index() as index:%0A            index.ping(
+            index.delete_thumbnails(%7B'query': %7B'match_all': %7B%7D%7D%7D
 )%0A

Fix es unit tests