tests: factorize xml tree helper (#83897)

This commit is contained in:
Benjamin Dauvergne 2024-02-20 15:31:08 +01:00
parent a4b55ca1a8
commit caa4309db9
1 changed files with 15 additions and 18 deletions

View File

@ -34,6 +34,19 @@ def test_to_pdfa(fake_invoice_bytes):
to_pdfa(fake_invoice_bytes)
def to_xml_tree(xml_str):
root = ET.fromstring(xml_str)
def helper(root):
tag = root.tag.split('}')[-1]
if len(root) == 0:
return [tag, root.text or '']
else:
return [tag] + [helper(node) for node in root]
return helper(root)
def test_add_facturx_from_bytes(fake_invoice_bytes):
facturx_ctx = {
'numero_de_facture': 'F20190001',
@ -54,16 +67,8 @@ def test_add_facturx_from_bytes(fake_invoice_bytes):
}
facturx_bytes = add_facturx_from_bytes(fake_invoice_bytes, facturx_ctx)
_, xml_str = facturx.get_facturx_xml_from_pdf(io.BytesIO(facturx_bytes))
root = ET.fromstring(xml_str)
def helper(root):
tag = root.tag.split('}')[-1]
if len(root) == 0:
return [tag, root.text or '']
else:
return [tag] + [helper(node) for node in root]
assert helper(root) == [
assert to_xml_tree(xml_str) == [
'CrossIndustryInvoice',
[
'ExchangedDocumentContext',
@ -145,16 +150,8 @@ def test_add_facturx_from_bytes_facture_avoir(fake_invoice_bytes):
}
facturx_bytes = add_facturx_from_bytes(fake_invoice_bytes, facturx_ctx)
_, xml_str = facturx.get_facturx_xml_from_pdf(io.BytesIO(facturx_bytes))
root = ET.fromstring(xml_str)
def helper(root):
tag = root.tag.split('}')[-1]
if len(root) == 0:
return [tag, root.text or '']
else:
return [tag] + [helper(node) for node in root]
assert helper(root) == [
assert to_xml_tree(xml_str) == [
'CrossIndustryInvoice',
[
'ExchangedDocumentContext',