def setUpClass(cls): super(TestConvertStockFormExport, cls).setUpClass() cls.schema = FormExportDataSchema( domain=cls.domain, group_schemas=[ ExportGroupSchema( path=MAIN_TABLE, items=[ StockItem( path=[ PathNode(name='form'), PathNode(name='transfer:questionid'), PathNode(name='entry'), PathNode(name='@id'), ], label='Question 1', last_occurrences={cls.app_id: 3}, ), ], last_occurrences={cls.app_id: 2}, ), ExportGroupSchema( path=[ PathNode(name='form'), PathNode(name='repeat', is_repeat=True) ], items=[ StockItem( path=[ PathNode(name='form'), PathNode(name='repeat', is_repeat=True), PathNode(name='transfer:questionid'), PathNode(name='entry'), PathNode(name='@id'), ], label='Question 1', last_occurrences={cls.app_id: 3}, ), ], last_occurrences={cls.app_id: 2}, ), ])
def setUpClass(cls): super(TestFormExportInstanceGeneration, cls).setUpClass() cls.app_id = '1234' cls.schema = FormExportDataSchema(group_schemas=[ ExportGroupSchema( path=MAIN_TABLE, items=[ MultipleChoiceItem( path=[ PathNode(name='data'), PathNode(name='question1') ], label='Question 1', last_occurrences={cls.app_id: 3}, ), StockItem( path=[ PathNode(name='data'), PathNode(name='balance:question-id'), PathNode(name='@type'), ], label='Stock 1', last_occurrences={cls.app_id: 3}, ) ], last_occurrences={cls.app_id: 3}, ), ExportGroupSchema( path=[ PathNode(name='data'), PathNode(name='repeat', is_repeat=True) ], items=[ ExportItem( path=[ PathNode(name='data'), PathNode(name='repeat', is_repeat=True), PathNode(name='q2') ], label='Question 2', last_occurrences={cls.app_id: 2}, ), ], last_occurrences={cls.app_id: 2}, ), ], )
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], ], } } )
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)