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

Frank Bessou frank.bessou at logilab.fr
Wed Sep 4 17:35:37 CEST 2019


# 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>')



More information about the saem-devel mailing list