test_base.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import unittest
  2. import types
  3. from celery.backends.base import BaseBackend, KeyValueStoreBackend
  4. from celery.serialization import find_nearest_pickleable_exception as fnpe
  5. from celery.serialization import UnpickleableExceptionWrapper
  6. from django.db.models.base import subclass_exception
  7. class wrapobject(object):
  8. def __init__(self, *args, **kwargs):
  9. self.args = args
  10. Oldstyle = types.ClassType("Oldstyle", (), {})
  11. Unpickleable = subclass_exception("Unpickleable", KeyError, "foo.module")
  12. Impossible = subclass_exception("Impossible", object, "foo.module")
  13. Lookalike = subclass_exception("Lookalike", wrapobject, "foo.module")
  14. b = BaseBackend()
  15. class TestBaseBackendInterface(unittest.TestCase):
  16. def test_get_status(self):
  17. self.assertRaises(NotImplementedError,
  18. b.is_done, "SOMExx-N0Nex1stant-IDxx-")
  19. def test_store_result(self):
  20. self.assertRaises(NotImplementedError,
  21. b.store_result, "SOMExx-N0nex1stant-IDxx-", 42, "DONE")
  22. def test_get_result(self):
  23. self.assertRaises(NotImplementedError,
  24. b.get_result, "SOMExx-N0nex1stant-IDxx-")
  25. class TestPickleException(unittest.TestCase):
  26. def test_oldstyle(self):
  27. self.assertTrue(fnpe(Oldstyle()) is None)
  28. def test_BaseException(self):
  29. self.assertTrue(fnpe(Exception()) is None)
  30. def test_unpickleable(self):
  31. self.assertTrue(isinstance(fnpe(Unpickleable()), KeyError))
  32. self.assertEquals(fnpe(Impossible()), None)
  33. class TestPrepareException(unittest.TestCase):
  34. def test_unpickleable(self):
  35. x = b.prepare_exception(Unpickleable(1, 2, "foo"))
  36. self.assertTrue(isinstance(x, KeyError))
  37. y = b.exception_to_python(x)
  38. self.assertTrue(isinstance(y, KeyError))
  39. def test_impossible(self):
  40. x = b.prepare_exception(Impossible())
  41. self.assertTrue(isinstance(x, UnpickleableExceptionWrapper))
  42. y = b.exception_to_python(x)
  43. self.assertEquals(y.__class__.__name__, "Impossible")
  44. self.assertEquals(y.__class__.__module__, "foo.module")
  45. def test_regular(self):
  46. x = b.prepare_exception(KeyError("baz"))
  47. self.assertTrue(isinstance(x, KeyError))
  48. y = b.exception_to_python(x)
  49. self.assertTrue(isinstance(y, KeyError))
  50. class TestKeyValueStoreBackendInterface(unittest.TestCase):
  51. def test_get(self):
  52. self.assertRaises(NotImplementedError, KeyValueStoreBackend().get,
  53. "a")
  54. def test_set(self):
  55. self.assertRaises(NotImplementedError, KeyValueStoreBackend().set,
  56. "a", 1)
  57. def test_cleanup(self):
  58. self.assertFalse(KeyValueStoreBackend().cleanup())