test_serialization.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import pytest
  2. import pytz
  3. import sys
  4. from datetime import datetime, date, time, timedelta
  5. from case import Mock, mock
  6. from kombu import Queue
  7. from celery.utils.serialization import (
  8. UnpickleableExceptionWrapper,
  9. get_pickleable_etype,
  10. jsonify,
  11. )
  12. class test_AAPickle:
  13. def test_no_cpickle(self):
  14. prev = sys.modules.pop('celery.utils.serialization', None)
  15. try:
  16. with mock.mask_modules('cPickle'):
  17. from celery.utils.serialization import pickle
  18. import pickle as orig_pickle
  19. assert pickle.dumps is orig_pickle.dumps
  20. finally:
  21. sys.modules['celery.utils.serialization'] = prev
  22. class test_UnpickleExceptionWrapper:
  23. def test_init(self):
  24. x = UnpickleableExceptionWrapper('foo', 'Bar', [10, lambda x: x])
  25. assert x.exc_args
  26. assert len(x.exc_args) == 2
  27. class test_get_pickleable_etype:
  28. def test_get_pickleable_etype(self):
  29. class Unpickleable(Exception):
  30. def __reduce__(self):
  31. raise ValueError('foo')
  32. assert get_pickleable_etype(Unpickleable) is Exception
  33. class test_jsonify:
  34. @pytest.mark.parametrize('obj', [
  35. Queue('foo'),
  36. ['foo', 'bar', 'baz'],
  37. {'foo': 'bar'},
  38. datetime.utcnow(),
  39. datetime.utcnow().replace(tzinfo=pytz.utc),
  40. datetime.utcnow().replace(microsecond=0),
  41. date(2012, 1, 1),
  42. time(hour=1, minute=30),
  43. time(hour=1, minute=30, microsecond=3),
  44. timedelta(seconds=30),
  45. 10,
  46. 10.3,
  47. 'hello',
  48. ])
  49. def test_simple(self, obj):
  50. assert jsonify(obj)
  51. def test_unknown_type_filter(self):
  52. unknown_type_filter = Mock()
  53. obj = object()
  54. assert (jsonify(obj, unknown_type_filter=unknown_type_filter) is
  55. unknown_type_filter.return_value)
  56. unknown_type_filter.assert_called_with(obj)
  57. with pytest.raises(ValueError):
  58. jsonify(obj)