test_deprecated.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from case import patch
  4. from celery.utils import deprecated
  5. class test_deprecated_property:
  6. @patch('celery.utils.deprecated.warn')
  7. def test_deprecated(self, warn):
  8. class X(object):
  9. _foo = None
  10. @deprecated.Property(deprecation='1.2')
  11. def foo(self):
  12. return self._foo
  13. @foo.setter
  14. def foo(self, value):
  15. self._foo = value
  16. @foo.deleter
  17. def foo(self):
  18. self._foo = None
  19. assert X.foo
  20. assert X.foo.__set__(None, 1)
  21. assert X.foo.__delete__(None)
  22. x = X()
  23. x.foo = 10
  24. warn.assert_called_with(
  25. stacklevel=3, deprecation='1.2', alternative=None,
  26. description='foo', removal=None,
  27. )
  28. warn.reset_mock()
  29. assert x.foo == 10
  30. warn.assert_called_with(
  31. stacklevel=3, deprecation='1.2', alternative=None,
  32. description='foo', removal=None,
  33. )
  34. warn.reset_mock()
  35. del(x.foo)
  36. warn.assert_called_with(
  37. stacklevel=3, deprecation='1.2', alternative=None,
  38. description='foo', removal=None,
  39. )
  40. assert x._foo is None
  41. def test_deprecated_no_setter_or_deleter(self):
  42. class X(object):
  43. @deprecated.Property(deprecation='1.2')
  44. def foo(self):
  45. pass
  46. assert X.foo
  47. x = X()
  48. with pytest.raises(AttributeError):
  49. x.foo = 10
  50. with pytest.raises(AttributeError):
  51. del(x.foo)
  52. class test_warn:
  53. @patch('warnings.warn')
  54. def test_warn_deprecated(self, warn):
  55. deprecated.warn('Foo')
  56. warn.assert_called()