[PATCH 13 of 17 seda] [py3] Properly handle when comparing XML in check_xsd_profile test method

Denis Laxalde denis.laxalde at logilab.fr
Wed Oct 2 16:06:41 CEST 2019


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.

diff --git a/test/test_profile_generation.py b/test/test_profile_generation.py
index 5f6cbe2..d5a1389 100644
--- a/test/test_profile_generation.py
+++ b/test/test_profile_generation.py
@@ -19,6 +19,7 @@ You may want to set the TEST_WRITE_SEDA_
 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 @@ class XmlTestMixin(object):
         """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):



More information about the saem-devel mailing list