def _test_cli(self, args, expected): writer = JValueTableWriter() with mock.patch('commcare_export.cli._get_writer', return_value=writer): main_with_args(args) for table in expected: assert writer.tables[table['name']] == TableSpec(**table)
def test_emit_table_unwrap_dicts(self): writer = JValueTableWriter() env = EmitterEnv(writer) env.emit_table( TableSpec( **{ 'name': 't1', 'headings': ['a'], 'rows': [ ['hi'], [{ '#text': 'test_text', '@case_type': 'person', '@relationship': 'child', 'id': 'nothing' }], [{ '@case_type': '', '@relationship': 'child', 'id': 'some_id' }], [{ 't': 123 }], ] })) writer.tables['t1'].rows = [['hi'], ['test_text'], [''], [{'t': 123}]]
def test_emit_env_generator(self): class TestEmitterEnv(EmitterEnv): def emit_table(self, table_spec): self.table = table_spec env = TestEmitterEnv(JValueTableWriter()) self._setup_emit_test(env) assert isinstance(env.table.rows, (map, filter, types.GeneratorType))
def test_emit_table_unwrap_dicts(self): writer = JValueTableWriter() env = EmitterEnv(writer) env.emit_table({ 'name': 't1', 'headings': ['a'], 'rows':[ ['hi'], [{'#text': 'test_text','@case_type': 'person','@relationship': 'child','id': 'nothing'}], [{'@case_type': '', '@relationship': 'child', 'id': 'some_id'}], [{'t': 123}], ] }) writer.tables['t1']['rows'] = [ ['hi'], ['test_text'], [''], [{'t': 123}] ]
def test_emit_multi_same_query(self): """Test that we can emit multiple tables from the same set of source data. This is useful if you need to generate multiple tables from the same datasource. """ writer = JValueTableWriter() env = BuiltInEnv() | JsonPathEnv() | EmitterEnv(writer) result = Map( source=Literal([ { 'foo': { 'baz': 3, 'bar': True, 'boo': None } }, { 'foo': { 'baz': 4, 'bar': False, 'boo': 1 } }, ]), body=List([ Emit( table='FooBaz', headings=[Literal('foo')], source=List([List([Reference('foo.baz')])]), ), Emit( table='FooBar', headings=[Literal('foo')], source=List([List([Reference('foo.bar')])]), ) ]), ).eval(env) # evaluate result list(result) assert 2 == len(writer.tables) assert writer.tables['FooBaz'].rows == [[3], [4]] assert writer.tables['FooBar'].rows == [[True], [False]]
def test_emit(self): writer = JValueTableWriter() env = BuiltInEnv() | JsonPathEnv( {'foo': { 'baz': 3, 'bar': True, 'boo': None }}) | EmitterEnv(writer) Emit(table='Foo', headings=[Literal('foo')], source=List([ List([ Reference('foo.baz'), Reference('foo.bar'), Reference('foo.foo'), Reference('foo.boo') ]) ]), missing_value='---').eval(env) assert list(writer.tables['Foo'].rows) == [[3, True, '---', None]]
def test_JValueTableWriter(self): writer = JValueTableWriter() writer.write_table( TableSpec( **{ 'name': 'foo', 'headings': ['a', 'bjørn', 'c', 'd'], 'rows': [ [1, '2', 3, datetime.date(2015, 1, 1)], [4, '日本', 6, datetime.date(2015, 1, 2)], ] })) writer.write_table( TableSpec( **{ 'name': 'foo', 'headings': ['a', 'bjørn', 'c', 'd'], 'rows': [ [5, 'bob', 9, datetime.date(2018, 1, 2)], ] })) assert writer.tables == { 'foo': TableSpec( **{ 'name': 'foo', 'headings': ['a', 'bjørn', 'c', 'd'], 'rows': [ [1, '2', 3, '2015-01-01'], [4, '日本', 6, '2015-01-02'], [5, 'bob', 9, '2018-01-02'], ], }) }
def test_emit_mutli_different_query(self): """Test that we can emit multiple tables from the same set of source data even if the emitted table have different 'root doc' expressions. Example use case could be emitting cases and case actions, or form data and repeats. """ writer = JValueTableWriter() env = BuiltInEnv() | JsonPathEnv() | EmitterEnv(writer) result = Filter( # the filter here is to prevent accumulating a `[None]` value for each doc predicate=Apply(Reference("filter_empty"), Reference("$")), source=Map( # in practice `source` would be and api query such as # Apply(Reference('api_data'), Literal('case'), Literal({'type': 'case_type'})) source=Literal([ { 'id': 1, 'foo': { 'baz': 3, 'bar': True, 'boo': None }, 'actions': [{ 'a': 3 }, { 'a': 4 }] }, { 'id': 2, 'foo': { 'baz': 4, 'bar': False, 'boo': 1 }, 'actions': [{ 'a': 5 }, { 'a': 6 }] }, ]), body=List([ Emit(table="t1", headings=[Literal("id")], source=Map( source=Reference("`this`"), body=List([ Reference("id"), ]), )), Emit(table="t2", headings=[Literal("id"), Literal("a")], source=Map( source=Reference("actions[*]"), body=List([Reference("$.id"), Reference("a")]), )) ]))).eval(env) # evaluate result list(result) print(writer.tables) assert writer.tables['t1'].rows == [['1'], ['2']] assert writer.tables['t2'].rows == [['1', 3], ['1', 4], ['2', 5], ['2', 6]]
def test_emit(self): writer = JValueTableWriter() self._setup_emit_test(EmitterEnv(writer)) assert list(writer.tables['Foo'].rows) == [[3, True, '---', None]]