1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-11-11 03:45:01 -05:00
SickRage/lib/sqlalchemy/testing/warnings.py

57 lines
1.6 KiB
Python
Raw Normal View History

2014-05-05 00:05:43 -04:00
# testing/warnings.py
# Copyright (C) 2005-2014 the SQLAlchemy authors and contributors <see AUTHORS file>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
2014-05-04 23:25:51 -04:00
from __future__ import absolute_import
import warnings
from .. import exc as sa_exc
from .. import util
2014-05-05 00:05:43 -04:00
import re
2014-05-04 23:25:51 -04:00
def testing_warn(msg, stacklevel=3):
"""Replaces sqlalchemy.util.warn during tests."""
filename = "sqlalchemy.testing.warnings"
lineno = 1
2014-05-05 00:05:43 -04:00
if isinstance(msg, util.string_types):
2014-05-04 23:25:51 -04:00
warnings.warn_explicit(msg, sa_exc.SAWarning, filename, lineno)
else:
warnings.warn_explicit(msg, filename, lineno)
def resetwarnings():
"""Reset warning behavior to testing defaults."""
util.warn = util.langhelpers.warn = testing_warn
warnings.filterwarnings('ignore',
category=sa_exc.SAPendingDeprecationWarning)
warnings.filterwarnings('error', category=sa_exc.SADeprecationWarning)
warnings.filterwarnings('error', category=sa_exc.SAWarning)
2014-05-05 00:05:43 -04:00
def assert_warnings(fn, warnings, regex=False):
2014-05-04 23:25:51 -04:00
"""Assert that each of the given warnings are emitted by fn."""
from .assertions import eq_, emits_warning
canary = []
orig_warn = util.warn
def capture_warnings(*args, **kw):
orig_warn(*args, **kw)
popwarn = warnings.pop(0)
canary.append(popwarn)
2014-05-05 00:05:43 -04:00
if regex:
assert re.match(popwarn, args[0])
else:
eq_(args[0], popwarn)
2014-05-04 23:25:51 -04:00
util.warn = util.langhelpers.warn = capture_warnings
result = emits_warning()(fn)()
assert canary, "No warning was emitted"
return result