warnings.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # testing/warnings.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """NOTE: copied/adapted from SQLAlchemy master for backwards compatibility;
  8. this should be removable when Alembic targets SQLAlchemy 0.9.4.
  9. """
  10. from __future__ import absolute_import
  11. import warnings
  12. from sqlalchemy import exc as sa_exc
  13. import re
  14. def setup_filters():
  15. """Set global warning behavior for the test suite."""
  16. warnings.filterwarnings('ignore',
  17. category=sa_exc.SAPendingDeprecationWarning)
  18. warnings.filterwarnings('error', category=sa_exc.SADeprecationWarning)
  19. warnings.filterwarnings('error', category=sa_exc.SAWarning)
  20. def assert_warnings(fn, warning_msgs, regex=False):
  21. """Assert that each of the given warnings are emitted by fn."""
  22. from .assertions import eq_
  23. with warnings.catch_warnings(record=True) as log:
  24. # ensure that nothing is going into __warningregistry__
  25. warnings.filterwarnings("always")
  26. result = fn()
  27. for warning in log:
  28. popwarn = warning_msgs.pop(0)
  29. if regex:
  30. assert re.match(popwarn, str(warning.message))
  31. else:
  32. eq_(popwarn, str(warning.message))
  33. return result