Esempio n. 1
0
    def test_form_stock_columns(self):
        """Ensure that we can export stock properties in a form export"""
        docs = [{
            '_id': 'simone-biles',
            'domain': DOMAIN,
            'form': {
                'balance': [
                    {
                        '@type': 'question-id',
                        'entry': {
                            '@quantity': '2',
                        }
                    }, {
                        '@type': 'other-question-id',
                        'entry': {
                            '@quantity': '3',
                        }
                    }]
            },
        }, {
            '_id': 'sam-mikulak',
            'domain': DOMAIN,
            'form': {
                'balance': {
                    '@type': 'question-id',
                    'entry': {
                        '@quantity': '2',
                    }
                },
            },
        }, {
            '_id': 'kerri-walsh',
            'domain': DOMAIN,
            'form': {
                'balance': {
                    '@type': 'other-question-id',
                    'entry': {
                        '@quantity': '2',
                    }
                },
            },
        }, {
            '_id': 'april-ross',
            'domain': DOMAIN,
            'form': {},
        }]
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            tables=[TableConfiguration(
                label="My table",
                selected=True,
                path=[],
                columns=[
                    StockFormExportColumn(
                        label="StockItem @type",
                        item=StockItem(
                            path=[
                                PathNode(name='form'),
                                PathNode(name='balance:question-id'),
                                PathNode(name='@type'),
                            ],
                        ),
                        selected=True,
                    ),
                    StockFormExportColumn(
                        label="StockItem @quantity",
                        item=StockItem(
                            path=[
                                PathNode(name='form'),
                                PathNode(name='balance:question-id'),
                                PathNode(name='entry'),
                                PathNode(name='@quantity'),
                            ],
                        ),
                        selected=True,
                    ),
                ]
            )]
        )
        writer = _get_writer([export_instance])

        with writer.open([export_instance]):
            _write_export_instance(writer, export_instance, docs)

        with ExportFile(writer.path, writer.format) as export:
            self.assertEqual(
                json.loads(export.read()),
                {
                    u'My table': {
                        u'headers': [u'StockItem @type', u'StockItem @quantity'],
                        u'rows': [
                            ['question-id', '2'],
                            ['question-id', '2'],
                            [MISSING_VALUE, MISSING_VALUE],
                            [MISSING_VALUE, MISSING_VALUE],
                        ],
                    }
                }
            )
Esempio n. 2
0
    def test_form_stock_columns(self, export_save):
        """Ensure that we can export stock properties in a form export"""
        docs = [{
            '_id': 'simone-biles',
            'domain': DOMAIN,
            'form': {
                'balance': [{
                    '@type': 'question-id',
                    'entry': {
                        '@quantity': '2',
                    }
                }, {
                    '@type': 'other-question-id',
                    'entry': {
                        '@quantity': '3',
                    }
                }]
            },
        }, {
            '_id': 'sam-mikulak',
            'domain': DOMAIN,
            'form': {
                'balance': {
                    '@type': 'question-id',
                    'entry': {
                        '@quantity': '2',
                    }
                },
            },
        }, {
            '_id': 'kerri-walsh',
            'domain': DOMAIN,
            'form': {
                'balance': {
                    '@type': 'other-question-id',
                    'entry': {
                        '@quantity': '2',
                    }
                },
            },
        }, {
            '_id': 'april-ross',
            'domain': DOMAIN,
            'form': {},
        }]
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            tables=[
                TableConfiguration(
                    label="My table",
                    selected=True,
                    path=[],
                    columns=[
                        StockFormExportColumn(
                            label="StockItem @type",
                            item=StockItem(path=[
                                PathNode(name='form'),
                                PathNode(name='balance:question-id'),
                                PathNode(name='@type'),
                            ], ),
                            selected=True,
                        ),
                        StockFormExportColumn(
                            label="StockItem @quantity",
                            item=StockItem(path=[
                                PathNode(name='form'),
                                PathNode(name='balance:question-id'),
                                PathNode(name='entry'),
                                PathNode(name='@quantity'),
                            ], ),
                            selected=True,
                        ),
                    ])
            ])

        assert_instance_gives_results(
            docs, export_instance, {
                'My table': {
                    'headers': ['StockItem @type', 'StockItem @quantity'],
                    'rows': [
                        ['question-id', '2'],
                        ['question-id', '2'],
                        [MISSING_VALUE, MISSING_VALUE],
                        [MISSING_VALUE, MISSING_VALUE],
                    ],
                }
            })
        self.assertTrue(export_save.called)