From: Raphael Michel Date: Wed, 18 May 2022 14:30:13 +0000 (+0200) Subject: Add diff output to test_roundtip X-Git-Tag: 2.2.0~1^2~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fe11b354457f56c3e14773481927cf961fee0ca1;p=thirdparty%2Fpython-drafthorse.git Add diff output to test_roundtip --- diff --git a/tests/test_roundtrip.py b/tests/test_roundtrip.py index 52d13dd..22691c0 100644 --- a/tests/test_roundtrip.py +++ b/tests/test_roundtrip.py @@ -1,13 +1,21 @@ import os +import sys +from difflib import unified_diff import pytest +import lxml.etree from drafthorse.models.document import Invoice -from drafthorse.utils import prettify +from drafthorse.utils import prettify, validate_xml samples = os.listdir(os.path.join(os.path.dirname(__file__), 'samples')) +def _diff_xml(a, b): + for line in unified_diff(a.splitlines(), b.splitlines()): + print(line) + + @pytest.mark.parametrize("filename", samples) def test_sample_roundtrip(filename): origxml = prettify( @@ -15,8 +23,25 @@ def test_sample_roundtrip(filename): remove_comments=True ) schema = 'FACTUR-X_' + filename.split('_')[2] + + # Validate that the sample file is valid, otherwise the test is moot + validate_xml(xmlout=origxml, schema=schema) + + # Parse the sample file into our internal python structure doc = Invoice.parse(origxml) - generatedxml = prettify(doc.serialize(schema)) - generatedxml = b"\n".join(generatedxml.split(b"\n")[1:]) # skip first line (namespace order…) - origxml = b"\n".join(origxml.split(b"\n")[1:]) # skip first line (namespace order…) - assert origxml.decode().strip() == generatedxml.decode().strip() + + # Validate output XML and render a diff for debugging + # skip first line (namespace order…) + origxml = b"\n".join(origxml.split(b"\n")[1:]).decode().strip() + try: + generatedxml = prettify(doc.serialize(schema)) + generatedxml = b"\n".join(generatedxml.split(b"\n")[1:]).decode().strip() + _diff_xml(origxml, generatedxml) + except lxml.etree.XMLSyntaxError: + generatedxml = prettify(doc.serialize(None)) + generatedxml = b"\n".join(generatedxml.split(b"\n")[1:]).decode().strip() + _diff_xml(origxml, generatedxml) + raise + + # Compare output XML + assert origxml == generatedxml