2020-04-16 12:53:48 +00:00
|
|
|
import json
|
|
|
|
import pickle
|
2020-05-16 10:42:07 +00:00
|
|
|
import pickletools
|
2020-04-16 12:53:48 +00:00
|
|
|
import queue
|
|
|
|
import unittest
|
|
|
|
|
2020-05-16 10:42:07 +00:00
|
|
|
from rq.serializers import DefaultSerializer, resolve_serializer
|
2020-04-16 12:53:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestSerializers(unittest.TestCase):
|
|
|
|
def test_resolve_serializer(self):
|
|
|
|
"""Ensure function resolve_serializer works correctly"""
|
|
|
|
serializer = resolve_serializer(None)
|
|
|
|
self.assertIsNotNone(serializer)
|
2020-05-16 10:42:07 +00:00
|
|
|
self.assertEqual(serializer, DefaultSerializer)
|
|
|
|
|
|
|
|
# Test round trip with default serializer
|
|
|
|
test_data = {'test': 'data'}
|
|
|
|
serialized_data = serializer.dumps(test_data)
|
|
|
|
self.assertEqual(serializer.loads(serialized_data), test_data)
|
2023-05-17 16:19:14 +00:00
|
|
|
self.assertEqual(next(pickletools.genops(serialized_data))[1], pickle.HIGHEST_PROTOCOL)
|
2020-04-16 12:53:48 +00:00
|
|
|
|
|
|
|
# Test using json serializer
|
|
|
|
serializer = resolve_serializer(json)
|
|
|
|
self.assertIsNotNone(serializer)
|
|
|
|
|
|
|
|
self.assertTrue(hasattr(serializer, 'dumps'))
|
|
|
|
self.assertTrue(hasattr(serializer, 'loads'))
|
|
|
|
|
|
|
|
# Test raise NotImplmentedError
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
resolve_serializer(object)
|
|
|
|
|
|
|
|
# Test raise Exception
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
resolve_serializer(queue.Queue())
|
|
|
|
|
|
|
|
# Test using path.to.serializer string
|
|
|
|
serializer = resolve_serializer('tests.fixtures.Serializer')
|
|
|
|
self.assertIsNotNone(serializer)
|