Fix: Formateo PEP8 (79 cols) y limpieza flake8 en tests y examples

- Líneas >79 cols reajustadas (asserts, strings CUDE/CUDS, comentarios)
- Elimina imports y variables muertas (F401/F841), E712 (== False -> is False)
- Reordena imports en docs/conf.py
This commit is contained in:
2026-08-12 19:56:33 -05:00
parent 4a7c2e2947
commit c4a29e4a6f
13 changed files with 518 additions and 271 deletions

View File

@@ -7,10 +7,10 @@
import pytest
#from click.testing import CliRunner
# from click.testing import CliRunner
from facho import facho
#from facho import cli
# from facho import cli
def test_facho_xml():
@@ -20,18 +20,19 @@ def test_facho_xml():
invoice.text = 'Test'
assert xml.tostring() == '<root><Invoice>Test</Invoice></root>'
invoice_line = xml.find_or_create_element('/root/Invoice/Line')
xml.find_or_create_element('/root/Invoice/Line')
assert xml.tostring() == '<root><Invoice>Test<Line/></Invoice></root>'
def test_facho_xml_with_attr():
xml = facho.FachoXML('root')
invoice = xml.find_or_create_element('/root/Invoice[id=123]')
xml.find_or_create_element('/root/Invoice[id=123]')
assert xml.tostring() == '<root><Invoice id="123"/></root>'
def test_facho_xml_idempotent():
xml = facho.FachoXML('root')
invoice = xml.find_or_create_element('/root/Invoice')
xml.find_or_create_element('/root/Invoice')
assert xml.tostring() == '<root><Invoice/></root>'
xml.find_or_create_element('/root/Invoice')
@@ -46,6 +47,7 @@ def test_facho_xml_idempotent():
xml.find_or_create_element('/root/Invoice/Line')
assert xml.tostring() == '<root><Invoice><Line/></Invoice></root>'
def test_facho_xml_aliases():
xml = facho.FachoXML('root')
xml.register_alias_xpath('Invoice', '/root/Invoice')
@@ -54,31 +56,42 @@ def test_facho_xml_aliases():
invoice.text = 'Test'
assert xml.tostring() == '<root><Invoice>Test</Invoice></root>'
def test_facho_xmlns():
xml = facho.FachoXML('root', nsmap={
'ext': 'https://ext',
'sts': 'https://sts',
})
invoiceAuthorization = xml.find_or_create_element('/root/ext:UBLExtensions/ext:UBLExtension/'
'ext:ExtensionContent/sts:DianExtensions/'
'sts:InvoiceControl/sts:InvoiceAuthorization')
assert xml.tostring().strip() == '<root xmlns:ext="https://ext" xmlns:sts="https://sts"><ext:UBLExtensions>'\
'<ext:UBLExtension>'\
'<ext:ExtensionContent>'\
'<sts:DianExtensions>'\
'<sts:InvoiceControl>'\
'<sts:InvoiceAuthorization/>'\
'</sts:InvoiceControl></sts:DianExtensions></ext:ExtensionContent></ext:UBLExtension></ext:UBLExtensions></root>'
invoiceAuthorization = xml.find_or_create_element(
'/root/ext:UBLExtensions/ext:UBLExtension/'
'ext:ExtensionContent/sts:DianExtensions/'
'sts:InvoiceControl/sts:InvoiceAuthorization')
assert xml.tostring().strip() == (
'<root xmlns:ext="https://ext" xmlns:sts="https://sts">'
'<ext:UBLExtensions>'
'<ext:UBLExtension>'
'<ext:ExtensionContent>'
'<sts:DianExtensions>'
'<sts:InvoiceControl>'
'<sts:InvoiceAuthorization/>'
'</sts:InvoiceControl></sts:DianExtensions>'
'</ext:ExtensionContent></ext:UBLExtension>'
'</ext:UBLExtensions></root>')
invoiceAuthorization.text = '123456789'
assert xml.tostring().strip() == '<root xmlns:ext="https://ext" xmlns:sts="https://sts"><ext:UBLExtensions>'\
'<ext:UBLExtension>'\
'<ext:ExtensionContent>'\
'<sts:DianExtensions>'\
'<sts:InvoiceControl>'\
'<sts:InvoiceAuthorization>123456789</sts:InvoiceAuthorization>'\
'</sts:InvoiceControl></sts:DianExtensions></ext:ExtensionContent></ext:UBLExtension></ext:UBLExtensions></root>'
assert xml.tostring().strip() == (
'<root xmlns:ext="https://ext" xmlns:sts="https://sts">'
'<ext:UBLExtensions>'
'<ext:UBLExtension>'
'<ext:ExtensionContent>'
'<sts:DianExtensions>'
'<sts:InvoiceControl>'
'<sts:InvoiceAuthorization>123456789</sts:InvoiceAuthorization>'
'</sts:InvoiceControl></sts:DianExtensions>'
'</ext:ExtensionContent></ext:UBLExtension>'
'</ext:UBLExtensions></root>')
def test_facho_xmlns_idempotent():
xml = facho.FachoXML('root', nsmap={
@@ -87,16 +100,22 @@ def test_facho_xmlns_idempotent():
})
xml.find_or_create_element('/root/ext:Extension/sts:Sotoros')
assert xml.tostring() == '<root xmlns:ext="https://ext" xmlns:sts="https://sts"><ext:Extension><sts:Sotoros/></ext:Extension></root>'
assert xml.tostring() == (
'<root xmlns:ext="https://ext" xmlns:sts="https://sts">'
'<ext:Extension><sts:Sotoros/></ext:Extension></root>')
xml.find_or_create_element('/root/ext:Extension/sts:Sotoros')
assert xml.tostring() == '<root xmlns:ext="https://ext" xmlns:sts="https://sts"><ext:Extension><sts:Sotoros/></ext:Extension></root>'
assert xml.tostring() == (
'<root xmlns:ext="https://ext" xmlns:sts="https://sts">'
'<ext:Extension><sts:Sotoros/></ext:Extension></root>')
def test_facho_xml_set_element_with_format():
xml = facho.FachoXML('root')
invoice = xml.set_element('/root/Invoice', 1, format_='%02d')
xml.set_element('/root/Invoice', 1, format_='%02d')
assert xml.tostring() == '<root><Invoice>01</Invoice></root>'
def test_facho_xml_fragment():
xml = facho.FachoXML('root')
invoice = xml.fragment('/root/Invoice')
@@ -116,7 +135,10 @@ def test_facho_xml_fragments():
line = xml.fragment('/Invoice/Line', append=True)
line.set_element('/Line/Id', 3)
assert xml.tostring() == '<Invoice><Line><Id>1</Id></Line><Line><Id>2</Id></Line><Line><Id>3</Id></Line></Invoice>'
assert xml.tostring() == (
'<Invoice><Line><Id>1</Id></Line><Line><Id>2</Id></Line>'
'<Line><Id>3</Id></Line></Invoice>')
def test_facho_xml_nested_fragments():
xml = facho.FachoXML('Invoice')
@@ -128,15 +150,20 @@ def test_facho_xml_nested_fragments():
party.set_element('/Party/LastName', 'test')
assert xml.tostring() == '<Invoice><Party><Name>test</Name><Address><Line>line 1</Line></Address><LastName>test</LastName></Party></Invoice>'
assert xml.tostring() == (
'<Invoice><Party><Name>test</Name>'
'<Address><Line>line 1</Line></Address>'
'<LastName>test</LastName></Party></Invoice>')
def test_facho_xml_get_element_text_of_fragment():
xml = facho.FachoXML('root')
invoice = xml.fragment('/root/Invoice')
invoice.set_element('/Invoice/Id', 1)
assert invoice.get_element_text('/Invoice/Id') == '1'
def test_facho_xml_get_element_text():
xml = facho.FachoXML('Invoice')
xml.set_element('/Invoice/ID', 'ABC123')
@@ -147,6 +174,7 @@ def test_facho_xml_get_element_text():
line.set_element('/Line/Quantity', 5)
assert line.get_element_text('/Line/Quantity', format_=int) == 5
def test_facho_xml_get_element_text_next_child():
xml = facho.FachoXML('Invoice')
xml.set_element('/Invoice/ID', 'ABC123')
@@ -166,25 +194,32 @@ def test_facho_xml_set_element_relative():
assert xml.get_element_text('/Invoice/ID') == 'ABC123'
def test_facho_xml_set_element_relative_with_namespace():
xml = facho.FachoXML('{%s}Invoice' % ('http://www.dian.gov.co/contratos/facturaelectronica/v1'),
nsmap={'fe': 'http://www.dian.gov.co/contratos/facturaelectronica/v1'})
xml = facho.FachoXML(
'{%s}Invoice' %
('http://www.dian.gov.co/contratos/facturaelectronica/v1'),
nsmap={
'fe': 'http://www.dian.gov.co/contratos/facturaelectronica/v1'})
xml.set_element('./ID', 'ABC123')
assert xml.get_element_text('/fe:Invoice/ID') == 'ABC123'
def test_facho_xml_fragment_relative():
xml = facho.FachoXML('root')
invoice = xml.fragment('./Invoice')
invoice.set_element('./Id', 1)
assert xml.tostring() == '<root><Invoice><Id>1</Id></Invoice></root>'
def test_facho_xml_get_element_fragment_relative():
xml = facho.FachoXML('root')
invoice = xml.fragment('./Invoice')
invoice.set_element('./Id', 1)
assert invoice.get_element_text('./Id') == '1'
def test_facho_xml_replacement_for():
xml = facho.FachoXML('root')
xml.placeholder_for('./child/type')
@@ -192,37 +227,47 @@ def test_facho_xml_replacement_for():
'./child/code', 'test')
assert xml.tostring() == '<root><child><code>test</code></child></root>'
def test_facho_xml_set_element_content_invalid_validation():
xml = facho.FachoXML('root')
with pytest.raises(facho.FachoValueInvalid) as e:
with pytest.raises(facho.FachoValueInvalid):
xml.set_element_validator('./Id', lambda text, attrs: text == 'mero')
xml.set_element('./Id', 'bad')
def test_facho_xml_set_element_content_valid_validation():
xml = facho.FachoXML('root')
xml.set_element_validator('./Id', lambda text, attrs: text == 'mero')
xml.set_element('./Id', 'mero')
def test_facho_xml_set_element_attribute_invalid_validation():
xml = facho.FachoXML('root')
with pytest.raises(facho.FachoValueInvalid) as e:
xml.set_element_validator('./Id', lambda text, attrs: attrs['code'] == 'ABC')
xml.set_element('./Id', 'mero', code = 'CBA')
with pytest.raises(facho.FachoValueInvalid):
xml.set_element_validator(
'./Id', lambda text, attrs: attrs['code'] == 'ABC')
xml.set_element('./Id', 'mero', code='CBA')
def test_facho_xml_set_element_attribute_valid_validation():
xml = facho.FachoXML('root')
xml.set_element_validator('./Id', lambda text, attrs: attrs['code'] == 'ABC')
xml.set_element('./Id', 'mero', code = 'ABC')
xml.set_element_validator(
'./Id',
lambda text,
attrs: attrs['code'] == 'ABC')
xml.set_element('./Id', 'mero', code='ABC')
def test_facho_xml_get_element_attribute():
xml = facho.FachoXML('root')
xml.set_element('./Id', 'mero', code = 'ABC')
xml.set_element('./Id', 'mero', code='ABC')
assert xml.get_element_attribute('/root/Id', 'code') == 'ABC'
def test_facho_xml_keep_orden_slibing():
xml = facho.FachoXML('root')
xml.find_or_create_element('./A')
@@ -233,14 +278,16 @@ def test_facho_xml_keep_orden_slibing():
assert xml.tostring() == '<root><A/><A/><B/><B/><C/></root>'
def test_facho_xml_placeholder_optional():
xml = facho.FachoXML('root')
xml.placeholder_for('./A')
xml.placeholder_for('./B', optional=True)
xml.placeholder_for('./C')
assert xml.tostring() == '<root><A/><C/></root>'
def test_facho_xml_placeholder_append_to_optional():
xml = facho.FachoXML('root')
xml.placeholder_for('./A')
@@ -250,6 +297,7 @@ def test_facho_xml_placeholder_append_to_optional():
xml.find_or_create_element('./B')
assert xml.tostring() == '<root><A/><B/><C/></root>'
def test_facho_xml_placeholder_set_element_to_optional():
xml = facho.FachoXML('root')
xml.placeholder_for('./A')
@@ -259,6 +307,7 @@ def test_facho_xml_placeholder_set_element_to_optional():
xml.set_element('./B', '2')
assert xml.tostring() == '<root><A/><B>2</B><C/></root>'
def test_facho_xml_placeholder_set_element_to_optional_with_append():
xml = facho.FachoXML('root')
xml.placeholder_for('./A')
@@ -275,8 +324,8 @@ def test_facho_xml_set_attributes():
xml.find_or_create_element('./A')
xml.set_attributes('./A',
value1 = '1',
value2 = '2'
value1='1',
value2='2'
)
assert xml.get_element_attribute('/root/A', 'value1') == '1'
assert xml.get_element_attribute('/root/A', 'value2') == '2'
@@ -287,13 +336,14 @@ def test_facho_xml_set_attributes_not_set_optional():
xml.find_or_create_element('./A')
xml.set_attributes('./A',
value1 = None,
value2 = '2'
value1=None,
value2='2'
)
with pytest.raises(KeyError):
xml.get_element_attribute('/root/A', 'value1')
assert xml.get_element_attribute('/root/A', 'value2') == '2'
def test_facho_xml_placeholder_with_fragment():
xml = facho.FachoXML('root')
xml.placeholder_for('./A')
@@ -305,8 +355,10 @@ def test_facho_xml_placeholder_with_fragment():
AA.find_or_create_element('./B', append=True)
AA = xml.fragment('./AA/Child', append=True)
assert xml.tostring() == '<root><A/><AA><Child><B/><B/></Child><Child/></AA><AAA/></root>'
assert xml.tostring() == (
'<root><A/><AA><Child><B/><B/></Child><Child/></AA><AAA/></root>')
def test_facho_xml_create_on_first_append():
xml = facho.FachoXML('root')
@@ -314,6 +366,7 @@ def test_facho_xml_create_on_first_append():
xml.find_or_create_element('./A', append=True)
assert xml.tostring() == '<root><A/></root>'
def test_facho_xml_create_on_first_append_multiple_appends():
xml = facho.FachoXML('root')
@@ -324,6 +377,7 @@ def test_facho_xml_create_on_first_append_multiple_appends():
xml.find_or_create_element('./C', append=True)
assert xml.tostring() == '<root><B/><A/><A/><A/><C/></root>'
def test_facho_xml_fragment_create_on_first_append():
xml = facho.FachoXML('root')
@@ -333,6 +387,7 @@ def test_facho_xml_fragment_create_on_first_append():
A.find_or_create_element('./C')
assert xml.tostring() == '<root><A><B/></A><A><C/></A></root>'
def test_facho_xml_placeholder_optional_and_fragment():
xml = facho.FachoXML('root')
@@ -346,6 +401,7 @@ def test_facho_xml_placeholder_optional_and_fragment():
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')
@@ -354,6 +410,7 @@ def test_facho_xml_placeholder_optional_and_set_attributes():
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')
@@ -365,17 +422,19 @@ def test_facho_xml_placeholder_optional_and_fragment_with_set_element():
assert xml.tostring() == '<root><A><AA prueba="OK"/></A></root>'
assert xml.get_element_attribute('/root/A/AA', 'prueba') == 'OK'
def test_facho_xml_exist_element():
xml = facho.FachoXML('root')
xml.placeholder_for('./A')
assert xml.exist_element('/root/A') == False
assert xml.exist_element('/root/A') is False
assert xml.tostring() == '<root><A/></root>'
xml.find_or_create_element('./A')
assert xml.exist_element('/root/A') == True
assert xml.exist_element('/root/A')
assert xml.tostring() == '<root><A/></root>'
def test_facho_xml_query_element_text_or_attribute():
xml = facho.FachoXML('root')
@@ -384,6 +443,7 @@ def test_facho_xml_query_element_text_or_attribute():
assert xml.get_element_text_or_attribute('/root/A') == 'contenido'
assert xml.get_element_text_or_attribute('/root/A/@clave') == 'valor'
def test_facho_xml_query_element_text_or_attribute_from_fragment():
xml = facho.FachoXML('root')
@@ -392,6 +452,7 @@ def test_facho_xml_query_element_text_or_attribute_from_fragment():
assert invoice.get_element_text_or_attribute('/Invoice/A') == 'contenido'
def test_facho_xml_build_xml_absolute():
xml = facho.FachoXML('root')
@@ -400,18 +461,23 @@ def test_facho_xml_build_xml_absolute():
def test_facho_xml_build_xml_absolute_namespace():
xml = facho.FachoXML('{%s}root' % ('http://www.dian.gov.co/contratos/facturaelectronica/v1'),
nsmap={'fe': 'http://www.dian.gov.co/contratos/facturaelectronica/v1'})
xml = facho.FachoXML(
'{%s}root' %
('http://www.dian.gov.co/contratos/facturaelectronica/v1'),
nsmap={
'fe': 'http://www.dian.gov.co/contratos/facturaelectronica/v1'})
xpath = xml.xpath_from_root('/A')
assert xpath == '/fe:root/A'
def test_facho_xml_build_xml_absolute_namespace_from_fragment():
xml = facho.FachoXML('{%s}root' % ('http://www.dian.gov.co/contratos/facturaelectronica/v1'),
nsmap={'fe': 'http://www.dian.gov.co/contratos/facturaelectronica/v1'})
xml = facho.FachoXML(
'{%s}root' %
('http://www.dian.gov.co/contratos/facturaelectronica/v1'),
nsmap={
'fe': 'http://www.dian.gov.co/contratos/facturaelectronica/v1'})
invoice = xml.fragment('/root/Invoice')
xpath = invoice.xpath_from_root('/A')
assert xpath == '/fe:root/Invoice/A'