<commit_before>from django.test import SimpleTestCase

from corehq.apps.app_manager.const import (
    AUTO_SELECT_CASE,
    AUTO_SELECT_RAW,
    WORKFLOW_FORM,
    WORKFLOW_MODULE,
    WORKFLOW_PARENT_MODULE,
    WORKFLOW_PREVIOUS,
    WORKFLOW_ROOT,
)
from corehq.apps.app_manager.models import FormDatum, FormLink
from corehq.apps.app_manager.suite_xml.post_process.workflow import (
    CommandId,
    _replace_session_references_in_stack,
)
from corehq.apps.app_manager.suite_xml.xml_models import StackDatum
from corehq.apps.app_manager.tests.app_factory import AppFactory
from corehq.apps.app_manager.tests.util import TestXmlMixin, patch_get_xform_resource_overrides
from corehq.apps.app_manager.xpath import session_var


@patch_get_xform_resource_overrides()
class TestFormWorkflow(SimpleTestCase, TestXmlMixin):
    file_path = ('data', 'form_workflow')

    def test_basic_form(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')
        m1, m1f0 = factory.new_basic_module('m1', 'frog')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="(today() - dob) &lt; 7", form_id=m1f0.unique_id)
        ]
        self.assertXmlPartialEqual(self.get_xml('form_link_basic_form'), factory.app.create_suite(), "./entry[1]")

    def test_registration_module(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="(today() - dob) &lt; 7", module_unique_id=m0.unique_id)
        ]
        self.assertXmlPartialEqual(
            self.get_xml('form_link_registration_module'),
            factory.app.create_suite(),
            "./entry[1]"
        )

    def test_followup_module(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="(today() - dob) &lt; 7", module_unique_id=m0.unique_id)
        ]
        factory.form_requires_case(m0f0)
        self.assertXmlPartialEqual(
            self.get_xml('form_link_followup_module'),
            factory.app.create_suite(),
            "./entry[1]"
        )

    def test_with_case_management_both_update(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')
        factory.form_requires_case(m0f0)
        m1, m1f0 = factory.new_basic_module('m1', 'frog')
        factory.form_requires_case(m1f0)

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="(today() - dob) > 7", form_id=m1f0.unique_id)
        ]

        self.assertXmlPartialEqual(self.get_xml('form_link_update_case'), factory.app.create_suite(), "./entry[1]")

    def test_with_case_management_create_update(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')
        factory.form_opens_case(m0f0)
        m1, m1f0 = factory.new_basic_module('m1', 'frog')
        factory.form_requires_case(m1f0)

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath='true()', form_id=m1f0.unique_id)
        ]

        self.assertXmlPartialEqual(self.get_xml('form_link_create_update_case'), factory.app.create_suite(), "./entry[1]")

    def test_with_case_management_multiple_links(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')
        factory.form_opens_case(m0f0)
        m1, m1f0 = factory.new_basic_module('m1', 'frog')
        factory.form_requires_case(m1f0)

        m1f1 = factory.new_form(m1)
        factory.form_opens_case(m1f1)

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="a = 1", form_id=m1f0.unique_id),
            FormLink(xpath="a = 2", form_id=m1f1.unique_id)
        ]

        self.assertXmlPartialEqual(self.get_xml('form_link_multiple'), factory.app.create_suite(), "./entry[1]")

    def test_link_to_child_module(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('enroll child', 'child')
        factory.form_opens_case(m0f0)

        m1, m1f0 = factory.new_basic_module('child visit', 'child')
        factory.form_requires_case(m1f0)
        factory.form_opens_case(m1f0, case_type='visit', is_subcase=True)

        m2, m2f0 = factory.new_advanced_module('visit history', 'visit', parent_module=m1)
        factory.form_requires_case(m2f0, 'child')
        factory.form_requires_case(m2f0, 'visit', parent_case_type='child')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="true()", form_id=m1f0.unique_id),
        ]

        m1f0.post_form_workflow = WORKFLOW_FORM
        m1f0.form_links = [
            FormLink(xpath="true()", form_id=m2f0.unique_id),
        ]

        self.assertXmlPartialEqual(self.get_xml('form_link_tdh'), factory.app.create_suite(), "./entry")

    def test_manual_form_link(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('enroll child', 'child')
        factory.form_opens_case(m0f0)

        m1, m1f0 = factory.new_basic_module('child visit', 'child')
        factory.form_requires_case(m1f0)
        factory.form_opens_case(m1f0, case_type='visit', is_subcase=True)

        m2, m2f0 = factory.new_advanced_module('visit history', 'visit', parent_module=m1)
        factory.form_requires_case(m2f0, 'child')
        factory.form_requires_case(m2f0, 'visit', parent_case_type='child')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="true()", form_id=m1f0.unique_id, datums=[
                FormDatum(name='case_id', xpath="instance('commcaresession')/session/data/case_id_new_child_0")
            ]),
        ]

        m1f0.post_form_workflow = WORKFLOW_FORM
        m1f0.form_links = [
            FormLink(xpath="true()", form_id=m2f0.unique_id, datums=[
                FormDatum(name='case_id', xpath="instance('commcaresession')/session/data/case_id"),
                FormDatum(name='case_id_load_visit_0', xpath="instance('commcaresession')/session/data/case_id_new_visit_0"),
            ]),
        ]
<commit_msg>Added tests for links to child modules<commit_after>from django.test import SimpleTestCase

from corehq.apps.app_manager.const import (
    AUTO_SELECT_CASE,
    AUTO_SELECT_RAW,
    WORKFLOW_FORM,
    WORKFLOW_MODULE,
    WORKFLOW_PARENT_MODULE,
    WORKFLOW_PREVIOUS,
    WORKFLOW_ROOT,
)
from corehq.apps.app_manager.models import FormDatum, FormLink
from corehq.apps.app_manager.suite_xml.post_process.workflow import (
    CommandId,
    _replace_session_references_in_stack,
)
from corehq.apps.app_manager.suite_xml.xml_models import StackDatum
from corehq.apps.app_manager.tests.app_factory import AppFactory
from corehq.apps.app_manager.tests.util import TestXmlMixin, patch_get_xform_resource_overrides
from corehq.apps.app_manager.xpath import session_var


@patch_get_xform_resource_overrides()
class TestFormWorkflow(SimpleTestCase, TestXmlMixin):
    file_path = ('data', 'form_workflow')

    def test_basic_form(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')
        m1, m1f0 = factory.new_basic_module('m1', 'frog')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="(today() - dob) &lt; 7", form_id=m1f0.unique_id)
        ]
        self.assertXmlPartialEqual(self.get_xml('form_link_basic_form'), factory.app.create_suite(), "./entry[1]")

    def test_registration_module(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="(today() - dob) &lt; 7", module_unique_id=m0.unique_id)
        ]
        self.assertXmlPartialEqual(
            self.get_xml('form_link_registration_module'),
            factory.app.create_suite(),
            "./entry[1]"
        )

    def test_followup_module(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="(today() - dob) &lt; 7", module_unique_id=m0.unique_id)
        ]
        factory.form_requires_case(m0f0)
        self.assertXmlPartialEqual(
            self.get_xml('form_link_followup_module'),
            factory.app.create_suite(),
            "./entry[1]"
        )

    def test_with_case_management_both_update(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')
        factory.form_requires_case(m0f0)
        m1, m1f0 = factory.new_basic_module('m1', 'frog')
        factory.form_requires_case(m1f0)

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="(today() - dob) > 7", form_id=m1f0.unique_id)
        ]

        self.assertXmlPartialEqual(self.get_xml('form_link_update_case'), factory.app.create_suite(), "./entry[1]")

    def test_with_case_management_create_update(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')
        factory.form_opens_case(m0f0)
        m1, m1f0 = factory.new_basic_module('m1', 'frog')
        factory.form_requires_case(m1f0)

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath='true()', form_id=m1f0.unique_id)
        ]

        self.assertXmlPartialEqual(self.get_xml('form_link_create_update_case'), factory.app.create_suite(), "./entry[1]")

    def test_with_case_management_multiple_links(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('m0', 'frog')
        factory.form_opens_case(m0f0)
        m1, m1f0 = factory.new_basic_module('m1', 'frog')
        factory.form_requires_case(m1f0)

        m1f1 = factory.new_form(m1)
        factory.form_opens_case(m1f1)

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="a = 1", form_id=m1f0.unique_id),
            FormLink(xpath="a = 2", form_id=m1f1.unique_id)
        ]

        self.assertXmlPartialEqual(self.get_xml('form_link_multiple'), factory.app.create_suite(), "./entry[1]")

    def test_link_to_child_module_form(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('enroll child', 'child')
        factory.form_opens_case(m0f0)

        m1, m1f0 = factory.new_basic_module('child visit', 'child')
        factory.form_requires_case(m1f0)
        factory.form_opens_case(m1f0, case_type='visit', is_subcase=True)

        m2, m2f0 = factory.new_advanced_module('visit history', 'visit', parent_module=m1)
        factory.form_requires_case(m2f0, 'child')
        factory.form_requires_case(m2f0, 'visit', parent_case_type='child')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="true()", form_id=m1f0.unique_id),
        ]

        m1f0.post_form_workflow = WORKFLOW_FORM
        m1f0.form_links = [
            FormLink(xpath="true()", form_id=m2f0.unique_id),
        ]

        self.assertXmlPartialEqual(self.get_xml('form_link_tdh'), factory.app.create_suite(), "./entry")

    def test_link_to_module(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('Update Stripes', 'stripes')
        factory.form_requires_case(m0f0)
        m1, m1f0 = factory.new_basic_module('Update Solids', 'solids')
        factory.form_requires_case(m1f0)
        m2, m2f0 = factory.new_basic_module('Close Stripes', 'stripes')
        factory.form_requires_case(m2f0)

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [FormLink(xpath="true()", module_unique_id=m0.unique_id)]
        self.assertXmlPartialEqual("""
            <partial>
                <stack>
                  <create if="true()">
                    <command value="'m0'"/>
                  </create>
                </stack>
            </partial>
        """, factory.app.create_suite(), "./entry[1]/stack")

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [FormLink(xpath="true()", module_unique_id=m1.unique_id)]
        self.assertXmlPartialEqual("""
            <partial>
                <stack>
                  <create if="true()">
                    <command value="'m1'"/>
                  </create>
                </stack>
            </partial>
        """, factory.app.create_suite(), "./entry[1]/stack")

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [FormLink(xpath="true()", module_unique_id=m2.unique_id)]
        self.assertXmlPartialEqual("""
            <partial>
                <stack>
                  <create if="true()">
                    <command value="'m2'"/>
                  </create>
                </stack>
            </partial>
        """, factory.app.create_suite(), "./entry[1]/stack")

    def test_link_to_child_module(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('Register City', 'city')
        factory.form_opens_case(m0f0)
        m1, m1f0 = factory.new_basic_module('Update City', 'city')
        factory.form_requires_case(m1f0)
        m2, m2f0 = factory.new_basic_module('Update Person', 'person', parent_module=m1)
        factory.form_requires_case(m2f0)

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [FormLink(xpath="true()", module_unique_id=m2.unique_id)]
        self.assertXmlPartialEqual("""
            <partial>
                <stack>
                  <create if="true()">
                    <command value="'m1'"/>
                    <datum id="case_id" value="instance('commcaresession')/session/data/case_id"/>
                    <command value="'m2'"/>
                  </create>
                </stack>
            </partial>
        """, factory.app.create_suite(), "./entry[1]/stack")

    def test_link_between_child_modules(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('Update City', 'city')
        factory.form_requires_case(m0f0)
        m1, m1f0 = factory.new_basic_module('Update Person', 'person', parent_module=m0)
        factory.form_requires_case(m1f0)
        m2, m2f0 = factory.new_basic_module('Close City', 'city')
        factory.form_requires_case(m2f0)
        m3, m3f0 = factory.new_basic_module('Close Person', 'person', parent_module=m2)
        factory.form_requires_case(m3f0)

        m1f0.post_form_workflow = WORKFLOW_FORM
        m1f0.form_links = [FormLink(xpath="true()", module_unique_id=m3.unique_id)]
        self.assertXmlPartialEqual("""
            <partial>
                <stack>
                  <create if="true()">
                    <command value="'m2'"/>
                    <datum id="case_id" value="instance('commcaresession')/session/data/case_id"/>
                    <command value="'m3'"/>
                  </create>
                </stack>
            </partial>
        """, factory.app.create_suite(), "./entry[2]/stack")

    def test_link_from_child_module(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('Update City', 'city')
        factory.form_requires_case(m0f0)
        m1, m1f0 = factory.new_basic_module('Update Person', 'person', parent_module=m0)
        factory.form_requires_case(m1f0)
        m2, m2f0 = factory.new_basic_module('Update planet', 'planet')
        factory.form_requires_case(m2f0)

        m1f0.post_form_workflow = WORKFLOW_FORM
        m1f0.form_links = [FormLink(xpath="true()", module_unique_id=m2.unique_id)]
        self.assertXmlPartialEqual("""
            <partial>
                <stack>
                  <create if="true()">
                    <command value="'m2'"/>
                  </create>
                </stack>
            </partial>
        """, factory.app.create_suite(), "./entry[2]/stack")

    def test_manual_form_link(self, *args):
        factory = AppFactory(build_version='2.9.0')
        m0, m0f0 = factory.new_basic_module('enroll child', 'child')
        factory.form_opens_case(m0f0)

        m1, m1f0 = factory.new_basic_module('child visit', 'child')
        factory.form_requires_case(m1f0)
        factory.form_opens_case(m1f0, case_type='visit', is_subcase=True)

        m2, m2f0 = factory.new_advanced_module('visit history', 'visit', parent_module=m1)
        factory.form_requires_case(m2f0, 'child')
        factory.form_requires_case(m2f0, 'visit', parent_case_type='child')

        m0f0.post_form_workflow = WORKFLOW_FORM
        m0f0.form_links = [
            FormLink(xpath="true()", form_id=m1f0.unique_id, datums=[
                FormDatum(name='case_id', xpath="instance('commcaresession')/session/data/case_id_new_child_0")
            ]),
        ]

        m1f0.post_form_workflow = WORKFLOW_FORM
        m1f0.form_links = [
            FormLink(xpath="true()", form_id=m2f0.unique_id, datums=[
                FormDatum(name='case_id', xpath="instance('commcaresession')/session/data/case_id"),
                FormDatum(name='case_id_load_visit_0', xpath="instance('commcaresession')/session/data/case_id_new_visit_0"),
            ]),
        ]

{'fraction_non_alphanumeric': 0.12684578661041412, 'fraction_numerical': 0.028921949594759633, 'mean_word_length': 2.818355235269182, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '5930763', 'n_tokens_mistral': 6469, 'n_tokens_neox': 6240, 'n_words': 792}