test_serialization.py 2.0 KB

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