<commit_before>import uuid

from django.test.testcases import TestCase

from corehq.apps.app_manager.models import Application, LinkedApplication
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.fixtures.models import FixtureDataType, FixtureTypeField
from corehq.apps.linked_domain.models import DomainLink
from corehq.apps.linked_domain.view_helpers import (
    build_app_view_model,
    build_fixture_view_model,
    build_keyword_view_model,
    build_report_view_model,
    get_apps,
    get_fixtures,
    get_keywords,
    get_reports,
)
from corehq.apps.sms.models import Keyword
from corehq.apps.userreports.dbaccessors import delete_all_report_configs
from corehq.apps.userreports.models import (
    DataSourceConfiguration,
    ReportConfiguration,
    ReportMeta,
)


def _create_report(domain, title="report", upstream_id=None, should_save=True):
    data_source = DataSourceConfiguration(
        domain=domain,
        table_id=uuid.uuid4().hex,
        referenced_doc_type='XFormInstance',
    )
    data_source.save()
    report = ReportConfiguration(
        domain=domain,
        config_id=data_source._id,
        title=title,
        report_meta=ReportMeta(created_by_builder=True, master_id=upstream_id),
    )
    if should_save:
        report.save()

    return report


def _create_keyword(domain, name="ping", upstream_id=None, should_save=True):
    keyword = Keyword(
        domain=domain,
        keyword=name,
        description="The description",
        override_open_sessions=True,
        upstream_id=upstream_id
    )
    if should_save:
        keyword.save()

    return keyword


def _create_fixture(domain, tag="table", should_save=True):
    data_type = FixtureDataType(
        domain=domain,
        tag=tag,
        fields=[
            FixtureTypeField(
                field_name="fixture_property",
                properties=["test"]
            )
        ],
        item_attributes=[],
        is_global=True
    )
    if should_save:
        data_type.save()

    return data_type


class TestGetDataModels(TestCase):
    file_path = ('data',)

    @classmethod
    def setUpClass(cls):
        super(TestGetDataModels, cls).setUpClass()
        cls.upstream_domain_obj = create_domain('upstream-domain')
        cls.upstream_domain = cls.upstream_domain_obj.name
        cls.downstream_domain_obj = create_domain('downstream-domain')
        cls.downstream_domain = cls.downstream_domain_obj.name

        cls.original_app = Application.new_app(cls.upstream_domain, "Original Application")
        cls.original_app.linked_whitelist = [cls.downstream_domain]
        cls.original_app.save()

        cls.linked_app = LinkedApplication.new_app(cls.downstream_domain, "Linked Application")
        cls.linked_app.upstream_app_id = cls.original_app._id
        cls.linked_app.save()

        cls.original_report = _create_report(cls.upstream_domain)
        cls.linked_report = _create_report(cls.downstream_domain, upstream_id=cls.original_report._id)

        cls.original_keyword = _create_keyword(cls.upstream_domain)
        cls.linked_keyword = _create_keyword(cls.downstream_domain, upstream_id=cls.original_keyword.id)

        cls.original_fixture = _create_fixture(cls.upstream_domain)

        cls.domain_link = DomainLink.link_domains(cls.downstream_domain, cls.upstream_domain)

    @classmethod
    def tearDownClass(cls):
        delete_all_report_configs()
        cls.original_fixture.delete()
        cls.original_keyword.delete()
        cls.linked_keyword.delete()
        cls.original_report.delete()
        cls.linked_report.delete()<commit_msg>support building view models for deleted data<commit_after>import uuid

from django.test.testcases import TestCase

from corehq.apps.app_manager.models import Application, LinkedApplication
from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.fixtures.models import FixtureDataType, FixtureTypeField
from corehq.apps.linked_domain.models import DomainLink
from corehq.apps.linked_domain.view_helpers import (
    build_app_view_model,
    build_fixture_view_model,
    build_keyword_view_model,
    build_report_view_model,
    get_apps,
    get_fixtures,
    get_keywords,
    get_reports,
)
from corehq.apps.sms.models import Keyword
from corehq.apps.userreports.dbaccessors import delete_all_report_configs
from corehq.apps.userreports.models import (
    DataSourceConfiguration,
    ReportConfiguration,
    ReportMeta,
)


def _create_report(domain, title="report", upstream_id=None, should_save=True):
    data_source = DataSourceConfiguration(
        domain=domain,
        table_id=uuid.uuid4().hex,
        referenced_doc_type='XFormInstance',
    )
    data_source.save()
    report = ReportConfiguration(
        domain=domain,
        config_id=data_source._id,
        title=title,
        report_meta=ReportMeta(created_by_builder=True, master_id=upstream_id),
    )
    if should_save:
        report.save()

    return report


def _create_keyword(domain, name="ping", upstream_id=None, should_save=True):
    keyword = Keyword(
        domain=domain,
        keyword=name,
        description="The description",
        override_open_sessions=True,
        upstream_id=upstream_id
    )
    if should_save:
        keyword.save()

    return keyword


def _create_fixture(domain, tag="table", should_save=True):
    data_type = FixtureDataType(
        domain=domain,
        tag=tag,
        fields=[
            FixtureTypeField(
                field_name="fixture_property",
                properties=["test"]
            )
        ],
        item_attributes=[],
        is_global=True
    )
    if should_save:
        data_type.save()

    return data_type


class TestGetDataModels(TestCase):
    file_path = ('data',)

    @classmethod
    def setUpClass(cls):
        super(TestGetDataModels, cls).setUpClass()
        cls.upstream_domain_obj = create_domain('upstream-domain')
        cls.upstream_domain = cls.upstream_domain_obj.name
        cls.downstream_domain_obj = create_domain('downstream-domain')
        cls.downstream_domain = cls.downstream_domain_obj.name

        cls.original_app = Application.new_app(cls.upstream_domain, "Original Application")
        cls.original_app.linked_whitelist = [cls.downstream_domain]
        cls.original_app.save()

        cls.linked_app = LinkedApplication.new_app(cls.downstream_domain, "Linked Application")
        cls.linked_app.family_id = cls.original_app._id
        cls.linked_app.save()

        cls.original_report = _create_report(cls.upstream_domain)
        cls.linked_report = _create_report(cls.downstream_domain, upstream_id=cls.original_report._id)

        cls.original_keyword = _create_keyword(cls.upstream_domain)
        cls.linked_keyword = _create_keyword(cls.downstream_domain, upstream_id=cls.original_keyword.id)

        cls.original_fixture = _create_fixture(cls.upstream_domain)

        cls.domain_link = DomainLink.link_domains(cls.downstream_domain, cls.upstream_domain)

    @classmethod
    def tearDownClass(cls):
        delete_all_report_configs()
        cls.original_fixture.delete()
        cls.original_keyword.delete()
        cls.linked_keyword.delete()
        cls.original_report.delete()
        cls.linked_report.delete()
{'fraction_non_alphanumeric': 0.08481532147742818, 'fraction_numerical': 0.00027359781121751026, 'mean_word_length': 3.3647761194029853, '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': '4621904', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2195, 'n_words': 333}