test_deprecation.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import warnings
  2. import pytest
  3. from flaskbb.deprecation import RemovedInFlaskBB3, deprecated
  4. NEXT_VERSION_STRING = ".".join([str(x) for x in RemovedInFlaskBB3.version])
  5. @deprecated("This is only a drill")
  6. def only_a_drill():
  7. pass
  8. # TODO(anr): Make the parens optional
  9. @deprecated()
  10. def default_deprecation():
  11. pass
  12. class TestDeprecation(object):
  13. def test_emits_default_deprecation_warning(self, recwarn):
  14. warnings.simplefilter("default", RemovedInFlaskBB3)
  15. default_deprecation()
  16. assert len(recwarn) == 1
  17. assert "default_deprecation is deprecated" in str(recwarn[0].message)
  18. assert recwarn[0].category == RemovedInFlaskBB3
  19. assert recwarn[0].filename == __file__
  20. # assert on the next line is conditional on the position of the call
  21. # to default_deprecation please don't jiggle it around too much
  22. assert recwarn[0].lineno == 25
  23. def tests_emits_specialized_message(self, recwarn):
  24. warnings.simplefilter("default", RemovedInFlaskBB3)
  25. only_a_drill()
  26. expected = "only_a_drill is deprecated and will be removed in version {}. This is only a drill".format( # noqa
  27. NEXT_VERSION_STRING
  28. )
  29. assert len(recwarn) == 1
  30. assert expected in str(recwarn[0].message)
  31. def tests_only_accepts_FlaskBBDeprecationWarnings(self):
  32. with pytest.raises(ValueError) as excinfo:
  33. # DeprecationWarning is ignored by default
  34. @deprecated("This is also a drill", category=UserWarning)
  35. def also_a_drill():
  36. pass
  37. assert "Expected subclass of FlaskBBDeprecation" in str(excinfo.value)
  38. def tests_deprecated_decorator_work_with_method(self, recwarn):
  39. warnings.simplefilter("default", RemovedInFlaskBB3)
  40. self.deprecated_instance_method()
  41. assert len(recwarn) == 1
  42. @deprecated()
  43. def deprecated_instance_method(self):
  44. pass