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

from casexml.apps.phone.tests.utils import create_restore_user, call_fixture_generator
from corehq import toggles
from corehq.apps.app_manager.fixtures.mobile_ucr import report_fixture_generator
from corehq.apps.app_manager.models import Application, ReportModule, ReportAppConfig
from corehq.apps.users.dbaccessors.all_commcare_users import delete_all_users
from corehq.apps.domain.models import Domain
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.userreports.tests.utils import (
    get_sample_report_config, mock_datasource_config, mock_sql_backend
)


class AppAwareSyncTests(TestCase):
    domain = 'test_domain'
    rows = [{
        'owner': 'bob',
        'count': 3,
        'is_starred': True
    }]

    @classmethod
    def setUpClass(cls):
        delete_all_users()
        create_domain(cls.domain)
        toggles.MOBILE_UCR.set(cls.domain, True, toggles.NAMESPACE_DOMAIN)
        cls.user = create_restore_user(cls.domain)

        cls.app1 = Application.new_app(cls.domain, 'Test App 1')
        cls.report_config1 = get_sample_report_config()
        cls.report_config1.domain = cls.domain
        cls.report_config1.save()
        report_app_config = {
            'report_id': cls.report_config1.get_id,
            'uuid': '123456'
        }
        module = cls.app1.add_module(ReportModule.new_module('Reports', None))
        module.report_configs = [ReportAppConfig.wrap(report_app_config)]
        cls.app1.save()

        cls.app2 = Application.new_app(cls.domain, 'Test App 2')
        cls.report_config2 = get_sample_report_config()
        cls.report_config2.domain = cls.domain
        cls.report_config2.save()
        report_app_config = {
            'report_id': cls.report_config2.get_id,
            'uuid': 'abcdef'
        }
        module = cls.app2.add_module(ReportModule.new_module('Reports', None))
        module.report_configs = [ReportAppConfig.wrap(report_app_config)]
        cls.app2.save()

        cls.app3 = Application.new_app(cls.domain, 'Test App 3')
        cls.app3.save()

    @classmethod
    def tearDownClass(cls):
        toggles.MOBILE_UCR.set(cls.domain, False, toggles.NAMESPACE_DOMAIN)
        cls.app1.delete()
        cls.report_config1.delete()
        cls.app2.delete()
        cls.report_config2.delete()
        delete_all_users()
        domain = Domain.get_by_name(cls.domain)
        domain.delete()

    def test_report_fixtures_provider_without_app(self):
        """
        ReportFixturesProvider should iterate all apps if app not given
        """
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            with mock_sql_backend():
                with mock_datasource_config():
                    fixtures = call_fixture_generator(report_fixture_generator, self.user)
        reports = fixtures[0].findall('.//report')
        self.assertEqual(len(reports), 2)
        report_ids = {r.attrib.get('id') for r in reports}
        self.assertEqual(report_ids, {'123456', 'abcdef'})

    def test_report_fixtures_provider_with_app(self):
        """
        ReportFixturesProvider should not iterate all apps if app given
        """
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            with mock_sql_backend():
                with mock_datasource_config():
                    fixtures = call_fixture_generator(report_fixture_generator, self.user, app=self.app1)
        reports = fixtures[0].findall('.//report')
        self.assertEqual(len(reports), 1)
        self.assertEqual(reports[0].attrib.get('id'), '123456')

    def test_report_fixtures_provider_with_app_that_doesnt_have_reports(self):
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            fixtures = call_fixture_generator(report_fixture_generator, self.user, app=self.app3)
        self.assertEqual(len(fixtures), 0)<commit_msg>test mobile ucr makes into restore (if configured)<commit_after>from django.test import TestCase
from mock import patch

from casexml.apps.phone.tests.utils import create_restore_user, call_fixture_generator
from corehq import toggles
from corehq.apps.app_manager.fixtures.mobile_ucr import report_fixture_generator
from corehq.apps.app_manager.models import Application, ReportModule, ReportAppConfig
from corehq.apps.users.dbaccessors.all_commcare_users import delete_all_users
from corehq.apps.domain.models import Domain
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.userreports.tests.utils import (
    get_sample_report_config, mock_datasource_config, mock_sql_backend
)


class AppAwareSyncTests(TestCase):
    domain = 'test_domain'
    rows = [{
        'owner': 'bob',
        'count': 3,
        'is_starred': True
    }]

    @classmethod
    def setUpClass(cls):
        delete_all_users()
        cls.domain_obj = create_domain(cls.domain)
        toggles.MOBILE_UCR.set(cls.domain, True, toggles.NAMESPACE_DOMAIN)
        cls.user = create_restore_user(cls.domain)

        cls.app1 = Application.new_app(cls.domain, 'Test App 1')
        cls.report_config1 = get_sample_report_config()
        cls.report_config1.domain = cls.domain
        cls.report_config1.save()
        report_app_config = {
            'report_id': cls.report_config1.get_id,
            'uuid': '123456'
        }
        module = cls.app1.add_module(ReportModule.new_module('Reports', None))
        module.report_configs = [ReportAppConfig.wrap(report_app_config)]
        cls.app1.save()

        cls.app2 = Application.new_app(cls.domain, 'Test App 2')
        cls.report_config2 = get_sample_report_config()
        cls.report_config2.domain = cls.domain
        cls.report_config2.save()
        report_app_config = {
            'report_id': cls.report_config2.get_id,
            'uuid': 'abcdef'
        }
        module = cls.app2.add_module(ReportModule.new_module('Reports', None))
        module.report_configs = [ReportAppConfig.wrap(report_app_config)]
        cls.app2.save()

        cls.app3 = Application.new_app(cls.domain, 'Test App 3')
        cls.app3.save()

    @classmethod
    def tearDownClass(cls):
        toggles.MOBILE_UCR.set(cls.domain, False, toggles.NAMESPACE_DOMAIN)
        cls.app1.delete()
        cls.report_config1.delete()
        cls.app2.delete()
        cls.report_config2.delete()
        delete_all_users()
        domain = Domain.get_by_name(cls.domain)
        domain.delete()

    def test_report_fixtures_provider_without_app(self):
        """
        ReportFixturesProvider should iterate all apps if app not given
        """
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            with mock_sql_backend():
                with mock_datasource_config():
                    fixtures = call_fixture_generator(report_fixture_generator, self.user)
        reports = fixtures[0].findall('.//report')
        self.assertEqual(len(reports), 2)
        report_ids = {r.attrib.get('id') for r in reports}
        self.assertEqual(report_ids, {'123456', 'abcdef'})

    def test_report_fixtures_provider_with_app(self):
        """
        ReportFixturesProvider should not iterate all apps if app given
        """
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            with mock_sql_backend():
                with mock_datasource_config():
                    fixtures = call_fixture_generator(report_fixture_generator, self.user, app=self.app1)
        reports = fixtures[0].findall('.//report')
        self.assertEqual(len(reports), 1)
        self.assertEqual(reports[0].attrib.get('id'), '123456')

    def test_report_fixtures_provider_with_app_that_doesnt_have_reports(self):
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource
        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            fixtures = call_fixture_generator(report_fixture_generator, self.user, app=self.app3)
        self.assertEqual(len(fixtures), 0)

    def test_user_restore(self):
        from casexml.apps.phone.tests.utils import generate_restore_payload
        from casexml.apps.case.xml import V3
        from corehq.apps.userreports.reports.data_source import ConfigurableReportDataSource

        with patch.object(ConfigurableReportDataSource, 'get_data') as get_data_mock:
            get_data_mock.return_value = self.rows
            with mock_sql_backend():
                with mock_datasource_config():
                    restore = generate_restore_payload(self.domain_obj, self.user, version=V3)
                    self.assertIn('<fixture id="commcare:reports"', restore)
                    self.assertIn('report_id="{id}"'.format(id=self.report_config1._id), restore)
                    self.assertIn('report_id="{id}"'.format(id=self.report_config2._id), restore)
{'fraction_non_alphanumeric': 0.094306778619701, 'fraction_numerical': 0.010649191071062871, 'mean_word_length': 3.079782790309106, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '1077413', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2908, 'n_words': 493}