<commit_before>import mock
import datetime
from django.test import TestCase

from corehq.apps.fixtures.models import FixtureDataType, FixtureTypeField, \
    FixtureDataItem, FieldList, FixtureItemField
from corehq.form_processor.interfaces.dbaccessors import CaseAccessors
from corehq.form_processor.tests.utils import FormProcessorTestUtils

from casexml.apps.case.mock import CaseFactory, CaseStructure
from corehq.apps.users.models import CommCareUser
from corehq.apps.userreports.tasks import rebuild_indicators

from custom.enikshay.const import (
    DOSE_MISSED,
    DOSE_TAKEN_INDICATORS as DTIndicators,
    DOSE_UNKNOWN,
    DAILY_SCHEDULE_FIXTURE_NAME,
    SCHEDULE_ID_FIXTURE,
    HISTORICAL_CLOSURE_REASON,
)
from custom.enikshay.data_store import AdherenceDatastore
from custom.enikshay.tasks import EpisodeUpdater, EpisodeAdherenceUpdate
from custom.enikshay.integrations.ninetyninedots.utils import update_episode_adherence_properties
from custom.enikshay.tests.utils import (
    get_person_case_structure,
    get_adherence_case_structure,
    get_occurrence_case_structure,
    get_episode_case_structure
)


class TestAdherenceUpdater(TestCase):
    _call_center_domain_mock = mock.patch(
        'corehq.apps.callcenter.data_source.call_center_data_source_configuration_provider'
    )

    @classmethod
    def setUpClass(cls):
        super(TestAdherenceUpdater, cls).setUpClass()
        cls._call_center_domain_mock.start()
        cls.domain = 'enikshay'
        cls.user = CommCareUser.create(
            cls.domain,
            "jon-snow@user",
            "123",
        )
        cls.setupFixtureData()
        cls.data_store = AdherenceDatastore(cls.domain)

    def setUp(self):
        super(TestAdherenceUpdater, self).setUp()
        self.factory = CaseFactory(domain=self.domain)
        self.person_id = u"person"
        self.occurrence_id = u"occurrence"
        self.episode_id = u"episode"
        self.case_updater = EpisodeUpdater(self.domain)

    @classmethod
    def setupFixtureData(cls):
        cls.fixture_data = {
            'schedule1': '7',
            'schedule2': '14',
            'schedule3': '21',
        }
        cls.data_type = FixtureDataType(
            domain=cls.domain,
            tag=DAILY_SCHEDULE_FIXTURE_NAME,
            name=DAILY_SCHEDULE_FIXTURE_NAME,
            fields=[
                FixtureTypeField(
                    field_name=SCHEDULE_ID_FIXTURE,
                    properties=[]
                ),
                FixtureTypeField(
                    field_name="doses_per_week",
                    properties=[]
                ),
            ],
            item_attributes=[],
        )
        cls.data_type.save()
        cls.data_items = []
        for _id, value in cls.fixture_data.iteritems():
            data_item = FixtureDataItem(
                domain=cls.domain,
                data_type_id=cls.data_type.get_id,
                fields={
                    SCHEDULE_ID_FIXTURE: FieldList(
                        field_list=[
                            FixtureItemField(
                                field_value=_id,
                            )
                        ]
                    ),
                    "doses_per_week": FieldList(
                        field_list=[
                            FixtureItemField(
                                field_value=value,
                            )
                        ]
                    )
                },
                item_attributes={},
            )
            data_item.save()
            cls.data_items.append(data_item)

    @classmethod
    def tearDownClass(cls):
        cls.user.delete()
        cls.data_type.delete()
        for data_item in cls.data_items:
            data_item.delete()
        cls.data_store.adapter.drop_table()
        super(TestAdherenceUpdater, cls).tearDownClass()
        cls._call_center_domain_mock.stop()

    def tearDown(self):
        self.data_store.adapter.clear_table()
        FormProcessorTestUtils.delete_all_cases()

    def _create_episode_case(self, adherence_schedule_date_start=None, adherence_schedule_id=None):
        person = get_person_case_structure(
            self.person_id,
            self.user.user_id,
        )

        occurrence = get_occurrence_case_structure(
            self.occurrence_id,
            person
        )

        episode_structure = get_episode_case_structure(
            self.episode_id,
            occurrence,
            extra_update={
                'adherence_schedule_date_start': adherence_schedule_date_start,
                'adherence_schedule_id': adherence_schedule_id
            }
        )
        cases = {case.case_id: case for case in self.factory.create_or_update_cases([episode_structure])}
        episode_case = cases[self.episode_id]
        self.case_updater._get_open_episode_cases = mock.MagicMock(return_value=[episode_case])
        return episode_case

    def _create_adherence_cases(self, case_dicts):
        return self.factory.create_or_update_cases([
            get_adherence_case_structure(
                "adherence{}".format(i),
                self.episode_id,
                case['name'],
                extra_update=case
            )
            for i, case in enumerate(case_dicts)
        ])

    def assert_update(self, purge_date, adherence_schedule_date_start,
                      adherence_schedule_id, adherence_cases,
                      date_today_in_india=None, output=None):
        adherence_cases = [
            {
                "name": adherence_case[0],
                "adherence_value": adherence_case[1],
                "adherence_source": "enikshay",
                "adherence_report_source": "treatment_supervisor"
            }
            for adherence_case in adherence_cases
        ]
        episode = self.create_episode_case(
            adherence_schedule_date_start, adherence_schedule_id, adherence_cases
        )

        updater = EpisodeAdherenceUpdate(self.domain, episode)
        updater.purge_date = purge_date
        if date_today_in_india is not None:
            updater.date_today_in_india = date_today_in_india

        return self._assert_properties_equal(updater.update_json(), output)

    def _assert_properties_equal(self, update_json, output):

        self.assertDictEqual(
            {key: str(update_json[key]) for key in output},
            {key: str(val) for key, val in output.iteritems()}  # convert values to strings<commit_msg>minor test change for readability<commit_after>import mock
import datetime
from django.test import TestCase

from corehq.apps.fixtures.models import FixtureDataType, FixtureTypeField, \
    FixtureDataItem, FieldList, FixtureItemField
from corehq.form_processor.interfaces.dbaccessors import CaseAccessors
from corehq.form_processor.tests.utils import FormProcessorTestUtils

from casexml.apps.case.mock import CaseFactory, CaseStructure
from corehq.apps.users.models import CommCareUser
from corehq.apps.userreports.tasks import rebuild_indicators

from custom.enikshay.const import (
    DOSE_MISSED,
    DOSE_TAKEN_INDICATORS as DTIndicators,
    DOSE_UNKNOWN,
    DAILY_SCHEDULE_FIXTURE_NAME,
    SCHEDULE_ID_FIXTURE,
    HISTORICAL_CLOSURE_REASON,
)
from custom.enikshay.data_store import AdherenceDatastore
from custom.enikshay.tasks import EpisodeUpdater, EpisodeAdherenceUpdate
from custom.enikshay.integrations.ninetyninedots.utils import update_episode_adherence_properties
from custom.enikshay.tests.utils import (
    get_person_case_structure,
    get_adherence_case_structure,
    get_occurrence_case_structure,
    get_episode_case_structure
)


class TestAdherenceUpdater(TestCase):
    _call_center_domain_mock = mock.patch(
        'corehq.apps.callcenter.data_source.call_center_data_source_configuration_provider'
    )

    @classmethod
    def setUpClass(cls):
        super(TestAdherenceUpdater, cls).setUpClass()
        cls._call_center_domain_mock.start()
        cls.domain = 'enikshay'
        cls.user = CommCareUser.create(
            cls.domain,
            "jon-snow@user",
            "123",
        )
        cls.setupFixtureData()
        cls.data_store = AdherenceDatastore(cls.domain)

    def setUp(self):
        super(TestAdherenceUpdater, self).setUp()
        self.factory = CaseFactory(domain=self.domain)
        self.person_id = u"person"
        self.occurrence_id = u"occurrence"
        self.episode_id = u"episode"
        self.case_updater = EpisodeUpdater(self.domain)

    @classmethod
    def setupFixtureData(cls):
        cls.fixture_data = {
            'schedule1': '7',
            'schedule2': '14',
            'schedule3': '21',
        }
        cls.data_type = FixtureDataType(
            domain=cls.domain,
            tag=DAILY_SCHEDULE_FIXTURE_NAME,
            name=DAILY_SCHEDULE_FIXTURE_NAME,
            fields=[
                FixtureTypeField(
                    field_name=SCHEDULE_ID_FIXTURE,
                    properties=[]
                ),
                FixtureTypeField(
                    field_name="doses_per_week",
                    properties=[]
                ),
            ],
            item_attributes=[],
        )
        cls.data_type.save()
        cls.data_items = []
        for _id, value in cls.fixture_data.iteritems():
            data_item = FixtureDataItem(
                domain=cls.domain,
                data_type_id=cls.data_type.get_id,
                fields={
                    SCHEDULE_ID_FIXTURE: FieldList(
                        field_list=[
                            FixtureItemField(
                                field_value=_id,
                            )
                        ]
                    ),
                    "doses_per_week": FieldList(
                        field_list=[
                            FixtureItemField(
                                field_value=value,
                            )
                        ]
                    )
                },
                item_attributes={},
            )
            data_item.save()
            cls.data_items.append(data_item)

    @classmethod
    def tearDownClass(cls):
        cls.user.delete()
        cls.data_type.delete()
        for data_item in cls.data_items:
            data_item.delete()
        cls.data_store.adapter.drop_table()
        super(TestAdherenceUpdater, cls).tearDownClass()
        cls._call_center_domain_mock.stop()

    def tearDown(self):
        self.data_store.adapter.clear_table()
        FormProcessorTestUtils.delete_all_cases()

    def _create_episode_case(self, adherence_schedule_date_start=None, adherence_schedule_id=None):
        person = get_person_case_structure(
            self.person_id,
            self.user.user_id,
        )

        occurrence = get_occurrence_case_structure(
            self.occurrence_id,
            person
        )

        episode_structure = get_episode_case_structure(
            self.episode_id,
            occurrence,
            extra_update={
                'adherence_schedule_date_start': adherence_schedule_date_start,
                'adherence_schedule_id': adherence_schedule_id
            }
        )
        cases = {case.case_id: case for case in self.factory.create_or_update_cases([episode_structure])}
        episode_case = cases[self.episode_id]
        self.case_updater._get_open_episode_cases = mock.MagicMock(return_value=[episode_case])
        return episode_case

    def _create_adherence_cases(self, case_dicts):
        return self.factory.create_or_update_cases([
            get_adherence_case_structure(
                "adherence{}".format(i),
                self.episode_id,
                case['name'],
                extra_update=case
            )
            for i, case in enumerate(case_dicts)
        ])

    def assert_update(self, purge_date, adherence_schedule_date_start,
                      adherence_schedule_id, adherence_cases,
                      date_today_in_india=None, output=None):
        adherence_cases = [
            {
                "name": adherence_case[0],
                "adherence_value": adherence_case[1],
                "adherence_source": "enikshay",
                "adherence_report_source": "treatment_supervisor"
            }
            for adherence_case in adherence_cases
        ]
        episode = self.create_episode_case(
            adherence_schedule_date_start, adherence_schedule_id, adherence_cases
        )

        updater = EpisodeAdherenceUpdate(self.domain, episode)
        updater.purge_date = purge_date
        if date_today_in_india is not None:
            updater.date_today_in_india = date_today_in_india

        return self._assert_properties_equal(updater.update_json(), output)

    def _assert_properties_equal(self, update_json, output):

        self.assertDictContainsSubset(
            # convert values to strings
            {key: str(val) for key, val in output.iteritems()},
            {key: str(update_json[key]) for key in output},
{'fraction_non_alphanumeric': 0.07445589919816724, 'fraction_numerical': 0.00198549064528446, 'mean_word_length': 2.007808911345889, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2227828', 'n_tokens_mistral': 3947, 'n_tokens_neox': 3726, 'n_words': 565}