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']], } })
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)
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] )
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"])
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"])
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])
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')
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'])
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], )
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_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']], } } )
def test_transform_dates(self): """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, ) ]) ]) 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'Date'], u'rows': [[MISSING_VALUE], [ couch_to_excel_datetime( '2015-07-22T14:16:49.584880Z', None) ]], } })
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)
def test_wrap_export_item_child(self, _): path = [PathNode(name="foo"), PathNode(name="bar")] item = MultipleChoiceItem(path=path, options=[Option(value="foo")]) wrapped = ExportItem.wrap(item.to_json()) self.assertEqual(type(wrapped), type(item)) self.assertEqual(wrapped.to_json(), item.to_json())
def test_wrap_export_item_child(self, _): path = [PathNode(name="foo"), PathNode(name="bar")] item = MultipleChoiceItem(path=path, options=[Option(value="foo")]) wrapped = ExportItem.wrap(item.to_json()) self.assertEqual(type(wrapped), type(item)) self.assertEqual(wrapped.to_json(), item.to_json())