| 1 |
import json |
| 2 |
import pickle |
| 3 |
import sys |
| 4 |
from datetime import date, datetime, time, timedelta, timezone |
| 5 |
from unittest.mock import Mock |
| 6 |
|
| 7 |
import pytest |
| 8 |
from kombu import Queue |
| 9 |
|
| 10 |
from celery.utils.serialization import (STRTOBOOL_DEFAULT_TABLE, UnpickleableExceptionWrapper, ensure_serializable, |
| 11 |
get_pickleable_etype, jsonify, strtobool) |
| 12 |
|
| 13 |
if sys.version_info >= (3, 9): |
| 14 |
from zoneinfo import ZoneInfo |
| 15 |
else: |
| 16 |
from backports.zoneinfo import ZoneInfo |
| 17 |
|
| 18 |
|
| 19 |
class test_AAPickle: |
| 20 |
|
| 21 |
@pytest.mark.masked_modules('cPickle') |
| 22 |
def test_no_cpickle(self, mask_modules): |
| 23 |
prev = sys.modules.pop('celery.utils.serialization', None) |
| 24 |
try: |
| 25 |
import pickle as orig_pickle |
| 26 |
|
| 27 |
from celery.utils.serialization import pickle |
| 28 |
assert pickle.dumps is orig_pickle.dumps |
| 29 |
finally: |
| 30 |
sys.modules['celery.utils.serialization'] = prev |
| 31 |
|
| 32 |
|
| 33 |
class test_ensure_serializable: |
| 34 |
|
| 35 |
def test_json_py3(self): |
| 36 |
expected = (1, "<class 'object'>") |
| 37 |
actual = ensure_serializable([1, object], encoder=json.dumps) |
| 38 |
assert expected == actual |
| 39 |
|
| 40 |
def test_pickle(self): |
| 41 |
expected = (1, object) |
| 42 |
actual = ensure_serializable(expected, encoder=pickle.dumps) |
| 43 |
assert expected == actual |
| 44 |
|
| 45 |
|
| 46 |
class test_UnpickleExceptionWrapper: |
| 47 |
|
| 48 |
def test_init(self): |
| 49 |
x = UnpickleableExceptionWrapper('foo', 'Bar', [10, lambda x: x]) |
| 50 |
assert x.exc_args |
| 51 |
assert len(x.exc_args) == 2 |
| 52 |
|
| 53 |
|
| 54 |
class test_get_pickleable_etype: |
| 55 |
|
| 56 |
def test_get_pickleable_etype(self): |
| 57 |
class Unpickleable(Exception): |
| 58 |
def __reduce__(self): |
| 59 |
raise ValueError('foo') |
| 60 |
|
| 61 |
assert get_pickleable_etype(Unpickleable) is Exception |
| 62 |
|
| 63 |
|
| 64 |
class test_jsonify: |
| 65 |
|
| 66 |
@pytest.mark.parametrize('obj', [ |
| 67 |
Queue('foo'), |
| 68 |
['foo', 'bar', 'baz'], |
| 69 |
{'foo': 'bar'}, |
| 70 |
datetime.now(timezone.utc), |
| 71 |
datetime.now(timezone.utc).replace(tzinfo=ZoneInfo("UTC")), |
| 72 |
datetime.now(timezone.utc).replace(microsecond=0), |
| 73 |
date(2012, 1, 1), |
| 74 |
time(hour=1, minute=30), |
| 75 |
time(hour=1, minute=30, microsecond=3), |
| 76 |
timedelta(seconds=30), |
| 77 |
10, |
| 78 |
10.3, |
| 79 |
'hello', |
| 80 |
]) |
| 81 |
def test_simple(self, obj): |
| 82 |
assert jsonify(obj) |
| 83 |
|
| 84 |
def test_unknown_type_filter(self): |
| 85 |
unknown_type_filter = Mock() |
| 86 |
obj = object() |
| 87 |
assert (jsonify(obj, unknown_type_filter=unknown_type_filter) is |
| 88 |
unknown_type_filter.return_value) |
| 89 |
unknown_type_filter.assert_called_with(obj) |
| 90 |
|
| 91 |
with pytest.raises(ValueError): |
| 92 |
jsonify(obj) |
| 93 |
|
| 94 |
|
| 95 |
class test_strtobool: |
| 96 |
|
| 97 |
@pytest.mark.parametrize('s,b', |
| 98 |
STRTOBOOL_DEFAULT_TABLE.items()) |
| 99 |
def test_default_table(self, s, b): |
| 100 |
assert strtobool(s) == b |
| 101 |
|
| 102 |
def test_unknown_value(self): |
| 103 |
with pytest.raises(TypeError, match="Cannot coerce 'foo' to type bool"): |
| 104 |
strtobool('foo') |
| 105 |
|
| 106 |
def test_no_op(self): |
| 107 |
assert strtobool(1) == 1 |
| 108 |
|
| 109 |
def test_custom_table(self): |
| 110 |
custom_table = { |
| 111 |
'foo': True, |
| 112 |
'bar': False |
| 113 |
} |
| 114 |
|
| 115 |
assert strtobool("foo", table=custom_table) |
| 116 |
assert not strtobool("bar", table=custom_table) |