Ejemplo n.º 1
0
    def test_subcase_multiple_repeats(self):
        self.form.actions.load_update_cases.append(
            LoadUpdateAction(
                case_type=self.parent_module.case_type,
                case_tag='load_1',
            ))
        self.form.actions.open_cases.append(
            AdvancedOpenCaseAction(
                case_type='child1',
                case_tag='open_1',
                name_update=ConditionalCaseUpdate(
                    question_path='/data/mother_name'),
                case_indices=[CaseIndex(tag='load_1')],
                repeat_context="/data/child",
            ))
        self.form.actions.open_cases[0].open_condition.type = 'if'
        self.form.actions.open_cases[
            0].open_condition.question = '/data/child/which_child'
        self.form.actions.open_cases[0].open_condition.answer = '1'

        self.form.actions.open_cases.append(
            AdvancedOpenCaseAction(
                case_type='child2',
                case_tag='open_2',
                name_update=ConditionalCaseUpdate(
                    question_path='/data/mother_name'),
                case_indices=[CaseIndex(tag='load_1')],
                repeat_context="/data/child",
            ))
        self.form.actions.open_cases[1].open_condition.type = 'if'
        self.form.actions.open_cases[
            1].open_condition.question = '/data/child/which_child'
        self.form.actions.open_cases[1].open_condition.answer = '2'
        self.assertXmlEqual(self.get_xml('subcase-repeat-multiple'),
                            self.form.render_xform())
Ejemplo n.º 2
0
 def test_get_casedb_schema_with_usercase(self):
     module, form = self.factory.new_basic_module('village', 'village')
     self.factory.form_uses_usercase(
         form,
         update={
             'name': ConditionalCaseUpdate(question_path='/data/username'),
             'role': ConditionalCaseUpdate(question_path='/data/userrole'),
         })
     with patch('corehq.apps.app_manager.app_schemas.casedb_schema.is_usercase_in_use') as mock1, \
             patch('corehq.apps.app_manager.app_schemas.case_properties.is_usercase_in_use') as mock2:
         mock1.return_value = True
         mock2.return_value = True
         schema = get_casedb_schema(form)
         subsets = {s["id"]: s for s in schema["subsets"]}
         self.assertDictEqual(
             subsets[USERCASE_TYPE], {
                 "id": USERCASE_TYPE,
                 "key": "@case_type",
                 "name": "user",
                 "structure": {
                     "name": {},
                     "role": {},
                     "first_name": {},
                     "last_name": {},
                     "username": {},
                     "phone_number": {},
                 },
             })
Ejemplo n.º 3
0
    def setUp(self):
        self.is_usercase_in_use_patch = patch('corehq.apps.app_manager.models.is_usercase_in_use')
        self.is_usercase_in_use_mock = self.is_usercase_in_use_patch.start()
        self.is_usercase_in_use_mock.return_value = True

        self.app = Application.new_app('domain', 'New App')
        self.module = self.app.add_module(AdvancedModule.new_module('Fish Module', None))
        self.module.case_type = 'fish'
        self.form = self.module.new_form('Form', 'en', self.get_xml('original').decode('utf-8'))
        self.other_module = self.app.add_module(AdvancedModule.new_module('Freshwater Module', lang='en'))
        self.other_module.case_type = 'freshwater'
        self.other_form = self.module.new_form('Other Form', 'en', self.get_xml('original').decode('utf-8'))
        self.case_index = CaseIndex(
            reference_id='host',
            relationship='extension',
        )
        self.subcase = AdvancedOpenCaseAction(
            case_tag='open_freshwater_0',
            case_type='freshwater',
            case_name='Wanda',
            name_update=ConditionalCaseUpdate(question_path='/data/question1'),
            open_condition=FormActionCondition(type='always'),
            case_properties={'name': ConditionalCaseUpdate(question_path='/data/question1')},
            case_indices=[self.case_index],
        )
        self.form.actions.open_cases.append(self.subcase)
        self.xform = XForm(self.get_xml('original'))
        path = 'subcase_0/'
        self.subcase_block = XFormCaseBlock(self.xform, path)
Ejemplo n.º 4
0
 def test_ext_case_update_host_case(self):
     """
     A extension case should be able to save host case properties
     """
     self.freshwater_form.requires = 'case'
     self.freshwater_form.actions.update_case = UpdateCaseAction(update={
         'question1': ConditionalCaseUpdate(question_path='/data/question1'),
         'host/question1': ConditionalCaseUpdate(question_path='/data/question1'),
     })
     self.freshwater_form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('update_host_case'), self.freshwater_form.render_xform())
Ejemplo n.º 5
0
 def test_update_parent_case(self):
     self.form.requires = 'case'
     self.form.actions.update_case = UpdateCaseAction(
         update={
             'question1':
             ConditionalCaseUpdate(question_path='/data/question1'),
             'parent/question1':
             ConditionalCaseUpdate(question_path='/data/question1'),
         })
     self.form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('update_parent_case'),
                         self.form.render_xform())
Ejemplo n.º 6
0
 def test_ext_case_update_host_case(self):
     """
     A extension case should be able to save host case properties in an advanced module
     """
     self.form.actions.load_update_cases.append(LoadUpdateAction(
         case_type=self.module.case_type,
         case_tag='load_1',
         case_properties={
             'question1': ConditionalCaseUpdate(question_path='/data/question1'),
             'host/question1': ConditionalCaseUpdate(question_path='/data/question1')
         },
     ))
     self.assertXmlEqual(self.get_xml('update_host_case_adv'), self.form.render_xform())
Ejemplo n.º 7
0
 def test_open_case(self):
     self.form.actions.open_case = OpenCaseAction(
         name_update=ConditionalCaseUpdate(
             question_path="/data/question1"), )
     self.form.actions.open_case.condition.type = 'always'
     self.form.actions.update_case = UpdateCaseAction(
         update={
             'question1': ConditionalCaseUpdate(
                 question_path='/data/question1')
         })
     self.form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('multi_open_update_case'),
                         self.form.render_xform())
Ejemplo n.º 8
0
 def test_update_parent_case(self):
     self.form.actions.load_update_cases.append(
         LoadUpdateAction(
             case_type=self.module.case_type,
             case_tag='load_1',
             case_properties={
                 'question1':
                 ConditionalCaseUpdate(question_path='/data/question1'),
                 'parent/question1':
                 ConditionalCaseUpdate(question_path='/data/question1')
             }))
     self.assertXmlEqual(self.get_xml('update_parent_case'),
                         self.form.render_xform())
Ejemplo n.º 9
0
 def test_open_case(self):
     """Opening cases still works with registry search"""
     self.form.actions.open_case = OpenCaseAction(
         name_update=ConditionalCaseUpdate(question_path="/data/question1"),
         external_id=None)
     self.form.actions.open_case.condition.type = 'always'
     self.form.actions.update_case = UpdateCaseAction(
         update={
             'question1': ConditionalCaseUpdate(
                 question_path='/data/question1')
         })
     self.form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('open_update_case'),
                         self.form.render_xform())
Ejemplo n.º 10
0
 def test_open_update_case(self):
     self.form.actions.open_cases.append(
         AdvancedOpenCaseAction(
             case_type=self.module.case_type,
             case_tag='open_1',
             name_update=ConditionalCaseUpdate(
                 question_path="/data/question1"),
             case_properties={
                 'question1':
                 ConditionalCaseUpdate(question_path='/data/question1')
             }))
     self.form.actions.open_cases[0].open_condition.type = 'always'
     self.assertXmlEqual(self.get_xml('open_update_case'),
                         self.form.render_xform())
Ejemplo n.º 11
0
 def test_open_case_external_id(self):
     self.form.actions.open_case = OpenCaseAction(
         name_update=ConditionalCaseUpdate(question_path="/data/question1"),
         external_id='/data/question1')
     self.form.actions.open_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('open_case_external_id'),
                         self.form.render_xform())
Ejemplo n.º 12
0
    def setUp(self):
        self.app = Application.new_app('domain', 'New App')
        self.app.version = 3
        self.parent_module = self.app.add_module(
            Module.new_module('New Module', lang='en'))
        self.parent_form = self.app.new_form(0, 'New Form', lang='en')
        self.parent_module.case_type = 'parent_test_case_type'
        self.parent_form.source = self.get_xml(
            'original_form', override_path=('data', )).decode('utf-8')
        self.parent_form.actions.open_case = OpenCaseAction(
            name_update=ConditionalCaseUpdate(question_path="/data/question1"),
            external_id=None)
        self.parent_form.actions.open_case.condition.type = 'always'

        self.module = self.app.add_module(
            AdvancedModule.new_module('New Module', lang='en'))
        form = AdvancedForm(name={"en": "Untitled Form"})
        self.module.forms.append(form)
        self.form = self.module.get_form(-1)
        self.module.case_type = 'test_case_type'
        self.form.source = self.get_xml('subcase_original').decode('utf-8')

        child_module_1 = self.app.add_module(
            Module.new_module('New Module', lang='en'))
        child_module_1.case_type = 'child1'
        child_module_2 = self.app.add_module(
            Module.new_module('New Module', lang='en'))
        child_module_2.case_type = 'child2'
        self.is_usercase_in_use_patch = patch(
            'corehq.apps.app_manager.models.is_usercase_in_use')
        self.is_usercase_in_use_mock = self.is_usercase_in_use_patch.start()
Ejemplo n.º 13
0
    def test_repeat_subcases_schema_generation(self):
        module = Module(case_type='child', _parent=Application())
        form = Form().with_id(0, module)
        form.actions.subcases = [
            OpenSubCaseAction(
                repeat_context='/data/repeat',
                case_properties={
                    'weight':
                    ConditionalCaseUpdate(
                        question_path='/data/repeat/group/weight'),
                },
                subcase_index=0,
                _nest=True).with_id(0, None),
            OpenSubCaseAction(
                repeat_context='/data/repeat',
                case_properties={
                    'height':
                    ConditionalCaseUpdate(question_path='/data/repeat/height'),
                },
                subcase_index=1,
                _nest=True).with_id(1, None),
            OpenSubCaseAction(
                repeat_context='/data/repeat/nested_repeat',
                case_properties={
                    'age':
                    ConditionalCaseUpdate(
                        question_path='/data/repeat/nested_repeat/age'),
                },
                subcase_index=2,
                _nest=False).with_id(2, None),
        ]

        schema = FormExportDataSchema._add_export_items_for_cases(
            ExportGroupSchema(path=MAIN_TABLE),
            [form],
            ['/data/repeat', '/data/nested_repeat'],
        )[0]

        self.assertEqual(len(schema.group_schemas), len(form.actions.subcases))
        for group_schema, action in zip(schema.group_schemas,
                                        form.actions.subcases):
            base_path = 'form.{}'.format(action.repeat_context[6:].replace(
                '/', '.'))
            if action._nest:
                base_path += '.{}'.format(action.form_element_name)
            self._check_subcase_repeat_group_schema(
                group_schema, list(action.case_properties), base_path)
Ejemplo n.º 14
0
    def test_child_module_adjusted_datums_basic_module(self):
        """
        Testing that the session variable name for the case_id is correct since
        it will have been adjusted in the suite.xml to match the variable name
        in the root module.
        """
        module = self.app.add_module(Module.new_module('New Module',
                                                       lang='en'))
        module.case_type = 'guppy'
        form = module.new_form(
            "Untitled Form", "en",
            self.get_xml('original_form',
                         override_path=('data', )).decode('utf-8'))

        form.requires = 'case'
        form.actions.update_case = UpdateCaseAction(
            update={
                'question1': ConditionalCaseUpdate(
                    question_path='/data/question1')
            })
        form.actions.update_case.condition.type = 'always'

        root_module = self.app.add_module(
            Module.new_module('root module', None))
        root_module.unique_id = 'm_root'
        root_module.case_type = 'test_case_type'

        root_module_form = root_module.new_form('root module form', None)
        root_module_form.requires = 'case'
        root_module_form.actions.update_case = UpdateCaseAction(
            update={
                'question1': ConditionalCaseUpdate(
                    question_path='/data/question1')
            })
        root_module_form.actions.update_case.condition.type = 'always'

        # make module a child module of root_module
        module.root_module_id = root_module.unique_id

        module.parent_select.active = True
        module.parent_select.module_id = root_module.unique_id

        self.assertXmlEqual(
            self.get_xml('child_module_adjusted_case_id_basic'),
            form.render_xform())
Ejemplo n.º 15
0
    def test_registration_form_subcase_multiple(self):
        self.form.actions.load_update_cases.append(
            LoadUpdateAction(case_type="parent", case_tag="parent"))
        self.form.actions.open_cases = [
            AdvancedOpenCaseAction(case_tag="child",
                                   case_type="child",
                                   name_update=ConditionalCaseUpdate(
                                       question_path="/data/question1"),
                                   case_indices=[CaseIndex(tag="parent")]),
            AdvancedOpenCaseAction(
                case_tag="grandchild",
                case_type="grandchild",
                name_update=ConditionalCaseUpdate(
                    question_path="/data/children/question1"),
                case_indices=[CaseIndex(tag="child")])
        ]

        self.assertFalse(self.form.is_registration_form())
Ejemplo n.º 16
0
 def test_open_subcase_action_default_relationship(self):
     """
     OpenSubCaseAction relationship should default to "child"
     """
     action = OpenSubCaseAction(
         case_type='mother',
         name_update=ConditionalCaseUpdate(question_path='Eva'),
     )
     self.assertEqual(action.relationship, 'child')
Ejemplo n.º 17
0
    def test_case_properties(self):
        app = Application.new_app('domain', 'New App')
        app._id = uuid.uuid4().hex
        app.version = 1
        m0 = self._make_module(app, 0, 'normal_module')
        m0f1 = m0.new_form(
            'update case',
            'en',
            attachment=self.get_xml('standard_questions').decode('utf-8'))
        self._assert_properties(app.get_case_metadata(), {'name'})

        m0f1.actions.update_case.condition.type = 'always'
        m0f1.actions.update_case.update = {
            "p1": ConditionalCaseUpdate(question_path="/data/question1"),
            "p2": ConditionalCaseUpdate(question_path="/data/question2")
        }
        app.version = 2
        self._assert_properties(app.get_case_metadata(), {'name', 'p1', 'p2'})
Ejemplo n.º 18
0
 def test_open_subcase_action_supports_relationship(self):
     """
     OpenSubCaseAction should allow relationship to be set
     """
     action = OpenSubCaseAction(
         case_type='mother',
         name_update=ConditionalCaseUpdate(question_path='Eva'),
         relationship='extension',
     )
     self.assertEqual(action.relationship, 'extension')
Ejemplo n.º 19
0
 def test_update_case(self):
     self.factory.form_requires_case(self.form, 'person')
     self.form.actions.update_case = UpdateCaseAction(
         update={
             'question1': ConditionalCaseUpdate(
                 question_path='/data/question1')
         })
     self.form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('multi_no_actions'),
                         self.form.render_xform())
Ejemplo n.º 20
0
 def test_case_properties_advanced(self):
     app = Application.new_app('domain', 'New App')
     app._id = uuid.uuid4().hex
     app.version = 1
     m0 = app.add_module(AdvancedModule.new_module('Module0', lang='en'))
     m0.case_type = 'child'
     m0f0 = m0.new_form(
         'other form',
         'en',
         attachment=self.get_xml('standard_questions').decode('utf-8'))
     m0f0.actions.load_update_cases.append(
         LoadUpdateAction(
             case_type='parent',
             case_tag='parent',
             case_properties={
                 'case_name':
                 ConditionalCaseUpdate(question_path='/data/question1'),
                 'other':
                 ConditionalCaseUpdate(question_path='/data/question2')
             }))
     m0f0.actions.open_cases.append(
         AdvancedOpenCaseAction(
             name_update=ConditionalCaseUpdate(
                 question_path='/data/question1'),
             case_type='child',
             case_indices=[CaseIndex(tag='parent', reference_id='father')],
             case_properties={
                 'child_other':
                 ConditionalCaseUpdate(question_path='/data/question2')
             }))
     m0f0.actions.open_cases[0].open_condition.type = 'always'
     meta = app.get_case_metadata()
     self.assertEqual(2, len(meta.case_types))
     child_type = meta.get_type('child')
     parent_type = meta.get_type('parent')
     self.assertFalse(child_type.has_errors)
     self.assertFalse(parent_type.has_errors)
     self.assertEqual({'name', 'child_other'},
                      {p.name
                       for p in child_type.properties})
     self.assertEqual({'case_name', 'other'},
                      {p.name
                       for p in parent_type.properties})
Ejemplo n.º 21
0
    def test_subcase_repeat_mixed_form(self):
        app = Application.new_app(None, "Untitled Application")
        module_0 = app.add_module(Module.new_module('parent', None))
        module_0.unique_id = 'm0'
        module_0.case_type = 'parent'
        form = app.new_form(
            0,
            "Form",
            None,
            attachment=self.get_xml('subcase_repeat_mixed_form_pre').decode(
                'utf-8'))

        module_1 = app.add_module(Module.new_module('subcase', None))
        module_1.unique_id = 'm1'
        module_1.case_type = 'subcase'

        form.actions.open_case = OpenCaseAction(
            name_update=ConditionalCaseUpdate(
                question_path="/data/parent_name"))
        form.actions.open_case.condition.type = 'always'

        form.actions.subcases.append(
            OpenSubCaseAction(case_type=module_1.case_type,
                              name_update=ConditionalCaseUpdate(
                                  question_path="/data/first_child_name"),
                              condition=FormActionCondition(type='always')))
        # subcase in the middle that has a repeat context
        form.actions.subcases.append(
            OpenSubCaseAction(
                case_type=module_1.case_type,
                name_update=ConditionalCaseUpdate(
                    question_path="/data/repeat_child/repeat_child_name"),
                repeat_context='/data/repeat_child',
                condition=FormActionCondition(type='always')))
        form.actions.subcases.append(
            OpenSubCaseAction(case_type=module_1.case_type,
                              name_update=ConditionalCaseUpdate(
                                  question_path="/data/last_child_name"),
                              condition=FormActionCondition(type='always')))

        self.assertXmlEqual(self.get_xml('subcase_repeat_mixed_form_post'),
                            app.get_module(0).get_form(0).render_xform())
Ejemplo n.º 22
0
    def test_open_case_and_subcase(self, *args):
        app = Application.new_app('domain', "Untitled Application")

        module = app.add_module(Module.new_module('parent', None))
        module.case_type = 'phone'
        module.unique_id = 'm0'

        form = app.new_form(0, "Untitled Form", None)
        form.xmlns = 'http://m0-f0'
        form.actions.open_case = OpenCaseAction(
            name_update=ConditionalCaseUpdate(question_path="/data/question1"))
        form.actions.open_case.condition.type = 'always'
        form.actions.subcases.append(
            OpenSubCaseAction(case_type='tablet',
                              name_update=ConditionalCaseUpdate(
                                  question_path="/data/question1"),
                              condition=FormActionCondition(type='always')))

        self.assertXmlPartialEqual(self.get_xml('open_case_and_subcase'),
                                   app.create_suite(), "./entry[1]")
Ejemplo n.º 23
0
    def test_case_list_form_basic(self):
        self._add_module_and_form(Module)

        self.form.actions.open_case = OpenCaseAction(
            name_update=ConditionalCaseUpdate(question_path="/data/question1"),
            external_id=None)
        self.form.actions.open_case.condition.type = 'always'
        self.module.case_list_form.form_id = self.form.get_unique_id()

        self.assertXmlEqual(self.get_xml('case_list_form_basic_form'),
                            self.form.render_xform())
Ejemplo n.º 24
0
 def test_update_case(self):
     """Case updates are not permitted"""
     self.form.requires = 'case'
     self.form.actions.update_case = UpdateCaseAction(
         update={
             'question1': ConditionalCaseUpdate(
                 question_path='/data/question1')
         })
     self.form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('no_actions'),
                         self.form.render_xform())
Ejemplo n.º 25
0
    def test_registration_form_simple(self):
        self.form.actions.open_cases = [
            AdvancedOpenCaseAction(
                case_tag="phone",
                case_type="phone",
                name_update=ConditionalCaseUpdate(
                    question_path="/data/question1"),
            )
        ]

        self.assertTrue(self.form.is_registration_form())
Ejemplo n.º 26
0
    def test_subcase_of_open(self):
        self.form.actions.open_cases.append(
            AdvancedOpenCaseAction(
                case_type=self.parent_module.case_type,
                case_tag='open_1',
                name_update=ConditionalCaseUpdate(
                    question_path='/data/mother_name'),
            ))

        self.form.actions.open_cases.append(
            AdvancedOpenCaseAction(case_type=self.module.case_type,
                                   case_tag='open_2',
                                   name_update=ConditionalCaseUpdate(
                                       question_path='/data/mother_name'),
                                   case_indices=[CaseIndex(tag='open_1')],
                                   repeat_context="/data/child"))
        for action in self.form.actions.open_cases:
            action.open_condition.type = 'always'
        self.assertXmlEqual(self.get_xml('subcase-open'),
                            self.form.render_xform())
Ejemplo n.º 27
0
 def test_update_attachment(self):
     self.form.requires = 'case'
     self.form.source = self.get_xml('attachment').decode('utf-8')
     self.form.actions.update_case = UpdateCaseAction(
         update={
             'photo': ConditionalCaseUpdate(
                 question_path='/data/thepicture')
         })
     self.form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('update_attachment_case'),
                         self.form.render_xform())
Ejemplo n.º 28
0
    def test_subcase_repeat_mixed(self, *args):
        app = Application.new_app(None, "Untitled Application")
        module_0 = app.add_module(Module.new_module('parent', None))
        module_0.unique_id = 'm0'
        module_0.case_type = 'parent'
        form = app.new_form(0, "Form", None)

        form.actions.open_case = OpenCaseAction(
            name_update=ConditionalCaseUpdate(question_path="/data/question1"))
        form.actions.open_case.condition.type = 'always'

        child_case_type = 'child'
        form.actions.subcases.append(
            OpenSubCaseAction(case_type=child_case_type,
                              name_update=ConditionalCaseUpdate(
                                  question_path="/data/question1"),
                              condition=FormActionCondition(type='always')))
        # subcase in the middle that has a repeat context
        form.actions.subcases.append(
            OpenSubCaseAction(case_type=child_case_type,
                              name_update=ConditionalCaseUpdate(
                                  question_path="/data/repeat/question1"),
                              repeat_context='/data/repeat',
                              condition=FormActionCondition(type='always')))
        form.actions.subcases.append(
            OpenSubCaseAction(case_type=child_case_type,
                              name_update=ConditionalCaseUpdate(
                                  question_path="/data/question1"),
                              condition=FormActionCondition(type='always')))

        expected = """
        <partial>
            <session>
              <datum id="case_id_new_parent_0" function="uuid()"/>
              <datum id="case_id_new_child_1" function="uuid()"/>
              <datum id="case_id_new_child_3" function="uuid()"/>
            </session>
        </partial>
        """
        self.assertXmlPartialEqual(expected, app.create_suite(),
                                   './entry[1]/session')
Ejemplo n.º 29
0
 def test_update_attachment(self):
     self.form.source = self.get_xml('attachment').decode('utf-8')
     self.form.actions.load_update_cases.append(
         LoadUpdateAction(
             case_type=self.module.case_type,
             case_tag='load_1',
             case_properties={
                 'photo':
                 ConditionalCaseUpdate(question_path='/data/thepicture')
             }))
     self.assertXmlEqual(self.get_xml('update_attachment_case'),
                         self.form.render_xform())
Ejemplo n.º 30
0
 def test_subcase(self):
     self.form.actions.load_update_cases.append(
         LoadUpdateAction(
             case_type=self.parent_module.case_type,
             case_tag='load_1',
         ))
     self.form.actions.open_cases.append(
         AdvancedOpenCaseAction(case_type=self.module.case_type,
                                case_tag='open_1',
                                name_update=ConditionalCaseUpdate(
                                    question_path='/data/mother_name'),
                                case_indices=[CaseIndex(tag='load_1')]))
     self.form.actions.open_cases[0].open_condition.type = 'always'
     self.assertXmlEqual(self.get_xml('subcase'), self.form.render_xform())