[PATCH eac] Refactoring of ctx_assert with a context manager

Frank Bessou frank.bessou at logilab.fr
Thu Sep 5 15:01:29 CEST 2019


Alpleid tahkns ! :)

On 05/09/2019 11:55, Frank Bessou wrote:
> LGTM ! :)
> 
> On 05/09/2019 11:53, Guillaume Vandevelde wrote:
>> # HG changeset patch
>> # User Guillaume Vandevelde <gvandevelde at logilab.fr>
>> # Date 1567677154 -7200
>> #      Thu Sep 05 11:52:34 2019 +0200
>> # Node ID 411269367a2029098fa4e3d489b8e3d6f43741cd
>> # Parent  268a347314f17a04559a48d3bd851863cb92e158
>> # Available At http://hg.logilab.org/review/cubes/eac
>> #              hg pull http://hg.logilab.org/review/cubes/eac -r 
>> 411269367a20
>> Refactoring of ctx_assert with a context manager
>>
>> diff -r 268a347314f1 -r 411269367a20 test/test_dataimport.py
>> --- a/test/test_dataimport.py    Thu Sep 05 10:10:32 2019 +0200
>> +++ b/test/test_dataimport.py    Thu Sep 05 11:52:34 2019 +0200
>> @@ -26,6 +26,7 @@
>>   from lxml import etree
>>   from six import reraise
>>   from six.moves import map
>> +from contextlib import contextmanager
>>   from cubicweb import NoResultError
>>   from cubicweb.dataimport.importer import ExtEntity, SimpleImportLog
>> @@ -64,14 +65,12 @@
>>       return edict
>> -def ctx_assert(assert_method, actual, expected, ctx, msg=None):
>> -    """Wrap assertion method with a context message"""
>> + at contextmanager
>> +def ctx_assert(ctx):
>>       try:
>> -        assert_method(actual, expected, msg=msg)
>> +        yield
>>       except AssertionError as exc:
>> -        msg = str(exc)
>> -        if ctx:
>> -            msg = ('[%s] ' % ctx) + msg
>> +        msg = '%s %s' % (exc, exc)
>>           reraise(AssertionError, AssertionError(msg), 
>> sys.exc_info()[-1])
>> @@ -794,7 +793,8 @@
>>           entities = extentities2dict(entities)
>>           expected = extentities2dict(expected)
>>           etypes, expected_etypes = list(entities), list(expected)
>> -        ctx_assert(self.assertCountEqual, etypes, expected_etypes, 
>> ctx='etypes')
>> +        with ctx_assert('etypes'):
>> +            self.assertCountEqual(etypes, expected_etypes)
>>           def safe_int(value):
>>               try:
>> @@ -808,12 +808,11 @@
>>               edict = expected[etype]
>>               entities_etype = entities[etype]
>>               extids, expected_extids = list(entities_etype), list(edict)
>> -            ctx_assert(self.assertCountEqual, extids, expected_extids,
>> -                       ctx='%s/extids' % etype)
>> +            with ctx_assert('%s/extids' % etype):
>> +                self.assertCountEqual(extids, expected_extids)
>>               for extid, values in edict.items():
>> -                ctx_assert(self.assertEqual,
>> -                           tolist(entities_etype[extid]), 
>> tolist(values),
>> -                           ctx='%s/%s/values' % (etype, extid))
>> +                with ctx_assert('%s/%s/values' % (etype, extid)):
>> +                    self.assertEqual(tolist(entities_etype[extid]), 
>> tolist(values))
>>       def test_errors(self):
>>           log = SimpleImportLog('<dummy>')
>>
> 

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



More information about the saem-devel mailing list