test_serialization.py 2.0 KB

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