]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- backport the expects_warning() context manager from 1.0
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 2 Sep 2014 18:22:28 +0000 (14:22 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 2 Sep 2014 18:24:33 +0000 (14:24 -0400)
lib/sqlalchemy/testing/assertions.py

index 79411af7e97cff105b96ea41c2581aeaf7bc651b..c15b95d0594478c9759920bd655112dec126dc61 100644 (file)
@@ -24,6 +24,28 @@ from .util import fail
 import contextlib
 
 
+@contextlib.contextmanager
+def expect_warnings(*messages):
+    """Context manager to expect warnings with the given messages."""
+
+    filters = [dict(action='ignore',
+                    category=sa_exc.SAPendingDeprecationWarning)]
+    if not messages:
+        filters.append(dict(action='ignore',
+                            category=sa_exc.SAWarning))
+    else:
+        filters.extend(dict(action='ignore',
+                            message=message,
+                            category=sa_exc.SAWarning)
+                       for message in messages)
+    for f in filters:
+        warnings.filterwarnings(**f)
+    try:
+        yield
+    finally:
+        resetwarnings()
+
+
 def emits_warning(*messages):
     """Mark a test as emitting a warning.
 
@@ -31,31 +53,10 @@ def emits_warning(*messages):
     strings; these will be matched to the root of the warning description by
     warnings.filterwarnings().
     """
-    # TODO: it would be nice to assert that a named warning was
-    # emitted. should work with some monkeypatching of warnings,
-    # and may work on non-CPython if they keep to the spirit of
-    # warnings.showwarning's docstring.
-    # - update: jython looks ok, it uses cpython's module
-
     @decorator
     def decorate(fn, *args, **kw):
-        # todo: should probably be strict about this, too
-        filters = [dict(action='ignore',
-                        category=sa_exc.SAPendingDeprecationWarning)]
-        if not messages:
-            filters.append(dict(action='ignore',
-                                category=sa_exc.SAWarning))
-        else:
-            filters.extend(dict(action='ignore',
-                                message=message,
-                                category=sa_exc.SAWarning)
-                           for message in messages)
-        for f in filters:
-            warnings.filterwarnings(**f)
-        try:
+        with expect_warnings(*messages):
             return fn(*args, **kw)
-        finally:
-            resetwarnings()
     return decorate