test_pickle.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import unittest
  2. 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. super(ArgOverrideException, self).__init__(message, status_code)
  9. class TestPickle(unittest.TestCase):
  10. # See: http://www.reddit.com/r/django/comments/8gdwi/
  11. # celery_distributed_task_queue_for_django/c097hr1
  12. def test_pickle_regular_exception(self):
  13. e = None
  14. try:
  15. raise RegularException("RegularException raised")
  16. except RegularException, e:
  17. pass
  18. pickled = pickle.dumps({"exception": e})
  19. unpickled = pickle.loads(pickled)
  20. exception = unpickled.get("exception")
  21. self.assertTrue(exception)
  22. self.assertTrue(isinstance(exception, RegularException))
  23. self.assertEquals(exception.args, ("RegularException raised", ))
  24. def test_pickle_arg_override_exception(self):
  25. e = None
  26. try:
  27. raise ArgOverrideException("ArgOverrideException raised",
  28. status_code=100)
  29. except ArgOverrideException, e:
  30. pass
  31. pickled = pickle.dumps({"exception": e})
  32. unpickled = pickle.loads(pickled)
  33. exception = unpickled.get("exception")
  34. self.assertTrue(exception)
  35. self.assertTrue(isinstance(exception, ArgOverrideException))
  36. self.assertEquals(exception.args, ("ArgOverrideException raised",
  37. 100))
  38. self.assertEquals(exception.status_code, 100)