def test_empty_group(self):
     table_configuration = TableConfiguration(
         path=[
             PathNode(name="form", is_repeat=False),
             PathNode(name="group", is_repeat=False),
             PathNode(name="repeat1", is_repeat=True)
         ],
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=[
                         PathNode(name="form"),
                         PathNode(name="group"),
                         PathNode(name="repeat1", is_repeat=True),
                         PathNode(name="q1")
                     ],
                 ),
                 selected=True,
             ),
         ]
     )
     submission = {
         'domain': 'my-domain',
         '_id': '1234',
         'form': {
             'group': ''
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission, 0)], []
     )
 def test_repeat(self):
     table_configuration = TableConfiguration(
         path=[PathNode(name="form", is_repeat=False), PathNode(name="repeat1", is_repeat=True)],
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=[
                         PathNode(name="form"),
                         PathNode(name="repeat1", is_repeat=True),
                         PathNode(name="q1")
                     ],
                 ),
                 selected=True,
             ),
         ]
     )
     submission = {
         'domain': 'my-domain',
         '_id': '1234',
         'form': {
             'repeat1': [
                 {'q1': 'foo'},
                 {'q1': 'bar'}
             ]
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission, 0)],
         [ExportRow(['foo']).data, ExportRow(['bar']).data]
     )
 def test_simple(self):
     table_configuration = TableConfiguration(
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=['form', 'q3'],
                 )
             ),
             ExportColumn(
                 item=ScalarItem(
                     path=['form', 'q1'],
                 )
             ),
         ]
     )
     submission = {
         "form": {
             "q1": "foo",
             "q2": "bar",
             "q3": "baz"
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission)],
         [['baz', 'foo']]
     )
 def test_nested_repeat(self):
     table = TableConfiguration(
         path=[PathNode(name='foo', is_repeat=True), PathNode(name='bar', is_repeat=True)],
     )
     self.assertEqual(
         table._get_sub_documents(
             {
                 'foo': [
                     {
                         'bar': [
                             {'baz': 'a'},
                             {'baz': 'b'}
                         ],
                     },
                     {
                         'bar': [
                             {'baz': 'c'}
                         ],
                     },
                 ],
             },
             0
         ),
         [
             DocRow(row=(0, 0, 0), doc={'baz': 'a'}),
             DocRow(row=(0, 0, 1), doc={'baz': 'b'}),
             DocRow(row=(0, 1, 0), doc={'baz': 'c'}),
         ]
     )
 def test_repeat(self):
     table_configuration = TableConfiguration(
         path=[PathNode(name="form", is_repeat=False), PathNode(name="repeat1", is_repeat=True)],
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=[
                         PathNode(name="form"),
                         PathNode(name="repeat1", is_repeat=True),
                         PathNode(name="q1")
                     ],
                 ),
                 selected=True,
             ),
         ]
     )
     submission = {
         'domain': 'my-domain',
         '_id': '1234',
         'form': {
             'repeat1': [
                 {'q1': 'foo'},
                 {'q1': 'bar'}
             ]
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission, 0)],
         [ExportRow(['foo']).data, ExportRow(['bar']).data]
     )
Esempio n. 6
0
 def test_simple(self):
     table_configuration = TableConfiguration(
         path=[],
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=[PathNode(name='form'),
                           PathNode(name='q3')], ),
                 selected=True,
             ),
             ExportColumn(
                 item=ScalarItem(
                     path=[PathNode(name='form'),
                           PathNode(name='q1')], ),
                 selected=True,
             ),
             ExportColumn(
                 item=ScalarItem(
                     path=[PathNode(name='form'),
                           PathNode(name='q2')], ),
                 selected=False,
             ),
         ])
     submission = {
         'domain': 'my-domain',
         '_id': '1234',
         "form": {
             "q1": "foo",
             "q2": "bar",
             "q3": "baz"
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission, 0)],
         [['baz', 'foo']])
 def test_split_columns(self):
     # TODO: It probably makes more sense to test columns independently...
     # I'm assuming they will have some sort of get_value(document) property
     table_configuration = TableConfiguration(
         path=['form'],
         columns=[
             SplitExportColumn(
                 item=MultipleChoiceItem(
                     path=['form', 'q1'],
                     options=[Option(value='a'), Option(value='c')]
                 ),
                 ignore_extras=True
             ),
             SplitExportColumn(
                 item=MultipleChoiceItem(
                     path=['form', 'q1'],
                     options=[Option(value='a'), Option(value='c')]
                 ),
                 ignore_extras=False
             ),
         ]
     )
     submission = {"form": {"q1": "a b d"}}
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission)],
         [[1, None, 1, None, "b d"]]
     )
Esempio n. 8
0
 def test_empty_group(self):
     table_configuration = TableConfiguration(
         path=[
             PathNode(name="form", is_repeat=False),
             PathNode(name="group", is_repeat=False),
             PathNode(name="repeat1", is_repeat=True)
         ],
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=[
                         PathNode(name="form"),
                         PathNode(name="group"),
                         PathNode(name="repeat1", is_repeat=True),
                         PathNode(name="q1")
                     ],
                 ),
                 selected=True,
             ),
         ]
     )
     submission = {
         'domain': 'my-domain',
         '_id': '1234',
         'form': {
             'group': ''
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission, 0)], []
     )
 def test_nested_repeat(self):
     table = TableConfiguration(
         path=[PathNode(name='foo', is_repeat=True), PathNode(name='bar', is_repeat=True)],
     )
     self.assertEqual(
         table._get_sub_documents(
             {
                 'foo': [
                     {
                         'bar': [
                             {'baz': 'a'},
                             {'baz': 'b'}
                         ],
                     },
                     {
                         'bar': [
                             {'baz': 'c'}
                         ],
                     },
                 ],
             },
             0
         ),
         [
             DocRow(row=(0, 0, 0), doc={'baz': 'a'}),
             DocRow(row=(0, 0, 1), doc={'baz': 'b'}),
             DocRow(row=(0, 1, 0), doc={'baz': 'c'}),
         ]
     )
 def test_double_repeat(self):
     table_configuration = TableConfiguration(
         path=[
             PathNode(name="form", is_repeat=False),
             PathNode(name="repeat1", is_repeat=True),
             PathNode(name="group1", is_repeat=False),
             PathNode(name="repeat2", is_repeat=True),
         ],
         columns=[
             RowNumberColumn(
                 selected=True
             ),
             ExportColumn(
                 item=ScalarItem(
                     path=[
                         PathNode(name='form'),
                         PathNode(name='repeat1', is_repeat=True),
                         PathNode(name='group1'),
                         PathNode(name='repeat2', is_repeat=True),
                         PathNode(name='q1')
                     ],
                 ),
                 selected=True,
             ),
         ]
     )
     submission = {
         'domain': 'my-domain',
         '_id': '1234',
         'form': {
             'repeat1': [
                 {
                     'group1': {
                         'repeat2': [
                             {'q1': 'foo'},
                             {'q1': 'bar'}
                         ]
                     }
                 },
                 {
                     'group1': {
                         'repeat2': [
                             {'q1': 'beep'},
                             {'q1': 'boop'}
                         ]
                     }
                 },
             ]
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission, 0)],
         [
             ["0.0.0", 0, 0, 0, 'foo'],
             ["0.0.1", 0, 0, 1, 'bar'],
             ["0.1.0", 0, 1, 0, 'beep'],
             ["0.1.1", 0, 1, 1, 'boop']
         ]
     )
 def test_double_repeat(self):
     table_configuration = TableConfiguration(
         path=[
             PathNode(name="form", is_repeat=False),
             PathNode(name="repeat1", is_repeat=True),
             PathNode(name="group1", is_repeat=False),
             PathNode(name="repeat2", is_repeat=True),
         ],
         columns=[
             RowNumberColumn(
                 selected=True
             ),
             ExportColumn(
                 item=ScalarItem(
                     path=[
                         PathNode(name='form'),
                         PathNode(name='repeat1', is_repeat=True),
                         PathNode(name='group1'),
                         PathNode(name='repeat2', is_repeat=True),
                         PathNode(name='q1')
                     ],
                 ),
                 selected=True,
             ),
         ]
     )
     submission = {
         'domain': 'my-domain',
         '_id': '1234',
         'form': {
             'repeat1': [
                 {
                     'group1': {
                         'repeat2': [
                             {'q1': 'foo'},
                             {'q1': 'bar'}
                         ]
                     }
                 },
                 {
                     'group1': {
                         'repeat2': [
                             {'q1': 'beep'},
                             {'q1': 'boop'}
                         ]
                     }
                 },
             ]
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission, 0)],
         [
             ["0.0.0", 0, 0, 0, 'foo'],
             ["0.0.1", 0, 0, 1, 'bar'],
             ["0.1.0", 0, 1, 0, 'beep'],
             ["0.1.1", 0, 1, 1, 'boop']
         ]
     )
Esempio n. 12
0
    def test_get_column(self):
        table_configuration = TableConfiguration(
            path=[
                PathNode(name='form', is_repeat=False),
                PathNode(name="repeat1", is_repeat=True)
            ],
            columns=[
                ExportColumn(item=ScalarItem(path=[
                    PathNode(name='form'),
                    PathNode(name='repeat1', is_repeat=True),
                    PathNode(name='q1')
                ], )),
                ExportColumn(item=ScalarItem(path=[
                    PathNode(name="form"),
                    PathNode(name="user_id"),
                ],
                                             transform=USERNAME_TRANSFORM)),
                ExportColumn(item=ScalarItem(path=[
                    PathNode(name='form'),
                    PathNode(name='repeat1', is_repeat=True),
                    PathNode(name='q2')
                ], )),
            ])

        index, column = table_configuration.get_column(
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='q1')
            ],
            'ScalarItem',
            None,
        )
        self.assertEqual(column.item.path, [
            PathNode(name='form'),
            PathNode(name='repeat1', is_repeat=True),
            PathNode(name='q1')
        ])
        self.assertEqual(index, 0)

        index, column = table_configuration.get_column(
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='DoesNotExist')
            ],
            'ScalarItem',
            None,
        )
        self.assertIsNone(column)

        # Verify that get_column ignores deid transforms
        index, column = table_configuration.get_column(
            [PathNode(name="form"),
             PathNode(name="user_id")], 'ScalarItem', USERNAME_TRANSFORM)
        self.assertIsNotNone(column)
        self.assertEqual(index, 1)
Esempio n. 13
0
 def setUp(self):
     self.instance = FormExportInstance(tables=[
         TableConfiguration(path=MAIN_TABLE),
         TableConfiguration(path=[
             PathNode(name='data', is_repeat=False),
             PathNode(name='repeat', is_repeat=True)
         ],
                            columns=[MultiMediaExportColumn(selected=True)])
     ])
    def test_basic(self):

        table = TableConfiguration(path=[])
        self.assertEqual(
            table._get_sub_documents(
                {'foo': 'a'},
                0
            ),
            [
                DocRow(row=(0,), doc={'foo': 'a'})
            ]
        )
    def test_basic(self):

        table = TableConfiguration(path=[])
        self.assertEqual(
            table._get_sub_documents(
                {'foo': 'a'},
                0
            ),
            [
                DocRow(row=(0,), doc={'foo': 'a'})
            ]
        )
Esempio n. 16
0
    def test_multi_table(self):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            tables=[
                TableConfiguration(
                    label="My table",
                    selected=True,
                    path=[],
                    columns=[
                        ExportColumn(
                            label="Q3",
                            item=ScalarItem(
                                path=[PathNode(name='form'), PathNode(name='q3')],
                            ),
                            selected=True,
                        ),
                    ]
                ),
                TableConfiguration(
                    label="My other table",
                    selected=True,
                    path=[PathNode(name='form', is_repeat=False), PathNode(name="q2", is_repeat=False)],
                    columns=[
                        ExportColumn(
                            label="Q4",
                            item=ScalarItem(
                                path=[PathNode(name='form'), PathNode(name='q2'), PathNode(name='q4')],
                            ),
                            selected=True,
                        ),
                    ]
                )
            ]
        )
        writer = _get_writer([export_instance])
        with writer.open([export_instance]):
            _write_export_instance(writer, export_instance, self.docs)
        with ExportFile(writer.path, writer.format) as export:
            self.assertEqual(
                json.loads(export.read()),
                {
                    u'My table': {
                        u'headers': [u'Q3'],
                        u'rows': [[u'baz'], [u'bop']],

                    },
                    u'My other table': {
                        u'headers': [u'Q4'],
                        u'rows': [[u'bar'], [u'boop']],
                    }
                }
            )
Esempio n. 17
0
 def test_single_iteration_repeat(self):
     table = TableConfiguration(path=[
         PathNode(name='group1', is_repeat=False),
         PathNode(name='repeat1', is_repeat=True)
     ], )
     self.assertEqual(
         table._get_sub_documents({'group1': {
             'repeat1': {
                 'baz': 'a'
             },
         }}, 0), [
             DocRow(row=(0, 0), doc={'baz': 'a'}),
         ])
Esempio n. 18
0
 def test_simple_repeat(self):
     table = TableConfiguration(path=[PathNode(name="foo", is_repeat=True)])
     self.assertEqual(
         table._get_sub_documents({'foo': [
             {
                 'bar': 'a'
             },
             {
                 'bar': 'b'
             },
         ]}, 0), [
             DocRow(row=(0, 0), doc={'bar': 'a'}),
             DocRow(row=(0, 1), doc={'bar': 'b'})
         ])
    def test_table_containing_duplicate_paths_with_differing_doc_types_can_find_either(self):
        path = [PathNode(name='closed')]
        prop1 = ScalarItem(path=path)
        prop2 = ExportItem(path=path)
        table_config = TableConfiguration(
            columns=[
                ExportColumn(item=prop1), ExportColumn(item=prop2)
            ]
        )

        scalarIndex, _ = table_config.get_column(path, 'ScalarItem', None)
        exportIndex, _ = table_config.get_column(path, 'ExportItem', None)

        self.assertEqual(scalarIndex, 0)
        self.assertEqual(exportIndex, 1)
Esempio n. 20
0
    def test_multi_table(self, export_save):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       ExportColumn(
                                           label="Q3",
                                           item=ScalarItem(path=[
                                               PathNode(name='form'),
                                               PathNode(name='q3')
                                           ], ),
                                           selected=True,
                                       ),
                                   ]),
                TableConfiguration(label="My other table",
                                   selected=True,
                                   path=[
                                       PathNode(name='form', is_repeat=False),
                                       PathNode(name="q2", is_repeat=False)
                                   ],
                                   columns=[
                                       ExportColumn(
                                           label="Q4",
                                           item=ScalarItem(path=[
                                               PathNode(name='form'),
                                               PathNode(name='q2'),
                                               PathNode(name='q4')
                                           ], ),
                                           selected=True,
                                       ),
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance, {
                'My table': {
                    'headers': ['Q3'],
                    'rows': [['baz'], ['bop']],
                },
                'My other table': {
                    'headers': ['Q4'],
                    'rows': [['bar'], ['boop']],
                }
            })
        self.assertTrue(export_save.called)
Esempio n. 21
0
    def test_paginated_table(self, export_save):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   columns=[
                                       ExportColumn(label="Q3",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q3')
                                                    ], ),
                                                    selected=True),
                                       ExportColumn(label="Q1",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q1')
                                                    ], ),
                                                    selected=True),
                                   ])
            ])

        assert_instance_gives_results(
            self.docs + self.docs, export_instance, {
                'My table_000': {
                    'headers': ['Q3', 'Q1'],
                    'rows': [['baz', 'foo'], ['bop', 'bip']],
                },
                'My table_001': {
                    'headers': ['Q3', 'Q1'],
                    'rows': [['baz', 'foo'], ['bop', 'bip']],
                }
            })
        self.assertTrue(export_save.called)
Esempio n. 22
0
    def test_simple_table(self, export_save):
        """
        Confirm that some simple documents and a simple FormExportInstance
        are writtern with _write_export_file() correctly
        """

        export_instance = FormExportInstance(
            export_format=Format.JSON,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   columns=[
                                       ExportColumn(label="Q3",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q3')
                                                    ], ),
                                                    selected=True),
                                       ExportColumn(label="Q1",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q1')
                                                    ], ),
                                                    selected=True),
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance, {
                'My table': {
                    'headers': ['Q3', 'Q1'],
                    'rows': [['baz', 'foo'], ['bop', 'bip']],
                }
            })
        self.assertTrue(export_save.called)
Esempio n. 23
0
    def test_empty_table_label(self):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            split_multiselects=True,
            tables=[
                TableConfiguration(label="",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       ExportColumn(label="Q1",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q1')
                                                    ], ),
                                                    selected=True),
                                   ])
            ])
        writer = _get_writer([export_instance])
        with writer.open([export_instance]):
            _write_export_instance(writer, export_instance, self.docs)

        with ExportFile(writer.path, writer.format) as export:
            self.assertEqual(json.loads(export.read()), {
                u'Sheet1': {
                    u'headers': [u'Q1'],
                    u'rows': [[u'foo'], [u'bip']],
                }
            })
Esempio n. 24
0
    def test_multi_table_order(self):
        tables = [
            TableConfiguration(label="My table {}".format(i),
                               selected=True,
                               path=[],
                               columns=[
                                   ExportColumn(
                                       label="Q{}".format(i),
                                       item=ScalarItem(path=[
                                           PathNode(name='form'),
                                           PathNode(name='q{}'.format(i))
                                       ], ),
                                       selected=True,
                                   ),
                               ]) for i in range(10)
        ]
        export_instance = FormExportInstance(export_format=Format.HTML,
                                             tables=tables)
        writer = _get_writer([export_instance])
        docs = [{
            'domain': 'my-domain',
            '_id': '1234',
            "form": {'q{}'.format(i): 'value {}'.format(i)
                     for i in range(10)}
        }]
        with writer.open([export_instance]):
            _write_export_instance(writer, export_instance, docs)
        with ExportFile(writer.path, writer.format) as export:
            exported_tables = [
                table for table in re.findall('<h2>(.*)</h2>', export.read())
            ]

        expected_tables = [t.label for t in tables]
        self.assertEqual(expected_tables, exported_tables)
Esempio n. 25
0
 def test_formid_column_label(self):
     export_with_modified_formid_column = FormExportInstance(
         is_odata_config=True,
         tables=[
             TableConfiguration(
                 selected=True,
                 columns=[
                     ExportColumn(
                         label='modified_form_id_column',
                         item=ExportItem(
                             path=[
                                 PathNode(name='form'),
                                 PathNode(name='meta'),
                                 PathNode(name='instanceID')
                             ]
                         ),
                         selected=True,
                     )
                 ]
             )
         ]
     )
     export_with_modified_formid_column.save()
     self.addCleanup(export_with_modified_formid_column.delete)
     cleaned_export = FormExportInstance.get(export_with_modified_formid_column.get_id)
     self.assertEqual(cleaned_export.tables[0].columns[0].label, 'formid')
Esempio n. 26
0
    def test_populated_metadata_document(self):
        odata_config = FormExportInstance(
            _id='my_config_id',
            domain=self.domain.name,
            is_odata_config=True,
            tables=[
                TableConfiguration(
                    columns=[
                        ExportColumn(label='selected_property_1', selected=True),
                        ExportColumn(label='selected_property_2', selected=True),
                        ExportColumn(label='unselected_property'),
                    ],
                ),
            ]
        )
        odata_config.save()
        self.addCleanup(odata_config.delete)

        correct_credentials = self._get_correct_credentials()
        with flag_enabled('ODATA'):
            response = self._execute_query(correct_credentials)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'application/xml')
        self.assertEqual(response['OData-Version'], '4.0')
        self.assertXmlEqual(
            self.get_xml(
                'populated_form_odata_metadata_document_from_config',
                override_path=PATH_TO_TEST_DATA
            ),
            response.content
        )
Esempio n. 27
0
    def test_split_questions_false(self):
        """Ensure multiselects are not split when `split_multiselects` is set to False"""
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            split_multiselects=False,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       SplitExportColumn(
                                           label="MC",
                                           item=MultipleChoiceItem(
                                               path=[
                                                   PathNode(name='form'),
                                                   PathNode(name='mc')
                                               ],
                                               options=[
                                                   Option(value='one'),
                                                   Option(value='two'),
                                               ]),
                                           selected=True,
                                       )
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance, {
                'My table': {
                    'headers': ['MC'],
                    'rows': [['two extra'], ['one two']],
                }
            })
Esempio n. 28
0
    def test_transform_dates(self, export_save):
        """Ensure dates are transformed for excel when `transform_dates` is set to True"""
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            transform_dates=True,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       ExportColumn(
                                           label="Date",
                                           item=MultipleChoiceItem(path=[
                                               PathNode(name='form'),
                                               PathNode(name='date')
                                           ], ),
                                           selected=True,
                                       )
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance, {
                'My table': {
                    'headers': ['Date'],
                    'rows': [[MISSING_VALUE],
                             [
                                 couch_to_excel_datetime(
                                     '2015-07-22T14:16:49.584880Z', None)
                             ]],
                }
            })
        self.assertTrue(export_save.called)
Esempio n. 29
0
 def test_ignore_form_link_label(self):
     export_with_form_link = FormExportInstance(
         is_odata_config=True,
         tables=[
             TableConfiguration(
                 selected=True,
                 columns=[
                     ExportColumn(
                         label='my_form_link',
                         item=ExportItem(
                             path=[
                                 PathNode(name='form'),
                                 PathNode(name='meta'),
                                 PathNode(name='instanceID')
                             ],
                             transform=FORM_ID_TO_LINK,
                         ),
                         selected=True,
                     )
                 ]
             )
         ]
     )
     export_with_form_link.save()
     self.addCleanup(export_with_form_link.delete)
     cleaned_export = FormExportInstance.get(export_with_form_link.get_id)
     self.assertEqual(cleaned_export.tables[0].columns[0].label, 'my_form_link')
Esempio n. 30
0
 def get_instance(cls, domain_name):
     return FormExportInstance(
         domain=domain_name,
         is_odata_config=True,
         transform_dates=False,
         tables=[
             TableConfiguration(
                 selected=True,
                 columns=[
                     ExportColumn(label='received_on',
                                  selected=True,
                                  item=ExportItem(
                                      path=[PathNode(name='received_on')])),
                     ExportColumn(label='started_time',
                                  selected=True,
                                  item=ExportItem(path=[
                                      PathNode(name='form'),
                                      PathNode(name='meta'),
                                      PathNode(name='timeStart'),
                                  ])),
                     ExportColumn(label='selected_property_1',
                                  selected=True),
                     ExportColumn(label='selected_property_2',
                                  selected=True),
                     ExportColumn(label='unselected_property'),
                 ],
             ),
         ])
Esempio n. 31
0
 def get_instance(cls, domain_name):
     return CaseExportInstance(
         domain=domain_name,
         is_odata_config=True,
         transform_dates=False,
         tables=[
             TableConfiguration(
                 selected=True,
                 columns=[
                     ExportColumn(
                         label='closed',
                         selected=True,
                         # this is what exports generate for a base level property
                         item=ExportItem(path=[PathNode(name='closed')])),
                     ExportColumn(
                         label='date_modified',
                         selected=True,
                         item=ExportItem(
                             path=[PathNode(name='date_modified')])),
                     ExportColumn(label='selected_property_1',
                                  selected=True),
                     ExportColumn(label='selected_property_2',
                                  selected=True),
                     ExportColumn(label='unselected_property'),
                 ],
             ),
         ])
Esempio n. 32
0
    def test_empty_table_label(self, export_save):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            split_multiselects=True,
            tables=[
                TableConfiguration(label="",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       ExportColumn(label="Q1",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q1')
                                                    ], ),
                                                    selected=True),
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance,
            {'Sheet1': {
                'headers': ['Q1'],
                'rows': [['foo'], ['bip']],
            }})
        self.assertTrue(export_save.called)
    def setUpClass(cls):
        super().setUpClass()
        with trap_extra_setup(ConnectionError,
                              msg="cannot connect to elasicsearch"):
            cls.es = get_es_new()
            initialize_index_and_mapping(cls.es, CASE_INDEX_INFO)

        cls.domain = uuid.uuid4().hex
        now = datetime.utcnow()
        cases = [
            new_case(domain=cls.domain,
                     foo="apple",
                     bar="banana",
                     server_modified_on=now - timedelta(hours=3)),
            new_case(domain=cls.domain,
                     foo="orange",
                     bar="pear",
                     server_modified_on=now - timedelta(hours=2)),
        ]

        for case in cases:
            send_to_elasticsearch('cases', case.to_json())

        cls.es.indices.refresh(CASE_INDEX_INFO.index)

        cls.export_instance = CaseExportInstance(
            export_format=Format.UNZIPPED_CSV,
            domain=cls.domain,
            case_type=DEFAULT_CASE_TYPE,
            tables=[
                TableConfiguration(
                    label="My table",
                    selected=True,
                    path=[],
                    columns=[
                        ExportColumn(
                            label="Foo column",
                            item=ExportItem(path=[PathNode(name="foo")]),
                            selected=True,
                        ),
                        ExportColumn(
                            label="Bar column",
                            item=ExportItem(path=[PathNode(name="bar")]),
                            selected=True,
                        )
                    ])
            ])
        cls.export_instance.save()

        cls.incremental_export = IncrementalExport.objects.create(
            domain=cls.domain,
            name='test_export',
            export_instance_id=cls.export_instance.get_id,
            connection_settings=ConnectionSettings.objects.create(
                domain=cls.domain,
                name='test conn',
                url='http://somewhere',
                auth_type=BASIC_AUTH,
            ))
Esempio n. 34
0
 def test_get_export_properties(self):
     export_instance = FormExportInstance(tables=[
         TableConfiguration(label="My table",
                            selected=True,
                            path=[],
                            columns=[
                                ExportColumn(
                                    label="Q3",
                                    item=ScalarItem(path=[
                                        PathNode(name='form'),
                                        PathNode(name='q3')
                                    ], ),
                                    selected=True,
                                ),
                                ExportColumn(
                                    label="dontshow",
                                    item=ScalarItem(path=[
                                        PathNode(name='form'),
                                        PathNode(name='dontshow')
                                    ], ),
                                    selected=False,
                                ),
                            ]),
         TableConfiguration(label="My other table",
                            selected=True,
                            path=[
                                PathNode(name='form', is_repeat=False),
                                PathNode(name="q2", is_repeat=False)
                            ],
                            columns=[
                                ExportColumn(
                                    label="Q4",
                                    item=ScalarItem(path=[
                                        PathNode(name='form'),
                                        PathNode(name='q2'),
                                        PathNode(name='q4')
                                    ], ),
                                    selected=True,
                                ),
                            ])
     ])
     with mock.patch('corehq.apps.export.models.new.FormExportInstance.get',
                     return_value=export_instance):
         props = _get_export_properties("fake id for my export instance",
                                        False)
         self.assertEqual(props, set(['q2-q4', 'q3']))
Esempio n. 35
0
    def test_populated_metadata_document(self):
        odata_config_1 = FormExportInstance(
            _id='odata_config_1',
            domain=self.domain.name,
            is_odata_config=True,
            tables=[TableConfiguration(columns=[])])
        odata_config_1.save()
        self.addCleanup(odata_config_1.delete)

        odata_config_2 = FormExportInstance(
            _id='odata_config_2',
            domain=self.domain.name,
            is_odata_config=True,
            tables=[
                TableConfiguration(columns=[
                    ExportColumn(label='selected_property_1', selected=True),
                    ExportColumn(label='selected_property_2', selected=True),
                    ExportColumn(label='unselected_property'),
                ], ),
            ])
        odata_config_2.save()
        self.addCleanup(odata_config_2.delete)

        non_odata_config = FormExportInstance(domain=self.domain.name)
        non_odata_config.save()
        self.addCleanup(non_odata_config.delete)

        config_in_other_domain = FormExportInstance(domain='other_domain',
                                                    is_odata_config=True)
        config_in_other_domain.save()
        self.addCleanup(config_in_other_domain.delete)

        correct_credentials = self._get_correct_credentials()
        with flag_enabled('ODATA'):
            with patch(
                    'corehq.apps.api.odata.views.get_odata_form_configs_by_domain',
                    return_value=sorted(get_odata_form_configs_by_domain(
                        self.domain.name),
                                        key=lambda _config: _config.get_id)):
                response = self._execute_query(correct_credentials)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'application/xml')
        self.assertEqual(response['OData-Version'], '4.0')
        self.assertXmlEqual(
            self.get_xml('populated_form_odata_metadata_document_from_config',
                         override_path=PATH_TO_TEST_DATA), response.content)
Esempio n. 36
0
 def test_get_export_properties(self):
     export_instance = FormExportInstance(tables=[
         TableConfiguration(label="My table",
                            selected=True,
                            path=[],
                            columns=[
                                ExportColumn(
                                    label="Q3",
                                    item=ScalarItem(path=[
                                        PathNode(name='form'),
                                        PathNode(name='q3')
                                    ], ),
                                    selected=True,
                                ),
                                ExportColumn(
                                    label="dontshow",
                                    item=ScalarItem(path=[
                                        PathNode(name='form'),
                                        PathNode(name='dontshow')
                                    ], ),
                                    selected=False,
                                ),
                            ]),
         TableConfiguration(label="My other table",
                            selected=True,
                            path=[
                                PathNode(name='form', is_repeat=False),
                                PathNode(name="q2", is_repeat=False)
                            ],
                            columns=[
                                ExportColumn(
                                    label="Q4",
                                    item=ScalarItem(path=[
                                        PathNode(name='form'),
                                        PathNode(name='q2'),
                                        PathNode(name='q4')
                                    ], ),
                                    selected=True,
                                ),
                            ])
     ])
     props = _get_export_properties(export_instance)
     self.assertEqual(props, set(['q2-q4', 'q3']))
 def test_simple_repeat(self):
     table = TableConfiguration(
         path=[PathNode(name="foo", is_repeat=True)]
     )
     self.assertEqual(
         table._get_sub_documents(
             {
                 'foo': [
                     {'bar': 'a'},
                     {'bar': 'b'},
                 ]
             },
             0
         ),
         [
             DocRow(row=(0, 0), doc={'bar': 'a'}),
             DocRow(row=(0, 1), doc={'bar': 'b'})
         ]
     )
 def test_single_iteration_repeat(self):
     table = TableConfiguration(
         path=[PathNode(name='group1', is_repeat=False), PathNode(name='repeat1', is_repeat=True)],
     )
     self.assertEqual(
         table._get_sub_documents(
             {
                 'group1': {
                     'repeat1': {
                         'baz': 'a'
                     },
                 }
             },
             0
         ),
         [
             DocRow(row=(0, 0), doc={'baz': 'a'}),
         ]
     )
Esempio n. 39
0
 def test_selected_false(self):
     export_json = get_export_json(
         CaseExportInstance(export_format=Format.JSON,
                            domain=DOMAIN,
                            case_type=DEFAULT_CASE_TYPE,
                            tables=[
                                TableConfiguration(label="My table",
                                                   selected=False,
                                                   path=[],
                                                   columns=[])
                            ]))
     self.assertEqual(export_json, {})
    def test_get_column(self):
        table_configuration = TableConfiguration(
            path=['form', 'repeat1'],
            columns=[
                ExportColumn(
                    item=ScalarItem(
                        path=['form', 'repeat1', 'q1'],
                    )
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=['form', 'repeat1', 'q2'],
                    )
                ),
            ]
        )

        column = table_configuration.get_column(['form', 'repeat1', 'q1'])
        self.assertEqual(column.item.path, ['form', 'repeat1', 'q1'])

        column = table_configuration.get_column(['form', 'repeat1', 'DoesNotExist'])
        self.assertIsNone(column)
 def test_double_repeat(self):
     table_configuration = TableConfiguration(
         path=['form', 'repeat1', 'group1', 'repeat2'],
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=['form', 'repeat1', 'group1', 'repeat2', 'q1'],
                 ),
                 selected=True,
             ),
         ]
     )
     submission = {
         'form': {
             'repeat1': [
                 {
                     'group1': {
                         'repeat2': [
                             {'q1': 'foo'},
                             {'q1': 'bar'}
                         ]
                     }
                 },
                 {
                     'group1': {
                         'repeat2': [
                             {'q1': 'beep'},
                             {'q1': 'boop'}
                         ]
                     }
                 },
             ]
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission)],
         [['foo'], ['bar'], ['beep'], ['boop']]
     )
 def test_repeat(self):
     table_configuration = TableConfiguration(
         path=['form', 'repeat1'],
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=['form', 'repeat1', 'q1'],
                 )
             ),
         ]
     )
     submission = {
         'form': {
             'repeat1': [
                 {'q1': 'foo'},
                 {'q1': 'bar'}
             ]
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission)],
         [ExportRow(['foo']).data, ExportRow(['bar']).data]
     )
 def test_simple(self):
     table_configuration = TableConfiguration(
         path=[],
         columns=[
             ExportColumn(
                 item=ScalarItem(
                     path=[PathNode(name='form'), PathNode(name='q3')],
                 ),
                 selected=True,
             ),
             ExportColumn(
                 item=ScalarItem(
                     path=[PathNode(name='form'), PathNode(name='q1')],
                 ),
                 selected=True,
             ),
             ExportColumn(
                 item=ScalarItem(
                     path=[PathNode(name='form'), PathNode(name='q2')],
                 ),
                 selected=False,
             ),
         ]
     )
     submission = {
         'domain': 'my-domain',
         '_id': '1234',
         "form": {
             "q1": "foo",
             "q2": "bar",
             "q3": "baz"
         }
     }
     self.assertEqual(
         [row.data for row in table_configuration.get_rows(submission, 0)],
         [['baz', 'foo']]
     )
    def test_get_column(self):
        table_configuration = TableConfiguration(
            path=[PathNode(name='form', is_repeat=False), PathNode(name="repeat1", is_repeat=True)],
            columns=[
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name='form'),
                            PathNode(name='repeat1', is_repeat=True),
                            PathNode(name='q1')
                        ],
                    )
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name="form"),
                            PathNode(name="user_id"),
                        ],
                        transform=USERNAME_TRANSFORM
                    )
                ),
                ExportColumn(
                    item=ScalarItem(
                        path=[
                            PathNode(name='form'),
                            PathNode(name='repeat1', is_repeat=True),
                            PathNode(name='q2')
                        ],
                    )
                ),
            ]
        )

        index, column = table_configuration.get_column(
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='q1')
            ],
            'ScalarItem',
            None,
        )
        self.assertEqual(
            column.item.path,
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='q1')
            ]
        )
        self.assertEqual(index, 0)

        index, column = table_configuration.get_column(
            [
                PathNode(name='form'),
                PathNode(name='repeat1', is_repeat=True),
                PathNode(name='DoesNotExist')
            ],
            'ScalarItem',
            None,
        )
        self.assertIsNone(column)

        # Verify that get_column ignores deid transforms
        index, column = table_configuration.get_column(
            [PathNode(name="form"), PathNode(name="user_id")],
            'ScalarItem',
            USERNAME_TRANSFORM
        )
        self.assertIsNotNone(column)
        self.assertEqual(index, 1)