test_deprecated.py 1.7 KB

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