2013-12-04 07:11:34 +00:00
|
|
|
import pickle
|
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
|
|
|
|
|
|
|
class PickableValidationErrorTestCase(TestCase):
|
|
|
|
def test_validationerror_is_picklable(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
original = ValidationError("a", code="something")
|
2013-12-04 07:11:34 +00:00
|
|
|
unpickled = pickle.loads(pickle.dumps(original))
|
|
|
|
self.assertIs(unpickled, unpickled.error_list[0])
|
|
|
|
self.assertEqual(original.message, unpickled.message)
|
|
|
|
self.assertEqual(original.code, unpickled.code)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
original = ValidationError("a", code="something")
|
2013-12-04 07:11:34 +00:00
|
|
|
unpickled = pickle.loads(pickle.dumps(ValidationError(original)))
|
|
|
|
self.assertIs(unpickled, unpickled.error_list[0])
|
|
|
|
self.assertEqual(original.message, unpickled.message)
|
|
|
|
self.assertEqual(original.code, unpickled.code)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
original = ValidationError(["a", "b"])
|
2013-12-04 07:11:34 +00:00
|
|
|
unpickled = pickle.loads(pickle.dumps(original))
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
original.error_list[0].message, unpickled.error_list[0].message
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
original.error_list[1].message, unpickled.error_list[1].message
|
|
|
|
)
|
|
|
|
|
|
|
|
original = ValidationError(["a", "b"])
|
2013-12-04 07:11:34 +00:00
|
|
|
unpickled = pickle.loads(pickle.dumps(ValidationError(original)))
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
original.error_list[0].message, unpickled.error_list[0].message
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
original.error_list[1].message, unpickled.error_list[1].message
|
|
|
|
)
|
|
|
|
|
|
|
|
original = ValidationError([ValidationError("a"), ValidationError("b")])
|
2013-12-04 07:11:34 +00:00
|
|
|
unpickled = pickle.loads(pickle.dumps(original))
|
|
|
|
self.assertIs(unpickled.args[0][0], unpickled.error_list[0])
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
original.error_list[0].message, unpickled.error_list[0].message
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
original.error_list[1].message, unpickled.error_list[1].message
|
|
|
|
)
|
|
|
|
|
|
|
|
message_dict = {"field1": ["a", "b"], "field2": ["c", "d"]}
|
2013-12-04 07:11:34 +00:00
|
|
|
original = ValidationError(message_dict)
|
|
|
|
unpickled = pickle.loads(pickle.dumps(original))
|
|
|
|
self.assertEqual(unpickled.message_dict, message_dict)
|