]> git.ipfire.org Git - thirdparty/python-drafthorse.git/commitdiff
Add diff output to test_roundtip
authorRaphael Michel <mail@raphaelmichel.de>
Wed, 18 May 2022 14:30:13 +0000 (16:30 +0200)
committerRaphael Michel <mail@raphaelmichel.de>
Wed, 18 May 2022 14:30:13 +0000 (16:30 +0200)
tests/test_roundtrip.py

index 52d13dd758dabb9f62bdeda44c45d9881d24e5cf..22691c03675fe4899e04c86f8456aa4edaa94d97 100644 (file)
@@ -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