test_pickle.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import absolute_import, unicode_literals
  2. from celery.utils.serialization import pickle
  3. class RegularException(Exception):
  4. pass
  5. class ArgOverrideException(Exception):
  6. def __init__(self, message, status_code=10):
  7. self.status_code = status_code
  8. Exception.__init__(self, message, status_code)
  9. class test_Pickle:
  10. def test_pickle_regular_exception(self):
  11. exc = None
  12. try:
  13. raise RegularException('RegularException raised')
  14. except RegularException as exc_:
  15. exc = exc_
  16. pickled = pickle.dumps({'exception': exc})
  17. unpickled = pickle.loads(pickled)
  18. exception = unpickled.get('exception')
  19. assert exception
  20. assert isinstance(exception, RegularException)
  21. assert exception.args == ('RegularException raised',)
  22. def test_pickle_arg_override_exception(self):
  23. exc = None
  24. try:
  25. raise ArgOverrideException(
  26. 'ArgOverrideException raised', status_code=100,
  27. )
  28. except ArgOverrideException as exc_:
  29. exc = exc_
  30. pickled = pickle.dumps({'exception': exc})
  31. unpickled = pickle.loads(pickled)
  32. exception = unpickled.get('exception')
  33. assert exception
  34. assert isinstance(exception, ArgOverrideException)
  35. assert exception.args == ('ArgOverrideException raised', 100)
  36. assert exception.status_code == 100