test_pickle.py 1.4 KB

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