def test_xform_parsing_with_repeat_group(self):
        form_xml = self.get_xml('repeat_group_form')

        schema = FormExportDataSchema._generate_schema_from_xform(
            XForm(form_xml), [], ['en'], self.app_id, 1)

        self.assertEqual(len(schema.group_schemas), 2)

        group_schema = schema.group_schemas[0]
        self.assertEqual(len(group_schema.items), 2)
        self.assertEqual(group_schema.path, MAIN_TABLE)

        form_items = filter(lambda item: item.tag is None, group_schema.items)
        self.assertEqual(form_items[0].path,
                         [PathNode(name='form'),
                          PathNode(name='question1')])
        self.assertEqual(
            form_items[1].path,
            [PathNode(name='form'),
             PathNode(name='zendquestion')])

        group_schema = schema.group_schemas[1]
        self.assertEqual(len(group_schema.items), 1)
        self.assertEqual(group_schema.path, [
            PathNode(name='form'),
            PathNode(name='question3', is_repeat=True)
        ])
        self.assertEqual(group_schema.items[0].path, [
            PathNode(name='form'),
            PathNode(name='question3', is_repeat=True),
            PathNode(name='question4')
        ])
    def _get_merged_schema(self, form_name1, form_name2):
        form_xml = self.get_xml(form_name1)
        form_xml2 = self.get_xml(form_name2)
        schema = FormExportDataSchema._generate_schema_from_xform(
            XForm(form_xml),
            ['en'],
            self.app_id,
            1
        )
        schema2 = FormExportDataSchema._generate_schema_from_xform(
            XForm(form_xml2),
            ['en'],
            self.app_id,
            2
        )

        return FormExportDataSchema._merge_schemas(schema, schema2)
Beispiel #3
0
    def test_repeat_subcases_schema_generation(self):
        form_xml = self.get_xml('nested_repeat_form')
        repeats_with_subcases = [
            OpenSubCaseAction(repeat_context='/data/repeat',
                              case_properties={
                                  'weight': '/data/repeat/group/weight',
                              }),
            OpenSubCaseAction(repeat_context='/data/repeat/nested_repeat',
                              case_properties={
                                  'age': '/data/repeat/nested_repeat/age',
                              }),
        ]

        schema = FormExportDataSchema._generate_schema_from_repeat_subcases(
            XForm(form_xml),
            repeats_with_subcases,
            ['en'],
            self.app_id,
            1,
        )

        self.assertEqual(len(schema.group_schemas), 2)

        group_schema = schema.group_schemas[0]
        attribute_items = filter(
            lambda item: item.path[-1].name in CASE_ATTRIBUTES,
            group_schema.items)

        self.assertEqual(len(attribute_items), len(CASE_ATTRIBUTES))
        self.assertTrue(
            all(
                map(
                    lambda item: item.readable_path.startswith(
                        'form.repeat.case'),
                    attribute_items,
                )))

        create_items = filter(
            lambda item: item.path[-1].name in CASE_CREATE_ELEMENTS,
            group_schema.items)
        self.assertEqual(len(create_items), len(CASE_CREATE_ELEMENTS))
        self.assertTrue(
            all(
                map(
                    lambda item: item.readable_path.startswith(
                        'form.repeat.case.create'),
                    create_items,
                )))

        update_items = list(
            set(group_schema.items) - set(create_items) - set(attribute_items))
        self.assertEqual(len(update_items), 1)
        self.assertEqual(update_items[0].readable_path,
                         'form.repeat.case.update.group.weight')
    def test_labels_in_xform(self):
        form_xml = self.get_xml('form_with_labels')

        schema = FormExportDataSchema._generate_schema_from_xform(
            XForm(form_xml), ['en'], self.app_id, 1)

        self.assertEqual(len(schema.group_schemas), 1)

        group_schema = schema.group_schemas[0]

        self.assertEqual(len(group_schema.items), 1)
        self.assertEqual(group_schema.items[0].path,
                         [PathNode(name='form'),
                          PathNode(name='label')])
        self.assertIsInstance(group_schema.items[0], LabelItem)
    def test_basic_xform_parsing(self):
        form_xml = self.get_xml('basic_form')

        schema = FormExportDataSchema._generate_schema_from_xform(
            XForm(form_xml), ['en'], self.app_id, 1)

        self.assertEqual(len(schema.group_schemas), 1)

        group_schema = schema.group_schemas[0]

        self.assertEqual(len(group_schema.items), 2)

        form_items = [item for item in group_schema.items if item.tag is None]
        self.assertEqual(form_items[0].path,
                         [PathNode(name='form'),
                          PathNode(name='question1')])
        self.assertEqual(form_items[1].path,
                         [PathNode(name='form'),
                          PathNode(name='question2')])
Beispiel #6
0
    def test_xform_parsing_with_multiple_choice(self):
        form_xml = self.get_xml('multiple_choice_form')
        schema = FormExportDataSchema._generate_schema_from_xform(
            XForm(form_xml), [], ['en'], self.app_id, 1)

        self.assertEqual(len(schema.group_schemas), 1)
        group_schema = schema.group_schemas[0]

        self.assertEqual(len(group_schema.items), 2)
        form_items = filter(lambda item: item.tag is None, group_schema.items)
        self.assertEqual(form_items[0].path,
                         [PathNode(name='form'),
                          PathNode(name='question1')])

        self.assertEqual(form_items[1].path,
                         [PathNode(name='form'),
                          PathNode(name='question2')])
        self.assertEqual(form_items[1].options[0].value, 'choice1')
        self.assertEqual(form_items[1].options[1].value, 'choice2')
Beispiel #7
0
    def test_xform_parsing_with_stock_questions(self):
        form_xml = self.get_xml('stock_form')
        schema = FormExportDataSchema._generate_schema_from_xform(
            XForm(form_xml), [], ['en'], self.app_id, 1)
        self.assertEqual(len(schema.group_schemas), 1)
        group_schema = schema.group_schemas[0]

        self.assertEqual(len(group_schema.items), 6)
        self.assertTrue(
            all(
                map(lambda item: item.doc_type == 'StockItem',
                    group_schema.items)))
        for parent_attr in ['@type', '@entity-id', '@date', '@section-id']:
            self.assertTrue(
                any(
                    map(
                        lambda item: item.path == [
                            PathNode(name='form'),
                            PathNode(name='balance:balance_one'),
                            PathNode(name=parent_attr),
                        ],
                        group_schema.items,
                    )))

        for entry_attr in ['@id', '@quantity']:
            self.assertTrue(
                any(
                    map(
                        lambda item: item.path == [
                            PathNode(name='form'),
                            PathNode(name='balance:balance_one'),
                            PathNode(name='entry'),
                            PathNode(name=entry_attr),
                        ],
                        group_schema.items,
                    )))
    def test_repeat_subcases_schema_generation(self):
        form_xml = self.get_xml('nested_repeat_form')
        repeats_with_subcases = [
            OpenSubCaseAction(
                repeat_context='/data/repeat',
                case_properties={
                    'weight': '/data/repeat/group/weight',
                },
                subcase_index=0,
                nest=False,
            ).with_id(0, None),
            OpenSubCaseAction(
                repeat_context='/data/repeat/nested_repeat',
                case_properties={
                    'age': '/data/repeat/nested_repeat/age',
                },
                subcase_index=1,
                nest=False,  # would normally get added by caller
            ).with_id(1, None),
        ]

        schema = FormExportDataSchema._generate_schema_from_repeat_subcases(
            XForm(form_xml),
            repeats_with_subcases,
            ['en'],
            self.app_id,
            1,
        )

        self.assertEqual(len(schema.group_schemas), 2)

        group_schema = schema.group_schemas[0]
        attribute_items = [
            item for item in group_schema.items
            if item.path[-1].name in CASE_ATTRIBUTES
        ]

        self.assertEqual(len(attribute_items), len(CASE_ATTRIBUTES))
        self.assertTrue(
            all(
                map(
                    lambda item: item.readable_path.startswith(
                        'form.repeat.case'),
                    attribute_items,
                )))

        create_items = [
            item for item in group_schema.items
            if item.path[-1].name in CASE_CREATE_ELEMENTS
        ]
        self.assertEqual(len(create_items), len(CASE_CREATE_ELEMENTS))
        self.assertTrue(
            all(
                map(
                    lambda item: item.readable_path.startswith(
                        'form.repeat.case.create'),
                    create_items,
                )))

        index_items = [
            item for item in group_schema.items
            if 'case.index.parent' in item.readable_path
        ]
        self.assertEqual(len(index_items), 2)

        update_items = list(
            set(group_schema.items) - set(create_items) -
            set(attribute_items) - set(index_items))
        self.assertEqual(len(update_items), 1)
        self.assertEqual(update_items[0].readable_path,
                         'form.repeat.case.update.group.weight')