def test_template_function_call_in_for_loop(self): expressions = [ 'for="item in object.list"', "item.test", "format_number(item.var)", "/for", ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) user_data = { "object": Mock( list=[Mock(var=32.123, test="2.3"), Mock(var=43.2, test="1.0")] ) } json_dict = res.render(user_data) self.assertEqual( json_dict, { "object": { "list": [ {"var": 32.123, "test": "2.3"}, {"var": 43.2, "test": "1.0"}, ] } }, )
def test_unknown_iterable_call(self): """For loop on unknown function call: extract the entire object""" expressions = [ 'for="i, val in not_enumerate(mylist)"', 'i', 'val.var0', 'val.var2', '/for' ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) user_data = { 'mylist': [ Mock(var0='0', var1=1, var2=2.0), Mock(var0=0, var1=1.0, var2='2'), Mock(var0=0.0, var1='1', var2=2), ] } json_dict = res.render(user_data) self.assertEqual( json_dict, { 'mylist': [ user_data['mylist'][0], user_data['mylist'][1], user_data['mylist'][2], ] })
def test_function_in_for(self): expressions = [ 'for="item in object.mylist"', 'format_date(item.date)', '/for', ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({ 'object': Mock(mylist=[ Mock(date='2015-12-13'), Mock(date='2015-12-14'), Mock(date='2015-12-15'), ]) }) assert json_dict == { 'object': { 'mylist': [ {'date': '2015-12-13'}, {'date': '2015-12-14'}, {'date': '2015-12-15'}, ] } }
def test_double_for_loop_on_same_attribute(self): expressions = [ 'for="item in root.object.list"', 'item.var1', '/for', 'for="item in root.object.list"', 'item.var0', '/for', '/for' ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) user_data = { 'root': Mock(object=Mock(list=[ Mock(var0=42, var1=-42), Mock(var0=170, var1=-170), ])) } json_dict = res.render(user_data) self.assertEqual( json_dict, { 'root': { 'object': { 'list': [ dict(var0=42, var1=-42), dict(var0=170, var1=-170), ] } } })
def test_enumerate_attribute_access(self): """For loop on enumerate: only attributes used in body are extracted. Since mylist is given as an argument to enumerate, a known builtin function, Py3oConvertor should be able to recognize that val refers to the elements of mylist. """ expressions = [ 'for="i, val in enumerate(mylist)"', 'i', 'val.var0', 'val.var2', '/for' ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) user_data = { 'mylist': [ Mock(var0='0', var1=1, var2=2.0), Mock(var0=0, var1=1.0, var2='2'), Mock(var0=0.0, var1='1', var2=2), ] } json_dict = res.render(user_data) self.assertEqual( json_dict, { 'mylist': [ dict(var0='0', var2=2.0), dict(var0=0, var2='2'), dict(var0=0.0, var2=2), ] })
def test_function_in_for(self): expressions = [ 'for="item in object.mylist"', "format_date(item.date)", "/for", ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render( { "object": Mock( mylist=[ Mock(date="2015-12-13"), Mock(date="2015-12-14"), Mock(date="2015-12-15"), ] ) } ) assert json_dict == { "object": { "mylist": [ {"date": "2015-12-13"}, {"date": "2015-12-14"}, {"date": "2015-12-15"}, ] } }
def test_convertor(self): source_odt_filename = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_if_parser.odt') outfilename = get_secure_filename() template = Template(source_odt_filename, outfilename) user_vars = template.get_user_variables() expressions = template.get_all_user_python_expression() py_expression = template.convert_py3o_to_python_ast(expressions) convertor = Py3oConvertor() data_struct = convertor(py_expression) assert 'objects' in data_struct objs = data_struct['objects'] assert 'company_label' in objs assert 'name' in objs assert isinstance(objs['company_label'], Py3oName) assert isinstance(objs['name'], Py3oName) # assert data_struct == Py3oModule( # { # 'objects': Py3oArray( # { # 'company_label': Py3oName({}), # 'name': Py3oName({}) # } # ) # } # ) expected_vars = [ 'registration.name', 'registration.company_label', ] assert set(user_vars) == set(expected_vars)
def test_unknown_iterable_call(self): """For loop on unknown function call: extract the entire object""" expressions = [ 'for="i, val in not_enumerate(mylist)"', "i", "val.var0", "val.var2", "/for", ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) user_data = { "mylist": [ Mock(var0="0", var1=1, var2=2.0), Mock(var0=0, var1=1.0, var2="2"), Mock(var0=0.0, var1="1", var2=2), ] } json_dict = res.render(user_data) self.assertEqual( json_dict, { "mylist": [ user_data["mylist"][0], user_data["mylist"][1], user_data["mylist"][2], ] }, )
def test_template_function_call_in_for_loop(self): expressions = [ 'for="item in object.list"', 'item.test', 'format_number(item.var)', '/for', ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) user_data = { 'object': Mock(list=[ Mock(var=32.123, test='2.3'), Mock(var=43.2, test='1.0') ]) } json_dict = res.render(user_data) self.assertEqual( json_dict, { 'object': { 'list': [ { 'var': 32.123, 'test': '2.3' }, { 'var': 43.2, 'test': '1.0' }, ] } })
def test_if_in_for(self): expressions = [ 'for="item in object.mylist"', 'if="item.mytest"', "item.myvar", "item.myvar2", "/if", "/for", ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render( { "object": Mock( mylist=[ Mock(mytest=0, myvar=1, myvar2=2), Mock(mytest=3, myvar=4, myvar2=5), Mock(mytest=6, myvar=7, myvar2=8), ] ) } ) assert json_dict == { "object": { "mylist": [ {"mytest": 0, "myvar": 1, "myvar2": 2}, {"mytest": 3, "myvar": 4, "myvar2": 5}, {"mytest": 6, "myvar": 7, "myvar2": 8}, ] } }
def test_if_in_for(self): expressions = [ 'for="item in object.mylist"', 'if="item.mytest"', 'item.myvar', 'item.myvar2', '/if', '/for', ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({ 'object': Mock(mylist=[ Mock(mytest=0, myvar=1, myvar2=2), Mock(mytest=3, myvar=4, myvar2=5), Mock(mytest=6, myvar=7, myvar2=8), ]) }) assert json_dict == { 'object': { 'mylist': [ {'mytest': 0, 'myvar': 1, 'myvar2': 2}, {'mytest': 3, 'myvar': 4, 'myvar2': 5}, {'mytest': 6, 'myvar': 7, 'myvar2': 8}, ] } }
def test_function_keywords(self): expressions = ["myfunc(object.var, k=object.k)"] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({"object": Mock(var=0, k=1)}) assert json_dict == {"object": {"var": 0, "k": 1}}
def test_function_array(self): expressions = ['for="var in myarray"', "myfunc(var)", "/for"] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({"myarray": [0, 1, 2, 3]}) assert json_dict == {"myarray": [0, 1, 2, 3]}
def test_if_comparator_literal(self): expressions = ['if="a == 1"', "/if"] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({"a": 1}) self.assertEqual(json_dict, {"a": 1})
def test_if_comparator(self): expressions = ['if="a in b"', "/if"] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({"a": 1, "b": [0, 1, 2]}) assert json_dict == {"a": 1, "b": [0, 1, 2]}
def test_if_global(self): expressions = ['if="mytest"', "item.myvar", "item.myvar2", "/if"] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({"item": Mock(myvar=1, myvar2=2), "mytest": 0}) assert json_dict == {"mytest": 0, "item": {"myvar": 1, "myvar2": 2}}
def __load_and_convert_template(self, path): template_xml = pkg_resources.resource_filename( 'py3o.template', path ) t = Template(template_xml, get_secure_filename()) expressions = t.get_all_user_python_expression() py_expr = t.convert_py3o_to_python_ast(expressions) return py_expr
def test_empty_for_loop(self): u"""Test ast extraction on for loops whose body do not use any data""" expressions = ['for="var in myarray"', "/for"] py_expr = Template.convert_py3o_to_python_ast(expressions) self.assertEqual(py_expr.strip(), "for var in myarray:\n pass") p = Py3oConvertor() res = p(py_expr) data = {"myarray": [0, 1, 2, 3]} json_dict = res.render(data) self.assertEqual(json_dict, data)
def test_unpack_from_data_source(self): expressions = ['for="a, b in myarray"', "a", "b.c", "/for"] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) data = {"myarray": [(1, Mock(c=2, d=3)), (4, Mock(c=5, d=6))]} json_dict = res.render(data) self.assertEqual( json_dict, {"myarray": [[1, {"c": 2}], [4, {"c": 5}]]} )
def test_function_keywords(self): expressions = [ 'myfunc(object.var, k=object.k)', ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({ 'object': Mock(var=0, k=1), }) assert json_dict == {'object': {'var': 0, 'k': 1}}
def test_if_global_array(self): expressions = [ 'for="var in myarray"', 'if="var"', "var", "/if", "/for", ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({"myarray": list(range(0, 5))}) assert json_dict == {"myarray": list(range(0, 5))}
def test_if_comparator(self): expressions = ['if="a in b"', '/if'] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({ 'a': 1, 'b': [0, 1, 2], }) assert json_dict == { 'a': 1, 'b': [0, 1, 2], }
def test_if(self): expressions = [ 'if="item.mytest"', 'item.myvar', 'item.myvar2', '/if', ] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) json_dict = res.render({ 'item': Mock(mytest=0, myvar=1, myvar2=2), }) assert json_dict == { 'item': {'mytest': 0, 'myvar': 1, 'myvar2': 2}, }
def test_calc(self): """Test date source extraction in ods files""" source_ods_filename = pkg_resources.resource_filename( "py3o.template", "tests/templates/py3o_simple_calc.ods" ) outfilename = get_secure_filename() template = Template(source_ods_filename, outfilename) expressions = template.get_all_user_python_expression() expected_expressions = [ 'for="item in items"', "item.col1", "item.col2", "item.col3", "item.col4", "/for", ] self.assertEqual(expressions, expected_expressions) py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) user_data = { "items": [ Mock(col0="0", col1=1, col2=2.0, col3="?", col4="!"), Mock(col0=0, col1=1.0, col2="2", col3="?", col4="!"), Mock(col0=0.0, col1="1", col2=2, col3="?", col4="!"), ] } json_dict = res.render(user_data) self.assertEqual( json_dict, { "items": [ dict(col1=1, col2=2.0, col3="?", col4="!"), dict(col1=1.0, col2="2", col3="?", col4="!"), dict(col1="1", col2=2, col3="?", col4="!"), ] }, )
def test_calc(self): """Test date source extraction in ods files""" source_ods_filename = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_simple_calc.ods') outfilename = get_secure_filename() template = Template(source_ods_filename, outfilename) expressions = template.get_all_user_python_expression() expected_expressions = [ 'for="item in items"', 'item.col1', 'item.col2', 'item.col3', 'item.col4', '/for', ] self.assertEqual(expressions, expected_expressions) py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) user_data = { 'items': [ Mock(col0='0', col1=1, col2=2.0, col3='?', col4='!'), Mock(col0=0, col1=1.0, col2='2', col3='?', col4='!'), Mock(col0=0.0, col1='1', col2=2, col3='?', col4='!'), ] } json_dict = res.render(user_data) self.assertEqual( json_dict, { 'items': [ dict(col1=1, col2=2.0, col3='?', col4='!'), dict(col1=1.0, col2='2', col3='?', col4='!'), dict(col1='1', col2=2, col3='?', col4='!'), ] })
def test_unpack_from_data_source(self): expressions = ['for="a, b in myarray"', 'a', 'b.c', '/for'] py_expr = Template.convert_py3o_to_python_ast(expressions) p = Py3oConvertor() res = p(py_expr) data = { 'myarray': [ (1, Mock(c=2, d=3)), (4, Mock(c=5, d=6)), ] } json_dict = res.render(data) self.assertEqual(json_dict, {'myarray': [ [1, { 'c': 2 }], [4, { 'c': 5 }], ]})