[PATCH 2 of 2 eac] extract ctx_assert from the tests

Frank Bessou frank.bessou at logilab.fr
Thu Sep 5 09:39:59 CEST 2019


Applied this one after modifying the commit message:

# HG changeset patch
# User Frank Bessou <frank.bessou at logilab.fr>
# Date 1567668315 -7200
#      Thu Sep 05 09:25:15 2019 +0200
# Node ID ebbd8c834e26b4db71a1f99e226eff046ed4dccf
# Parent  63196f5cf34086f24a4f46bd21d7185a22364863
# Available At http://hg.logilab.org/review/cubes/eac
#              hg pull http://hg.logilab.org/review/cubes/eac -r 
ebbd8c834e26
Extract ctx_assert from the EACXMLParserTC

This is a helper function that could be used anywhere.
We directly pass an assert_method argument to this function in order to
make it easier to understand.


On 04/09/2019 17:35, Frank Bessou wrote:
> # HG changeset patch
> # User Frank Bessou <frank.bessou at logilab.fr>
> # Date 1567608525 -7200
> #      Wed Sep 04 16:48:45 2019 +0200
> # Node ID 283e2fe96d57eaae4a98c9aec0c0515de403a994
> # Parent  850a27100121faf395d73dec6436f9ad7c4f10da
> # Available At http://hg.logilab.org/review/cubes/eac
> #              hg pull http://hg.logilab.org/review/cubes/eac -r 283e2fe96d57
> extract ctx_assert from the tests
> 
> diff --git a/test/test_dataimport.py b/test/test_dataimport.py
> --- a/test/test_dataimport.py
> +++ b/test/test_dataimport.py
> @@ -69,6 +69,17 @@ def datapath(*fname):
>       return join(dirname(__file__), 'data', *fname)
>   
>   
> +def ctx_assert(assert_method, actual, expected, ctx, msg=None):
> +    """Wrap assertion method with a context message"""
> +    try:
> +        assert_method(actual, expected, msg=msg)
> +    except AssertionError as exc:
> +        msg = str(exc)
> +        if ctx:
> +            msg = ('[%s] ' % ctx) + msg
> +        reraise(AssertionError, AssertionError(msg), sys.exc_info()[-1])
> +
> +
>   class EACXMLParserTC(unittest.TestCase):
>   
>       if sys.version_info < (3, 2):
> @@ -763,21 +774,11 @@ class EACXMLParserTC(unittest.TestCase):
>           self.assertIn(expected, [x.values['parts'].pop() for x in entities
>                                    if x.etype == 'NameEntry'])
>   
> -    def ctx_assert(self, method, actual, expected, ctx, msg=None):
> -        """Wrap assertion method with a context message"""
> -        try:
> -            getattr(self, method)(actual, expected, msg=msg)
> -        except AssertionError as exc:
> -            msg = str(exc)
> -            if ctx:
> -                msg = ('[%s] ' % ctx) + msg
> -            reraise(AssertionError, AssertionError(msg), sys.exc_info()[-1])
> -
>       def check_external_entities(self, entities, expected):
>           entities = extentities2dict(entities)
>           expected = extentities2dict(expected)
>           etypes, expected_etypes = list(entities), list(expected)
> -        self.ctx_assert('assertCountEqual', etypes, expected_etypes, ctx='etypes')
> +        ctx_assert(self.assertCountEqual, etypes, expected_etypes, ctx='etypes')
>   
>           def safe_int(value):
>               try:
> @@ -791,12 +792,12 @@ class EACXMLParserTC(unittest.TestCase):
>               edict = expected[etype]
>               entities_etype = entities[etype]
>               extids, expected_extids = list(entities_etype), list(edict)
> -            self.ctx_assert('assertCountEqual', extids, expected_extids,
> -                            ctx='%s/extids' % etype)
> +            ctx_assert(self.assertCountEqual, extids, expected_extids,
> +                       ctx='%s/extids' % etype)
>               for extid, values in edict.items():
> -                self.ctx_assert('assertEqual',
> -                                tolist(entities_etype[extid]), tolist(values),
> -                                ctx='%s/%s/values' % (etype, extid))
> +                ctx_assert(self.assertEqual,
> +                           tolist(entities_etype[extid]), tolist(values),
> +                           ctx='%s/%s/values' % (etype, extid))
>   
>       def test_errors(self):
>           log = SimpleImportLog('<dummy>')
> 

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



More information about the saem-devel mailing list