[py3] Properly handle when comparing XML in check_xsd_profile test method
authorDenis Laxalde <denis.laxalde@logilab.fr>
Wed, 02 Oct 2019 15:09:46 +0200
changeset 3045 97f0cc843868
parent 3044 fe168e4a546b
child 3046 bf353c190352
[py3] Properly handle when comparing XML in check_xsd_profile test method We use io.open() to consistently open files in text mode in both python2 and python3 (we use text mode because we need to .format() the read string, which is only possible with text on python3). Accordingly, we set encoding=text_type when dumping XML to string, in order to produce a unicode string.
test/test_profile_generation.py
--- a/test/test_profile_generation.py	Wed Oct 02 15:01:36 2019 +0200
+++ b/test/test_profile_generation.py	Wed Oct 02 15:09:46 2019 +0200
@@ -19,6 +19,7 @@
 writing of generated content back to the file-system.
 """
 
+import io
 from doctest import Example
 from itertools import chain, repeat
 import os
@@ -106,13 +107,13 @@
         """Check that the SEDA profile can be used to validate a sample XML document."""
         if os.environ.get('TEST_WRITE_SEDA_FILES'):
             fname = join('/tmp', basename(sample_file))
-            with open(fname, 'w') as stream:
-                stream.write(etree.tostring(root, pretty_print=True))
+            with io.open(fname, 'w') as stream:
+                stream.write(etree.tostring(root, encoding=text_type, pretty_print=True))
             print('Generated profile saved to {}'.format(fname))
         profile = self.schema_class(root)
-        with open(sample_file) as f:
+        with io.open(sample_file) as f:
             sample_xml_string = f.read().format(**substitutions)
-        profile.assert_(etree.fromstring(sample_xml_string))
+        profile.assert_(etree.fromstring(sample_xml_string.encode('utf-8')))
 
 
 class XMLSchemaTestMixin(XmlTestMixin):