test_deprecation.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. """
  12. Existing docstring
  13. """
  14. pass
  15. class TestDeprecation(object):
  16. def test_emits_default_deprecation_warning(self, recwarn):
  17. warnings.simplefilter("default", RemovedInFlaskBB3)
  18. default_deprecation()
  19. assert len(recwarn) == 1
  20. assert "default_deprecation is deprecated" in str(recwarn[0].message)
  21. assert recwarn[0].category == RemovedInFlaskBB3
  22. assert recwarn[0].filename == __file__
  23. # assert on the next line is conditional on the position of the call
  24. # to default_deprecation please don't jiggle it around too much
  25. assert recwarn[0].lineno == 28
  26. assert "only_a_drill is deprecated" in only_a_drill.__doc__
  27. def tests_emits_specialized_message(self, recwarn):
  28. warnings.simplefilter("default", RemovedInFlaskBB3)
  29. only_a_drill()
  30. expected = "only_a_drill is deprecated and will be removed in version {}. This is only a drill".format( # noqa
  31. NEXT_VERSION_STRING
  32. )
  33. assert len(recwarn) == 1
  34. assert expected in str(recwarn[0].message)
  35. def tests_only_accepts_FlaskBBDeprecationWarnings(self):
  36. with pytest.raises(ValueError) as excinfo:
  37. # DeprecationWarning is ignored by default
  38. @deprecated("This is also a drill", category=UserWarning)
  39. def also_a_drill():
  40. pass
  41. assert "Expected subclass of FlaskBBDeprecation" in str(excinfo.value)
  42. def tests_deprecated_decorator_work_with_method(self, recwarn):
  43. warnings.simplefilter("default", RemovedInFlaskBB3)
  44. self.deprecated_instance_method()
  45. assert len(recwarn) == 1
  46. def test_adds_to_existing_docstring(self, recwarn):
  47. docstring = default_deprecation.__doc__
  48. assert "Existing docstring" in docstring
  49. assert "default_deprecation is deprecated" in docstring
  50. @deprecated()
  51. def deprecated_instance_method(self):
  52. pass