[PATCH 16 of 17 seda] [py3] Manipulate unicode strings in _test_profile()

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


This is to fix SEDA exports tests on python3.

We first dump XML documents as unicode using the _encoding argument
introduced in previous changeset. Accordingly, expected files are
changed to remove the XML declaration because dumped values will not
contain them.

Then we open files in text mode (using io.open() for consistency between
python2 and python3) and do string formatting using unicode values.
(Especially, six.binary_type(<some integer>) produces different values
in python2 and python3, and we actually want to stringify the integer --
the eid value.)

diff --git a/test/data/seda_02_export.rng b/test/data/seda_02_export.rng
index 494729b..5e826db 100644
--- a/test/data/seda_02_export.rng
+++ b/test/data/seda_02_export.rng
@@ -1,4 +1,3 @@
-<?xml version='1.0' encoding='utf-8' standalone='no'?>
 <rng:grammar
     xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
     xmlns:clmDAFFileTypeCode="urn:un:unece:uncefact:codelist:draft:DAF:fileTypeCode:2009-08-18"
diff --git a/test/data/seda_02_export.xsd b/test/data/seda_02_export.xsd
index c7dfa8e..4013e27 100644
--- a/test/data/seda_02_export.xsd
+++ b/test/data/seda_02_export.xsd
@@ -1,4 +1,3 @@
-<?xml version='1.0' encoding='utf-8' standalone='no'?>
 <xsd:schema
     xmlns:clm60133="urn:un:unece:uncefact:codelist:standard:6:0133:40106"
     xmlns:clmDAFFileTypeCode="urn:un:unece:uncefact:codelist:draft:DAF:fileTypeCode:2009-08-18"
diff --git a/test/data/seda_1_export.rng b/test/data/seda_1_export.rng
index ba814c3..88fe5d9 100644
--- a/test/data/seda_1_export.rng
+++ b/test/data/seda_1_export.rng
@@ -1,4 +1,3 @@
-<?xml version='1.0' encoding='utf-8' standalone='no'?>
 <rng:grammar
     xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
     xmlns:clm60133="urn:un:unece:uncefact:codelist:standard:6:0133:40106"
diff --git a/test/data/seda_1_export.xsd b/test/data/seda_1_export.xsd
index 4531095..a4530a6 100644
--- a/test/data/seda_1_export.xsd
+++ b/test/data/seda_1_export.xsd
@@ -1,4 +1,3 @@
-<?xml version='1.0' encoding='utf-8' standalone='no'?>
 <xsd:schema
     xmlns:clm60133="urn:un:unece:uncefact:codelist:standard:6:0133:40106"
     xmlns:clmDAFFileTypeCode="urn:un:unece:uncefact:codelist:draft:DAF:fileTypeCode:2009-08-18"
diff --git a/test/test_profile_generation.py b/test/test_profile_generation.py
index 62f16f8..9aeb5ab 100644
--- a/test/test_profile_generation.py
+++ b/test/test_profile_generation.py
@@ -26,7 +26,7 @@ import os
 from os.path import basename, join
 import unittest
 
-from six import binary_type, text_type
+from six import text_type
 from six.moves import zip
 
 from lxml import etree
@@ -917,7 +917,7 @@ class OldSEDAExportMixin(object):
             agent = cnx.entity_from_eid(self.agent_eid)
 
             adapter = transfer.cw_adapt_to(adapter_id)
-            generated_xsd = adapter.dump()
+            generated_xsd = adapter.dump(_encoding=text_type)
 
             if os.environ.get('TEST_WRITE_SEDA_FILES'):
                 orig_content = generated_xsd
@@ -926,24 +926,24 @@ class OldSEDAExportMixin(object):
                                    (text_type(agent.eid), 'agent-id'),
                                    (agent.dc_title(), 'agent-name')]:
                     orig_content = orig_content.replace(value, '%({})s'.format(key))
-                with open(self.datapath(expected_file + '.new'), 'w') as stream:
+                with io.open(self.datapath(expected_file + '.new'), 'w') as stream:
                     stream.write(orig_content)
                 print('Regenerated expected file as {}.new'.format(expected_file))
 
-            root = etree.fromstring(generated_xsd)
+            root = etree.fromstring(generated_xsd.encode('utf-8'))
             self.assertXmlValid(root)
-            with open(self.datapath(expected_file)) as f:
+            with io.open(self.datapath(expected_file), 'r') as f:
                 expected = f.read() % {
-                    'unit-eid': binary_type(self.unit_eid),
-                    'subunit1-eid': binary_type(self.subunit1_eid),
-                    'subunit2-eid': binary_type(self.subunit2_eid),
-                    'bdo-eid': binary_type(self.bdo_eid),
-                    'kw-eid': binary_type(self.kw_eid),
-                    'chi-eid': binary_type(self.chi_eid),
-                    'concept-uri': binary_type(file_concept.cwuri),
-                    'scheme-uri': binary_type(file_concept.scheme.cwuri),
-                    'agent-id': binary_type(agent.eid),
-                    'agent-name': binary_type(agent.dc_title()),
+                    'unit-eid': text_type(self.unit_eid),
+                    'subunit1-eid': text_type(self.subunit1_eid),
+                    'subunit2-eid': text_type(self.subunit2_eid),
+                    'bdo-eid': text_type(self.bdo_eid),
+                    'kw-eid': text_type(self.kw_eid),
+                    'chi-eid': text_type(self.chi_eid),
+                    'concept-uri': file_concept.cwuri,
+                    'scheme-uri': file_concept.scheme.cwuri,
+                    'agent-id': text_type(agent.eid),
+                    'agent-name': agent.dc_title(),
                 }
             self.assertXmlEqual(expected, generated_xsd)
             return adapter, root



More information about the saem-devel mailing list