Пример #1
0
    def test_get_value(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name='form'), PathNode(name='q1')],
                options=[Option(value='a'), Option(value='c')]
            ),
            ignore_unspecified_options=False
        )
        doc = {"q1": "a b d"}
        self.assertEqual(column.get_value(
            'domain',
            'docid',
            doc,
            [PathNode(name='form')],
            split_column=True),
            [1, EMPTY_VALUE, "b d"]
        )

        doc = {}
        self.assertEqual(
            column.get_value(
                'domain',
                'docid',
                doc,
                [PathNode(name='form')],
                split_column=True
            ),
            [MISSING_VALUE, MISSING_VALUE, MISSING_VALUE]
        )
Пример #2
0
 def test_basic_get_headers(self):
     column = SplitExportColumn(
         label="Fruit",
         item=MultipleChoiceItem(options=[Option(value="Apple"), Option(value="Banana")]),
         ignore_unspecified_options=True,
     )
     self.assertEqual(column.get_headers(split_column=True), ["Fruit | Apple", "Fruit | Banana"])
Пример #3
0
    def test_get_value(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name='form'), PathNode(name='q1')],
                options=[Option(value='a'), Option(value='c')]
            ),
            ignore_unspecified_options=False
        )
        doc = {"q1": "a b d"}
        self.assertEqual(column.get_value(
            'domain',
            'docid',
            doc,
            [PathNode(name='form')],
            split_column=True),
            [1, EMPTY_VALUE, "b d"]
        )

        doc = {}
        self.assertEqual(
            column.get_value(
                'domain',
                'docid',
                doc,
                [PathNode(name='form')],
                split_column=True
            ),
            [MISSING_VALUE, MISSING_VALUE, MISSING_VALUE]
        )
Пример #4
0
 def test_get_headers_with_extras(self):
     column = SplitExportColumn(
         label="Fruit - {option}",
         item=MultipleChoiceItem(options=[Option(value="Apple"), Option(value="Banana")]),
         ignore_unspecified_options=False,
     )
     self.assertEqual(column.get_headers(split_column=True), ["Fruit - Apple", "Fruit - Banana", "Fruit - extra"])
Пример #5
0
 def test_get_headers_with_extras(self):
     column = SplitExportColumn(
         label="Fruit - {option}",
         item=MultipleChoiceItem(
             options=[Option(value='Apple'),
                      Option(value='Banana')]),
         ignore_unspecified_options=False)
     self.assertEqual(column.get_headers(split_column=True),
                      ["Fruit - Apple", "Fruit - Banana", "Fruit - extra"])
Пример #6
0
 def test_basic_get_headers(self):
     column = SplitExportColumn(
         label="Fruit",
         item=MultipleChoiceItem(
             options=[Option(value='Apple'),
                      Option(value='Banana')]),
         ignore_unspecified_options=True)
     self.assertEqual(column.get_headers(split_column=True),
                      ["Fruit | Apple", "Fruit | Banana"])
Пример #7
0
 def test_get_headers_with_template_string(self):
     column = SplitExportColumn(
         label="Fruit - {option}",
         item=MultipleChoiceItem(
             options=[Option(value='Apple'), Option(value='Banana')]
         ),
         ignore_unspecified_options=True
     )
     self.assertEqual(column.get_headers(split_column=True), ["Fruit - Apple", "Fruit - Banana"])
Пример #8
0
    def test_get_value(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name="form"), PathNode(name="mc")], options=[Option(value="foo"), Option(value="bar")]
            )
        )
        result = column.get_value("domain", "docid", {"form": {"mc": "foo extra"}}, [], split_column=True)
        self.assertEqual(result, [1, EMPTY_VALUE, "extra"])

        result = column.get_value("domain", "docid", {"form": {"mc": "foo extra"}}, [], split_column=False)
        self.assertEqual(result, "foo extra")
Пример #9
0
    def test_get_value(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name='form'), PathNode(name='mc')],
                options=[Option(value="foo"), Option(value="bar")]
            ),
        )
        result = column.get_value('domain', 'docid', {'form': {'mc': 'foo extra'}}, [], split_column=True)
        self.assertEqual(result, [1, EMPTY_VALUE, 'extra'])

        result = column.get_value('domain', 'docid', {'form': {'mc': 'foo extra'}}, [], split_column=False)
        self.assertEqual(result, 'foo extra')
Пример #10
0
    def test_get_value(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name='form'), PathNode(name='mc')],
                options=[Option(value="foo"), Option(value="bar")]
            ),
        )
        result = column.get_value('domain', 'docid', {'form': {'mc': 'foo extra'}}, [], split_column=True)
        self.assertEqual(result, [1, EMPTY_VALUE, 'extra'])

        result = column.get_value('domain', 'docid', {'form': {'mc': 'foo extra'}}, [], split_column=False)
        self.assertEqual(result, 'foo extra')
Пример #11
0
 def test_get_value_numerical(self):
     column = SplitExportColumn(
         item=MultipleChoiceItem(
             path=[PathNode(name="form"), PathNode(name="q1")], options=[Option(value="1"), Option(value="2")]
         ),
         ignore_unspecified_options=False,
     )
     doc = {"q1": 3}
     self.assertEqual(
         column.get_value("domain", "docid", doc, [PathNode(name="form")], split_column=True),
         [EMPTY_VALUE, EMPTY_VALUE, 3],
     )
Пример #12
0
    def test_get_headers(self):
        column = SplitExportColumn(
            item=MultipleChoiceItem(
                path=[PathNode(name="form"), PathNode(name="mc")], options=[Option(value="foo"), Option(value="bar")]
            ),
            label="form.mc",
        )
        result = column.get_headers(split_column=True)
        self.assertEqual(result, ["form.mc | foo", "form.mc | bar", "form.mc | extra"])

        result = column.get_headers(split_column=False)
        self.assertEqual(result, ["form.mc"])
Пример #13
0
 def test_get_value_numerical(self):
     column = SplitExportColumn(item=MultipleChoiceItem(
         path=[PathNode(name='form'),
               PathNode(name='q1')],
         options=[Option(value='1'), Option(value='2')]),
                                ignore_unspecified_options=False)
     doc = {"q1": 3}
     self.assertEqual(
         column.get_value('domain',
                          'docid',
                          doc, [PathNode(name='form')],
                          split_column=True), [EMPTY_VALUE, EMPTY_VALUE, 3])
Пример #14
0
    def test_get_headers(self):
        column = SplitExportColumn(item=MultipleChoiceItem(
            path=[PathNode(name='form'),
                  PathNode(name='mc')],
            options=[Option(value="foo"),
                     Option(value="bar")]),
                                   label='form.mc')
        result = column.get_headers(split_column=True)
        self.assertEqual(result,
                         ['form.mc | foo', 'form.mc | bar', 'form.mc | extra'])

        result = column.get_headers(split_column=False)
        self.assertEqual(result, ['form.mc'])
Пример #15
0
 def test_ignore_extas(self):
     column = SplitExportColumn(item=MultipleChoiceItem(
         path=[PathNode(name='form'),
               PathNode(name='q1')],
         options=[Option(value='a'), Option(value='c')]),
                                ignore_unspecified_options=True)
     doc = {"q1": "a b d"}
     self.assertEqual(
         column.get_value('domain',
                          'docid',
                          doc, [PathNode(name="form")],
                          split_column=True),
         [1, None],
     )
Пример #16
0
 def test_ignore_extas(self):
     column = SplitExportColumn(
         item=MultipleChoiceItem(
             path=[PathNode(name="form"), PathNode(name="q1")], options=[Option(value="a"), Option(value="c")]
         ),
         ignore_unspecified_options=True,
     )
     doc = {"q1": "a b d"}
     self.assertEqual(
         column.get_value("domain", "docid", doc, [PathNode(name="form")], split_column=True), [1, EMPTY_VALUE]
     )
     doc = {}
     self.assertEqual(
         column.get_value("domain", "docid", doc, [PathNode(name="form")], split_column=True),
         [MISSING_VALUE, MISSING_VALUE],
     )
Пример #17
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']],
                }
            })
Пример #18
0
 def test_get_value_numerical(self):
     column = SplitExportColumn(
         item=MultipleChoiceItem(
             path=[PathNode(name='form'), PathNode(name='q1')],
             options=[Option(value='1'), Option(value='2')]
         ),
         ignore_unspecified_options=False
     )
     doc = {"q1": 3}
     self.assertEqual(column.get_value(
         'domain',
         'docid',
         doc,
         [PathNode(name='form')],
         split_column=True),
         [EMPTY_VALUE, EMPTY_VALUE, 3]
     )
Пример #19
0
 def test_ignore_extas(self):
     column = SplitExportColumn(
         item=MultipleChoiceItem(
             path=[PathNode(name='form'), PathNode(name='q1')],
             options=[Option(value='a'), Option(value='c')]
         ),
         ignore_unspecified_options=True
     )
     doc = {"q1": "a b d"}
     self.assertEqual(column.get_value(
         'domain',
         'docid',
         doc,
         [PathNode(name="form")],
         split_column=True),
         [1, None],
     )
Пример #20
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,
                    )
                ]
            )]
        )
        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'MC'],
                        u'rows': [['two extra'], ['one two']],

                    }
                }
            )
Пример #21
0
    def test_split_questions(self, export_save):
        """Ensure columns are split when `split_multiselects` is set to True"""
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            split_multiselects=True,
            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 | one', 'MC | two', 'MC | extra'],
                    'rows': [[EMPTY_VALUE, 1, 'extra'], [1, 1, '']],
                }
            })
        self.assertTrue(export_save.called)