se adicionan mas pruebas a facho xml
FossilOrigin-Name: 1425e28d9ca9a0025dacd40fc57fff5cf3bbf67d0fc8482899502b996a0d41f9
This commit is contained in:
		| @@ -111,9 +111,19 @@ class LXMLBuilder: | |||||||
|     def get_attribute(self, elem, key): |     def get_attribute(self, elem, key): | ||||||
|         return elem.attrib[key] |         return elem.attrib[key] | ||||||
|  |  | ||||||
|  |     def is_attribute(self, elem, key, value): | ||||||
|  |         return elem.get(key, False) == value | ||||||
|  |  | ||||||
|     def set_attribute(self,  elem, key, value): |     def set_attribute(self,  elem, key, value): | ||||||
|         elem.attrib[key] = value |         elem.attrib[key] = value | ||||||
|  |  | ||||||
|  |     def remove_attributes(self, elem, keys): | ||||||
|  |         for key in keys: | ||||||
|  |             try: | ||||||
|  |                 del elem.attrib[key] | ||||||
|  |             except KeyError: | ||||||
|  |                 pass | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def tostring(self, oelem, **attrs): |     def tostring(self, oelem, **attrs): | ||||||
|         elem = deepcopy(oelem) |         elem = deepcopy(oelem) | ||||||
| @@ -122,6 +132,11 @@ class LXMLBuilder: | |||||||
|         attrs['encoding'] = attrs.pop('encoding', 'UTF-8') |         attrs['encoding'] = attrs.pop('encoding', 'UTF-8') | ||||||
|  |  | ||||||
|         for el in elem.getiterator(): |         for el in elem.getiterator(): | ||||||
|  |             try: | ||||||
|  |                 del el.attrib['facho_placeholder'] | ||||||
|  |             except KeyError: | ||||||
|  |                 pass | ||||||
|  |  | ||||||
|             is_optional = el.get('facho_optional', 'False') == 'True' |             is_optional = el.get('facho_optional', 'False') == 'True' | ||||||
|             if is_optional and el.getchildren() == [] and el.keys() == ['facho_optional']: |             if is_optional and el.getchildren() == [] and el.keys() == ['facho_optional']: | ||||||
|                 el.getparent().remove(el) |                 el.getparent().remove(el) | ||||||
| @@ -195,6 +210,7 @@ class FachoXML: | |||||||
|         elem = self.find_or_create_element(xpath, append) |         elem = self.find_or_create_element(xpath, append) | ||||||
|         if optional: |         if optional: | ||||||
|             elem.set('facho_optional', 'True') |             elem.set('facho_optional', 'True') | ||||||
|  |         elem.set('facho_placeholder', 'True') | ||||||
|         return elem |         return elem | ||||||
|  |  | ||||||
|     def replacement_for(self, xpath, new_xpath, content, **attrs): |     def replacement_for(self, xpath, new_xpath, content, **attrs): | ||||||
| @@ -254,25 +270,27 @@ class FachoXML: | |||||||
|  |  | ||||||
|         # se fuerza la adicion como un nuevo elemento |         # se fuerza la adicion como un nuevo elemento | ||||||
|         if append: |         if append: | ||||||
|             last_slibing = current_elem |             last_slibing = None | ||||||
|             for child in parent.getchildren(): |             for child in parent.getchildren(): | ||||||
|                 if child.tag == node_tag: |                 if child.tag == node_tag: | ||||||
|                     last_slibing = child |                     last_slibing = child | ||||||
|  |  | ||||||
|             node = self.builder.build_from_expression(node_tag) |             # si no ahi primos se adiciona como hijo | ||||||
|  |             if last_slibing is None: | ||||||
|  |                 self.builder.append(parent, node) | ||||||
|  |                 return node | ||||||
|  |  | ||||||
|  |             if self.builder.is_attribute(last_slibing, 'facho_placeholder', 'True'): | ||||||
|  |                 self._remove_facho_attributes(last_slibing) | ||||||
|  |                 return last_slibing  | ||||||
|             self.builder.append_next(last_slibing, node) |             self.builder.append_next(last_slibing, node) | ||||||
|             return node |             return node | ||||||
|  |  | ||||||
|         try: |  | ||||||
|             # TODO(bit4bit) acoplamiento indirecto a placeholder |  | ||||||
|             del current_elem.attrib['facho_optional'] |  | ||||||
|         except KeyError: |  | ||||||
|             pass |  | ||||||
|  |  | ||||||
|         if child is None: |         if child is None: | ||||||
|             self.builder.append(current_elem, node) |             self.builder.append(current_elem, node) | ||||||
|             current_elem = node |             return node | ||||||
|  |  | ||||||
|  |         self._remove_facho_attributes(current_elem) | ||||||
|         return current_elem |         return current_elem | ||||||
|  |  | ||||||
|     def set_element_validator(self, xpath, validator = False): |     def set_element_validator(self, xpath, validator = False): | ||||||
| @@ -309,7 +327,9 @@ class FachoXML: | |||||||
|         if content: |         if content: | ||||||
|             self.builder.set_text(elem, format_ % content) |             self.builder.set_text(elem, format_ % content) | ||||||
|         for k, v in attrs.items(): |         for k, v in attrs.items(): | ||||||
|             self.builder.set_attribute(elem, k, v) |             if v is not None or str(v) != 'None': | ||||||
|  |                 self.builder.set_attribute(elem, k, str(v)) | ||||||
|  |  | ||||||
|         return elem |         return elem | ||||||
|  |  | ||||||
|     def set_attributes(self, xpath, **attrs): |     def set_attributes(self, xpath, **attrs): | ||||||
| @@ -331,6 +351,7 @@ class FachoXML: | |||||||
|  |  | ||||||
|     def get_element_attribute(self, xpath, attribute): |     def get_element_attribute(self, xpath, attribute): | ||||||
|         elem = self.get_element(xpath) |         elem = self.get_element(xpath) | ||||||
|  |         print(elem.attrib) | ||||||
|         return self.builder.get_attribute(elem, attribute) |         return self.builder.get_attribute(elem, attribute) | ||||||
|  |  | ||||||
|     def get_element(self, xpath): |     def get_element(self, xpath): | ||||||
| @@ -343,6 +364,9 @@ class FachoXML: | |||||||
|         text = self.builder.get_text(elem) |         text = self.builder.get_text(elem) | ||||||
|         return format_(text) |         return format_(text) | ||||||
|  |  | ||||||
|  |     def _remove_facho_attributes(self, elem): | ||||||
|  |         self.builder.remove_attributes(elem, ['facho_optional', 'facho_placeholder']) | ||||||
|  |  | ||||||
|     def tostring(self, **kw): |     def tostring(self, **kw): | ||||||
|         return self.builder.tostring(self.root, **kw) |         return self.builder.tostring(self.root, **kw) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -319,3 +319,35 @@ def test_facho_xml_fragment_create_on_first_append(): | |||||||
|     A = xml.fragment('./A', append=True) |     A = xml.fragment('./A', append=True) | ||||||
|     A.find_or_create_element('./C') |     A.find_or_create_element('./C') | ||||||
|     assert xml.tostring() == '<root><A><B/></A><A><C/></A></root>' |     assert xml.tostring() == '<root><A><B/></A><A><C/></A></root>' | ||||||
|  |  | ||||||
|  | def test_facho_xml_placeholder_optional_and_fragment(): | ||||||
|  |     xml = facho.FachoXML('root') | ||||||
|  |  | ||||||
|  |     xml.placeholder_for('./A/AA') | ||||||
|  |  | ||||||
|  |     A = xml.fragment('./A', append_not_exists=True) | ||||||
|  |     A.find_or_create_element('./AA/B') | ||||||
|  |  | ||||||
|  |     A = xml.fragment('./A', append_not_exists=True) | ||||||
|  |     A.find_or_create_element('./AA/C') | ||||||
|  |  | ||||||
|  |     assert xml.tostring() == '<root><A><AA><B/><C/></AA></A></root>' | ||||||
|  |  | ||||||
|  | def test_facho_xml_placeholder_optional_and_set_attributes(): | ||||||
|  |     xml = facho.FachoXML('root') | ||||||
|  |     xml.placeholder_for('./A') | ||||||
|  |  | ||||||
|  |     xml.set_attributes('/root/A', prueba='OK') | ||||||
|  |     assert xml.get_element_attribute('/root/A', 'prueba') == 'OK' | ||||||
|  |     assert xml.tostring() == '<root><A prueba="OK"/></root>' | ||||||
|  |  | ||||||
|  | def test_facho_xml_placeholder_optional_and_fragment_with_set_element(): | ||||||
|  |     xml = facho.FachoXML('root') | ||||||
|  |  | ||||||
|  |     xml.placeholder_for('./A/AA') | ||||||
|  |  | ||||||
|  |     A = xml.fragment('./A', append_not_exists=True) | ||||||
|  |     A.set_element('./AA', None, append_=True, prueba='OK') | ||||||
|  |  | ||||||
|  |     assert xml.tostring() == '<root><A><AA prueba="OK"/></A></root>' | ||||||
|  |     assert xml.get_element_attribute('/root/A/AA', 'prueba') == 'OK' | ||||||
|   | |||||||
| @@ -21,7 +21,7 @@ def test_adicionar_devengado_Basico(): | |||||||
|     assert xml.get_element_attribute('/fe:NominaIndividual/Devengados/Basico', 'DiasTrabajados') == '30' |     assert xml.get_element_attribute('/fe:NominaIndividual/Devengados/Basico', 'DiasTrabajados') == '30' | ||||||
|     assert xml.get_element_attribute('/fe:NominaIndividual/Devengados/Basico', 'SueldoTrabajado') == '1000000.0' |     assert xml.get_element_attribute('/fe:NominaIndividual/Devengados/Basico', 'SueldoTrabajado') == '1000000.0' | ||||||
|  |  | ||||||
| def test_adicionar_devengado_transporte(): | def atest_adicionar_devengado_transporte(): | ||||||
|     nomina = fe.nomina.DIANNominaIndividual() |     nomina = fe.nomina.DIANNominaIndividual() | ||||||
|  |  | ||||||
|     nomina.adicionar_devengado(fe.nomina.DevengadoTransporte( |     nomina.adicionar_devengado(fe.nomina.DevengadoTransporte( | ||||||
| @@ -29,9 +29,10 @@ def test_adicionar_devengado_transporte(): | |||||||
|     )) |     )) | ||||||
|  |  | ||||||
|     xml = nomina.toFachoXML() |     xml = nomina.toFachoXML() | ||||||
|  |  | ||||||
|     assert xml.get_element_attribute('/fe:NominaIndividual/Devengados/Transporte', 'AuxilioTransporte') == '2000000.0' |     assert xml.get_element_attribute('/fe:NominaIndividual/Devengados/Transporte', 'AuxilioTransporte') == '2000000.0' | ||||||
|  |  | ||||||
| def atest_adicionar_devengado_transporte_muchos(): | def test_adicionar_devengado_transporte_muchos(): | ||||||
|     nomina = fe.nomina.DIANNominaIndividual() |     nomina = fe.nomina.DIANNominaIndividual() | ||||||
|  |  | ||||||
|     nomina.adicionar_devengado(fe.nomina.DevengadoTransporte( |     nomina.adicionar_devengado(fe.nomina.DevengadoTransporte( | ||||||
| @@ -44,7 +45,5 @@ def atest_adicionar_devengado_transporte_muchos(): | |||||||
|  |  | ||||||
|     xml = nomina.toFachoXML() |     xml = nomina.toFachoXML() | ||||||
|     print(xml) |     print(xml) | ||||||
|     assert str(xml) == """ |     assert str(xml) == """<NominaIndividual xmlns:facho="http://git.disroot.org/Etrivial/facho" xmlns="http://www.dian.gov.co/contratos/facturaelectronica/v1" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cdt="urn:DocumentInformation:names:specification:ubl:colombia:schema:xsd:DocumentInformationAggregateComponents-1" xmlns:clm54217="urn:un:unece:uncefact:codelist:specification:54217:2001" xmlns:clmIANAMIMEMediaType="urn:un:unece:uncefact:codelist:specification:IANAMIMEMediaType:2003" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:sts="dian:gov:co:facturaelectronica:Structures-2-1" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:sig="http://www.w3.org/2000/09/xmldsig#"><Devengados><Basico/><Transporte AuxilioTransporte="2000000.0"/><Transporte AuxilioTransporte="3000000.0"/></Devengados></NominaIndividual>""" | ||||||
| <NominaIndividual xmlns:facho="http://git.disroot.org/Etrivial/facho" xmlns="http://www.dian.gov.co/contratos/facturaelectronica/v1" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cdt="urn:DocumentInformation:names:specification:ubl:colombia:schema:xsd:DocumentInformationAggregateComponents-1" xmlns:clm54217="urn:un:unece:uncefact:codelist:specification:54217:2001" xmlns:clmIANAMIMEMediaType="urn:un:unece:uncefact:codelist:specification:IANAMIMEMediaType:2003" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:sts="dian:gov:co:facturaelectronica:Structures-2-1" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:sig="http://www.w3.org/2000/09/xmldsig#"><Devengados><Basico/><Transporte facho_optional="True" AuxilioTransporte="2000000.0"/><Transporte facho_optional="True" AuxilioTransporte="3000000.0"/></Devengados></NominaIndividual> |  | ||||||
| """ |  | ||||||
|    |    | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user