import uuid

from django.test import TestCase, override_settings
from elasticsearch.exceptions import ConnectionError

from corehq.apps.es import CaseES
from corehq.elastic import get_es_new
from corehq.form_processor.tests.utils import FormProcessorTestUtils, run_with_all_backends
from corehq.pillows.mappings.reportcase_mapping import REPORT_CASE_INDEX_INFO
from corehq.util.elastic import ensure_index_deleted
from corehq.util.test_utils import trap_extra_setup, create_and_save_a_case
from pillowtop.es_utils import initialize_index_and_mapping
from testapps.test_pillowtop.utils import process_kafka_changes, process_couch_changes

DOMAIN = 'report-case-pillowtest-domain'


@override_settings(ES_CASE_FULL_INDEX_DOMAINS=[DOMAIN])
class ReportCasePillowTest(TestCase):

    def setUp(self):
        super(ReportCasePillowTest, self).setUp()
        FormProcessorTestUtils.delete_all_xforms()
        FormProcessorTestUtils.delete_all_cases()
        with trap_extra_setup(ConnectionError):
            self.elasticsearch = get_es_new()
            ensure_index_deleted(REPORT_CASE_INDEX_INFO.index)
            initialize_index_and_mapping(self.elasticsearch, REPORT_CASE_INDEX_INFO)

    def tearDown(self):
        ensure_index_deleted(REPORT_CASE_INDEX_INFO.index)
        FormProcessorTestUtils.delete_all_xforms()
        FormProcessorTestUtils.delete_all_cases()
        super(ReportCasePillowTest, self).tearDown()

    @run_with_all_backends
    def test_report_case_pillow(self):
        case_id, case_name = self._create_case_and_sync_to_es(DOMAIN)

        # confirm change made it to elasticserach
        results = CaseES('report_cases').run()
        self.assertEqual(1, results.total)
        case_doc = results.hits[0]
        self.assertEqual(DOMAIN, case_doc['domain'])
        self.assertEqual(case_id, case_doc['_id'])
        self.assertEqual(case_name, case_doc['name'])

    @run_with_all_backends
    def test_unsupported_domain(self):
        self._create_case_and_sync_to_es('unsupported-domain')

        results = CaseES('report_cases').run()
        self.assertEqual(0, results.total)

    def _create_case_and_sync_to_es(self, domain):
        case_id = uuid.uuid4().hex
        case_name = 'case-name-{}'.format(uuid.uuid4().hex)
        with process_kafka_changes('ReportCaseToElasticsearchPillow'):
            with process_couch_changes('DefaultChangeFeedPillow'):
                create_and_save_a_case(domain, case_id, case_name)
        self.elasticsearch.indices.refresh(REPORT_CASE_INDEX_INFO.index)
        return case_id, case_name

@@ -354,16 +354,80 @@
 EX_INFO%0A
+from corehq.pillows.reportcase import get_report_case_reindexer%0A
 from cor
@@ -2636,8 +2636,1366 @@
 se_name%0A
+%0A%0A@override_settings(ES_CASE_FULL_INDEX_DOMAINS=%5BDOMAIN%5D)%0Aclass ReportCaseReindexerTest(TestCase):%0A%0A    def setUp(self):%0A        super(ReportCaseReindexerTest, self).setUp()%0A        FormProcessorTestUtils.delete_all_xforms()%0A        FormProcessorTestUtils.delete_all_cases()%0A        with trap_extra_setup(ConnectionError):%0A            self.elasticsearch = get_es_new()%0A            ensure_index_deleted(REPORT_CASE_INDEX_INFO.index)%0A%0A    def tearDown(self):%0A        FormProcessorTestUtils.delete_all_xforms()%0A        FormProcessorTestUtils.delete_all_cases()%0A        ensure_index_deleted(REPORT_CASE_INDEX_INFO.index)%0A        super(ReportCaseReindexerTest, self).tearDown()%0A%0A    @run_with_all_backends%0A    def test_report_case_reindexer(self):%0A        cases_included = set()%0A        for i in range(3):%0A            case = create_and_save_a_case(DOMAIN, uuid.uuid4().hex, 'case_name-%7B%7D'.format(i))%0A            cases_included.add(case.case_id)%0A%0A        # excluded case%0A        create_and_save_a_case('unsupported', uuid.uuid4().hex, 'unsupported')%0A%0A        reindexer = get_report_case_reindexer()%0A        reindexer.reindex()%0A%0A        # verify there%0A        results = CaseES(%22report_cases%22).run()%0A        self.assertEqual(3, results.total, results.hits)%0A        ids_in_es = %7Bdoc%5B'_id'%5D for doc in results.hits%7D%0A        self.assertEqual(cases_included, ids_in_es)%0A

add report case reindexer test