[py3] Manipulate unicode strings in _test_profile()
authorDenis Laxalde <denis.laxalde@logilab.fr>
Wed, 02 Oct 2019 15:51:14 +0200
changeset 3048 91110717a53b
parent 3047 63e2c0ef1a8e
child 3049 7ee3e184cf4f
[py3] Manipulate unicode strings in _test_profile() 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.)
test/data/seda_02_export.rng
test/data/seda_02_export.xsd
test/data/seda_1_export.rng
test/data/seda_1_export.xsd
test/test_profile_generation.py
--- a/test/data/seda_02_export.rng	Wed Oct 02 15:46:00 2019 +0200
+++ b/test/data/seda_02_export.rng	Wed Oct 02 15:51:14 2019 +0200
@@ -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"
--- a/test/data/seda_02_export.xsd	Wed Oct 02 15:46:00 2019 +0200
+++ b/test/data/seda_02_export.xsd	Wed Oct 02 15:51:14 2019 +0200
@@ -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"
--- a/test/data/seda_1_export.rng	Wed Oct 02 15:46:00 2019 +0200
+++ b/test/data/seda_1_export.rng	Wed Oct 02 15:51:14 2019 +0200
@@ -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"
--- a/test/data/seda_1_export.xsd	Wed Oct 02 15:46:00 2019 +0200
+++ b/test/data/seda_1_export.xsd	Wed Oct 02 15:51:14 2019 +0200
@@ -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"
--- a/test/test_profile_generation.py	Wed Oct 02 15:46:00 2019 +0200
+++ b/test/test_profile_generation.py	Wed Oct 02 15:51:14 2019 +0200
@@ -26,7 +26,7 @@
 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 @@
             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 @@
                                    (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