<commit_before>import uuid
from unittest.mock import patch

from django.test import TestCase

from elasticsearch.exceptions import ConnectionError

from pillow_retry.models import PillowError
from pillowtop.es_utils import initialize_index_and_mapping

from corehq.apps.es import CaseES, CaseSearchES
from corehq.elastic import get_es_new
from corehq.form_processor.interfaces.dbaccessors import CaseAccessors
from corehq.form_processor.tests.utils import (
    FormProcessorTestUtils,
    run_with_all_backends,
)
from corehq.pillows.mappings.case_mapping import CASE_INDEX_INFO
from corehq.pillows.mappings.case_search_mapping import CASE_SEARCH_INDEX_INFO
from corehq.util.elastic import delete_es_index, ensure_index_deleted
from corehq.util.test_utils import create_and_save_a_case, trap_extra_setup
from testapps.test_pillowtop.utils import process_pillow_changes


class CasePillowTest(TestCase):
    domain = 'case-pillowtest-domain'

    @classmethod
    def setUpClass(cls):
        super(CasePillowTest, cls).setUpClass()
        delete_es_index(CASE_INDEX_INFO.index)
        delete_es_index(CASE_SEARCH_INDEX_INFO.index)

    def setUp(self):
        super(CasePillowTest, self).setUp()
        self.process_case_changes = process_pillow_changes('DefaultChangeFeedPillow')
        self.process_case_changes.add_pillow('case-pillow', {'skip_ucr': True})
        FormProcessorTestUtils.delete_all_cases()
        with trap_extra_setup(ConnectionError):
            self.elasticsearch = get_es_new()
            initialize_index_and_mapping(self.elasticsearch, CASE_INDEX_INFO)
            initialize_index_and_mapping(self.elasticsearch, CASE_SEARCH_INDEX_INFO)

    def tearDown(self):
        ensure_index_deleted(CASE_INDEX_INFO.index)
        ensure_index_deleted(CASE_SEARCH_INDEX_INFO.index)
        FormProcessorTestUtils.delete_all_cases_forms_ledgers(self.domain)
        PillowError.objects.all().delete()
        super(CasePillowTest, self).tearDown()

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

        # confirm change made it to elasticserach
        results = CaseES().run()
        self.assertEqual(1, results.total)
        case_doc = results.hits[0]
        self.assertEqual(self.domain, case_doc['domain'])<commit_msg>Use ensure_index_deleted for case-pillow test<commit_after>import uuid
from unittest.mock import patch

from django.test import TestCase

from elasticsearch.exceptions import ConnectionError

from pillow_retry.models import PillowError
from pillowtop.es_utils import initialize_index_and_mapping

from corehq.apps.es import CaseES, CaseSearchES
from corehq.elastic import get_es_new
from corehq.form_processor.interfaces.dbaccessors import CaseAccessors
from corehq.form_processor.tests.utils import (
    FormProcessorTestUtils,
    run_with_all_backends,
)
from corehq.pillows.mappings.case_mapping import CASE_INDEX_INFO
from corehq.pillows.mappings.case_search_mapping import CASE_SEARCH_INDEX_INFO
from corehq.util.elastic import ensure_index_deleted
from corehq.util.test_utils import create_and_save_a_case, trap_extra_setup
from testapps.test_pillowtop.utils import process_pillow_changes


class CasePillowTest(TestCase):
    domain = 'case-pillowtest-domain'

    @classmethod
    def setUpClass(cls):
        super(CasePillowTest, cls).setUpClass()
        ensure_index_deleted(CASE_INDEX_INFO.index)
        ensure_index_deleted(CASE_SEARCH_INDEX_INFO.index)

    def setUp(self):
        super(CasePillowTest, self).setUp()
        self.process_case_changes = process_pillow_changes('DefaultChangeFeedPillow')
        self.process_case_changes.add_pillow('case-pillow', {'skip_ucr': True})
        FormProcessorTestUtils.delete_all_cases()
        with trap_extra_setup(ConnectionError):
            self.elasticsearch = get_es_new()
            initialize_index_and_mapping(self.elasticsearch, CASE_INDEX_INFO)
            initialize_index_and_mapping(self.elasticsearch, CASE_SEARCH_INDEX_INFO)

    def tearDown(self):
        ensure_index_deleted(CASE_INDEX_INFO.index)
        ensure_index_deleted(CASE_SEARCH_INDEX_INFO.index)
        FormProcessorTestUtils.delete_all_cases_forms_ledgers(self.domain)
        PillowError.objects.all().delete()
        super(CasePillowTest, self).tearDown()

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

        # confirm change made it to elasticserach
        results = CaseES().run()
        self.assertEqual(1, results.total)
        case_doc = results.hits[0]
        self.assertEqual(self.domain, case_doc['domain'])
{'fraction_non_alphanumeric': 0.07089472560324578, 'fraction_numerical': 0.0008541533205210335, 'mean_word_length': 4.582836710369487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3307834', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1469, 'n_words': 240}