forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #21555 -- Made ValidationError pickable.
Thanks trac username zanuxzan for the report and original patch.
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pickle | ||
from unittest import TestCase | ||
|
||
from django.core.exceptions import ValidationError | ||
|
||
|
||
class PickableValidationErrorTestCase(TestCase): | ||
|
||
def test_validationerror_is_picklable(self): | ||
original = ValidationError('a', code='something') | ||
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) | ||
|
||
original = ValidationError('a', code='something') | ||
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) | ||
|
||
original = ValidationError(['a', 'b']) | ||
unpickled = pickle.loads(pickle.dumps(original)) | ||
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']) | ||
unpickled = pickle.loads(pickle.dumps(ValidationError(original))) | ||
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')]) | ||
unpickled = pickle.loads(pickle.dumps(original)) | ||
self.assertIs(unpickled.args[0][0], unpickled.error_list[0]) | ||
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']} | ||
original = ValidationError(message_dict) | ||
unpickled = pickle.loads(pickle.dumps(original)) | ||
self.assertEqual(unpickled.message_dict, message_dict) |