def test_get_user_instruction_mapping(self): """test the jsonified result of get_user_instruction_mapping""" source_odt_filename = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_example_template.odt' ) outfilename = get_secure_filename() template = Template(source_odt_filename, outfilename) for_lists, vars = template.get_user_instructions_mapping() expected_res = { 'document': { 'total': 0 }, 'items': [{ 'val1': 1, 'val2': 2, 'val3': 3, 'Amount': 4, 'Currency': 5, 'InvoiceRef': 6, }] } data = { 'document': Mock(total=0), 'items': [ Mock( val1=1, val2=2, val3=3, Amount=4, Currency=5, InvoiceRef=6 ) ] } res = ForList.to_dict(for_lists, vars, data) assert res == expected_res
def test_jsonify_empty_for_loop(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_empty_for_loop.odt') t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = {'my1list': []} res = ForList.to_dict(for_lists, variables, data) expected = {'my1list': []} assert res == expected
def get_user_instructions_mapping(self): """ Public method to get the mapping of all variables defined in the template """ instructions = self.get_user_instructions() user_variables = self.get_user_variables() # For now we just want for loops instructions = [ i for i in instructions if i.startswith('for') or i == '/for' ] # Now we call the decoder to get variable mapping from instructions d = Decoder() res = [] for_insts = {} tmp = res # Create a hierarchie with for loops for i in instructions: if i == '/for': tmp = tmp.parent else: # Decode the instruction: # inst.values() -> forloop variable # inst.keys() -> forloop iterable var, it = d.decode_py3o_instruction(i) # we keep all inst in a dict for_insts[var] = it # get the variable defined inside the for loop for_vars = [ v for v in user_variables if v.split('.')[0] == var ] # create a new ForList for the forloop and add it to the # children or list new_list = ForList(it, var) if isinstance(tmp, list): # We have a root for loop res.append(new_list) tmp = res[-1] tmp.parent = res else: tmp.add_child(new_list) tmp = new_list # Add the attributes to our new child for v in for_vars: tmp.add_attr(v) # Insert global variable in a second list user_vars = [ v for v in user_variables if not v.split('.')[0] in for_insts.keys() ] return res, user_vars
def test_jsonify_global_variable_inside_loop(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_access_global_variable_inside_loop.odt') t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = {'global_var': Mock(val='global_val')} res = ForList.to_dict(for_lists, variables, data) expected = {'global_var': {'val': 'global_val'}, 'my4list': []} assert res == expected
def test_jsonify_in_loop_variable_with_attribute(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_access_in_loop_variable_with_attribute.odt') t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = {'my3list': [Mock(val='val1'), Mock(val='val2')]} res = ForList.to_dict(for_lists, variables, data) expected = {'my3list': [{'val': 'val1'}, {'val': 'val2'}]} assert res == expected
def test_jsonify_iterator_with_attribute(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_iterator_with_attribute.odt') t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = {'global_var': Mock(my5list=[])} res = ForList.to_dict(for_lists, variables, data) expected = {'global_var': {'my5list': []}} assert res == expected
def test_jsonify_in_loop_variable_with_attribute(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_access_in_loop_variable_with_attribute.odt' ) t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = { 'my3list': [Mock(val='val1'), Mock(val='val2')] } res = ForList.to_dict(for_lists, variables, data) expected = {'my3list': [{'val': 'val1'}, {'val': 'val2'}]} assert res == expected
def test_jsonify_access_in_loop_variable(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_access_in_loop_variable.odt' ) t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = { 'my2list': ['val1', 'val2'] } res = ForList.to_dict(for_lists, variables, data) expected = {'my2list': ['val1', 'val2']} assert res == expected
def test_jsonify_iterator_with_attribute_and_in_loop_variable(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_iterator_with_attribute_and_in_loop_variable.odt' ) t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = { 'global_var': Mock(my6list=['val1', 'val2']) } res = ForList.to_dict(for_lists, variables, data) expected = {'global_var': {'my6list': ['val1', 'val2']}} assert res == expected
def test_jsonify_global_variable_inside_loop(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_access_global_variable_inside_loop.odt' ) t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = { 'global_var': Mock(val='global_val') } res = ForList.to_dict(for_lists, variables, data) expected = {'global_var': {'val': 'global_val'}, 'my4list': []} assert res == expected
def test_jsonify_iterator_with_attribute_and_in_loop_variable_with_attribute(self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_iterator_with_attribute_and_in_loop_variable_with_attribute.odt' ) t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = { 'global_var': Mock(my7list=[Mock(val='val1'), Mock(val='val2')]) } res = ForList.to_dict(for_lists, variables, data) expected = {'global_var': {'my7list': [{'val': 'val1'}, {'val': 'val2'}]}} assert res == expected # def test_jsonify_access_variable_in_nested_loop(self): # """ Test the jsonify function # """ # template_xml = pkg_resources.resource_filename( # 'py3o.template', # 'tests/templates/py3o_access_variable_in_nested_loop.odt' # ) # t = Template(template_xml, get_secure_filename()) # for_lists, variables = t.get_user_instructions_mapping() # data = { # 'my8list': [['val1', 'val2'], ['val3']] # } # res = ForList.to_dict(for_lists, variables, data) # expected = {'my8list': [['val1', 'val2'], ['val3']]} # assert res == expected # def test_jsonify_access_parent_variable_in_nested_loop(self): # """ Test the jsonify function # """ # template_xml = pkg_resources.resource_filename( # 'py3o.template', # 'tests/templates/py3o_access_parent_variable_in_nested_loop.odt' # ) # t = Template(template_xml, get_secure_filename()) # for_lists, variables = t.get_user_instructions_mapping() # data = { # 'my9list': [Mock(val='val1', mylist=[]), Mock(val='val2', mylist=[])] # } # res = ForList.jsonify(for_lists, variables, data) # expected = "{'my9list': [{'val': 'val1', 'mylist': []}, {'val': 'val2', 'mylist': []}]}" # assert res == expected # # def test_jsonify_access_variable_in_nested_loop_with_attribute(self): # """ Test the jsonify function # """ # template_xml = pkg_resources.resource_filename( # 'py3o.template', # 'tests/templates/py3o_access_variable_in_nested_loop_with_attribute.odt' # ) # t = Template(template_xml, get_secure_filename()) # for_lists, variables = t.get_user_instructions_mapping() # data = { # 'my10list': [Mock(my_list=[Mock(val='val1'), Mock(val='val2')]), Mock(my_list=[Mock(val='val3')])] # } # res = ForList.jsonify(for_lists, variables, data) # expected = "{'my10list': [{'my_list': [{'val': 'val1'}, {'val': 'val2'}]}, {'my_list': [{'val': 'val3'}]}]}" # assert res == expected
def test_jsonify_iterator_with_attribute_and_in_loop_variable_with_attribute( self): """ Test the jsonify function """ template_xml = pkg_resources.resource_filename( 'py3o.template', 'tests/templates/py3o_iterator_with_attribute_and_in_loop_variable_with_attribute.odt' ) t = Template(template_xml, get_secure_filename()) for_lists, variables = t.get_user_instructions_mapping() data = { 'global_var': Mock(my7list=[Mock(val='val1'), Mock(val='val2')]) } res = ForList.to_dict(for_lists, variables, data) expected = { 'global_var': { 'my7list': [{ 'val': 'val1' }, { 'val': 'val2' }] } } assert res == expected # def test_jsonify_access_variable_in_nested_loop(self): # """ Test the jsonify function # """ # template_xml = pkg_resources.resource_filename( # 'py3o.template', # 'tests/templates/py3o_access_variable_in_nested_loop.odt' # ) # t = Template(template_xml, get_secure_filename()) # for_lists, variables = t.get_user_instructions_mapping() # data = { # 'my8list': [['val1', 'val2'], ['val3']] # } # res = ForList.to_dict(for_lists, variables, data) # expected = {'my8list': [['val1', 'val2'], ['val3']]} # assert res == expected # def test_jsonify_access_parent_variable_in_nested_loop(self): # """ Test the jsonify function # """ # template_xml = pkg_resources.resource_filename( # 'py3o.template', # 'tests/templates/py3o_access_parent_variable_in_nested_loop.odt' # ) # t = Template(template_xml, get_secure_filename()) # for_lists, variables = t.get_user_instructions_mapping() # data = { # 'my9list': [Mock(val='val1', mylist=[]), Mock(val='val2', mylist=[])] # } # res = ForList.jsonify(for_lists, variables, data) # expected = "{'my9list': [{'val': 'val1', 'mylist': []}, {'val': 'val2', 'mylist': []}]}" # assert res == expected # # def test_jsonify_access_variable_in_nested_loop_with_attribute(self): # """ Test the jsonify function # """ # template_xml = pkg_resources.resource_filename( # 'py3o.template', # 'tests/templates/py3o_access_variable_in_nested_loop_with_attribute.odt' # ) # t = Template(template_xml, get_secure_filename()) # for_lists, variables = t.get_user_instructions_mapping() # data = { # 'my10list': [Mock(my_list=[Mock(val='val1'), Mock(val='val2')]), Mock(my_list=[Mock(val='val3')])] # } # res = ForList.jsonify(for_lists, variables, data) # expected = "{'my10list': [{'my_list': [{'val': 'val1'}, {'val': 'val2'}]}, {'my_list': [{'val': 'val3'}]}]}" # assert res == expected