[PATCH 15 of 16 eac] [py3] Force XML serialization to a unicode string in doctests

Frank Bessou frank.bessou at logilab.fr
Tue Oct 1 15:36:48 CEST 2019


Applied on new branch "0.8". Thanks :)

On 01/10/2019 12:36, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde <denis.laxalde at logilab.fr>
> # Date 1569925333 -7200
> #      Tue Oct 01 12:22:13 2019 +0200
> # Node ID 0ad5b0bfdf474b017c2d937839dbc7f5bc79c73e
> # Parent  fdc62496878a3eccff45757983ff5da96d3d0ba1
> # Available At http://hg.logilab.org/review/cubes/eac
> #              hg pull http://hg.logilab.org/review/cubes/eac -r 0ad5b0bfdf47
> [py3] Force XML serialization to a unicode string in doctests
> 
> Doctests use comparisons between unicode strings because bytes in an
> interactive shell is clumsy. On the other hand, "EAC-CPF" adapter's
> dump() returns bytes in order to feed the download view (which is marked
> as binary). In order to keep, unicode strings in docstrings we thus
> allow serialization as unicode strings in dump() through the special
> encoding=text_type parameter. Since XML serialization to a unicode string
> is not possible with an XML declaration, we conditionally drop it in
> dump().
> 
> diff --git a/cubicweb_eac/entities.py b/cubicweb_eac/entities.py
> index f6355cc..4867e95 100644
> --- a/cubicweb_eac/entities.py
> +++ b/cubicweb_eac/entities.py
> @@ -317,7 +317,7 @@ class AuthorityRecordEACAdapter(Abstract
>               name = text_type(self.entity.eid)
>           return u'EAC_{0}.xml'.format(name)
>   
> -    def dump(self):
> +    def dump(self, _encoding=None):
>           """Return an XML string representing the given agent using the EAC-CPF schema."""
>           # Keep related activities since they are used multiple times
>           self.activities = sorted(self.entity.reverse_generated, key=lambda x: x.start, reverse=True)
> @@ -330,7 +330,15 @@ class AuthorityRecordEACAdapter(Abstract
>           self.control_element(eac_cpf_elt)
>           self.cpfdescription_element(eac_cpf_elt)
>           tree = etree.ElementTree(eac_cpf_elt)
> -        return etree.tostring(tree, xml_declaration=True, encoding=self.encoding, pretty_print=True)
> +        encoding = _encoding if _encoding is not None else self.encoding
> +        kwargs = {
> +            'pretty_print': True,
> +            'encoding': encoding,
> +            'xml_declaration': True,
> +        }
> +        if encoding is text_type:
> +            kwargs['xml_declaration'] = False
> +        return etree.tostring(tree, **kwargs)
>   
>       def control_element(self, eac_cpf_elt):
>           control_elt = self.element('control', parent=eac_cpf_elt)
> diff --git a/test/export-roundtrip.rst b/test/export-roundtrip.rst
> index 22aac2f..0a698fa 100644
> --- a/test/export-roundtrip.rst
> +++ b/test/export-roundtrip.rst
> @@ -4,12 +4,13 @@ A roundtrip export test case
>   .. code-block:: python
>   
>       >>> from __future__ import print_function
> +    >>> from six import text_type
>       >>> fpath = self.datapath('FRAD033_EAC_00001_simplified_export.xml')
>       >>> created, updated = testutils.eac_import(cnx, fpath)
>       >>> record = cnx.find('AuthorityRecord', isni=u'22330001300016').one()
> -    >>> generated_eac = record.cw_adapt_to('EAC-CPF').dump()
> +    >>> generated_eac = record.cw_adapt_to('EAC-CPF').dump(_encoding=text_type)
>       >>> print(generated_eac)
> -    <?xml version='1.0' encoding='utf-8'?>
> +    <?xml version='1.0'?>
>       <eac-cpf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="urn:isbn:1-931666-33-4" xsi:schemaLocation="urn:isbn:1-931666-33-4 http://eac.staatsbibliothek-berlin.de/schema/cpf.xsd">
>         <control>
>           <recordId>987654321</recordId>
> diff --git a/test/export-simple.rst b/test/export-simple.rst
> index 5a81efc..02aa00c 100644
> --- a/test/export-simple.rst
> +++ b/test/export-simple.rst
> @@ -4,6 +4,7 @@ A simple export test case
>   .. code-block:: python
>   
>       >>> from __future__ import print_function
> +    >>> from six import text_type
>       >>> from cubicweb import Binary
>       >>> record = testutils.authority_record(cnx, u'Charlie', record_id=u'666')
>       >>> home_addr = cnx.create_entity(
> @@ -31,8 +32,8 @@ A simple export test case
>       ...     chronological_predecessor=record2, chronological_successor=record,
>       ...     xml_wrap=Binary(b'<plip>plop</plip>'))
>       >>> cnx.commit()
> -    >>> print(record.cw_adapt_to('EAC-CPF').dump())
> -    <?xml version='1.0' encoding='utf-8'?>
> +    >>> print(record.cw_adapt_to('EAC-CPF').dump(_encoding=text_type))
> +    <?xml version='1.0'?>
>       <eac-cpf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="urn:isbn:1-931666-33-4" xsi:schemaLocation="urn:isbn:1-931666-33-4 http://eac.staatsbibliothek-berlin.de/schema/cpf.xsd">
>         <control>
>           <recordId>666</recordId>
> 

-- 
Frank Bessou
Logilab         https://www.logilab.fr



More information about the saem-devel mailing list