diff options
author | Anubhav Joshi <anubhav9042@gmail.com> | 2014-06-06 16:40:20 +0530 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2014-06-11 10:03:34 -0400 |
commit | 42736ac8e8c31137131714013951249a09e6e7d4 (patch) | |
tree | fcd32b692aee3b1a16902e340ab020e8a3b5a445 /tests/model_regress | |
parent | e163a3d17b2551001677b615ccdaeadf17705d29 (diff) | |
download | django-42736ac8e8c31137131714013951249a09e6e7d4.tar.gz |
Fixed #21430 -- Added a RuntimeWarning when unpickling Models and QuerySets from a different Django version.
Thanks FunkyBob for the suggestion, prasoon2211 for the initial patch,
and akaariai, loic, and charettes for helping in shaping the patch.
Diffstat (limited to 'tests/model_regress')
-rw-r--r-- | tests/model_regress/test_pickle.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/model_regress/test_pickle.py b/tests/model_regress/test_pickle.py new file mode 100644 index 0000000000..8dc90893bf --- /dev/null +++ b/tests/model_regress/test_pickle.py @@ -0,0 +1,53 @@ +import pickle +import warnings + +from django.db import models, DJANGO_VERSION_PICKLE_KEY +from django.test import TestCase +from django.utils.encoding import force_text +from django.utils.version import get_major_version, get_version + + +class ModelPickleTestCase(TestCase): + def test_missing_django_version_unpickling(self): + """ + #21430 -- Verifies a warning is raised for models that are + unpickled without a Django version + """ + class MissingDjangoVersion(models.Model): + title = models.CharField(max_length=10) + + def __reduce__(self): + reduce_list = super(MissingDjangoVersion, self).__reduce__() + data = reduce_list[-1] + del data[DJANGO_VERSION_PICKLE_KEY] + return reduce_list + + p = MissingDjangoVersion(title="FooBar") + with warnings.catch_warnings(record=True) as recorded: + pickle.loads(pickle.dumps(p)) + msg = force_text(recorded.pop().message) + self.assertEqual(msg, + "Pickled model instance's Django version is not specified.") + + def test_unsupported_unpickle(self): + """ + #21430 -- Verifies a warning is raised for models that are + unpickled with a different Django version than the current + """ + class DifferentDjangoVersion(models.Model): + title = models.CharField(max_length=10) + + def __reduce__(self): + reduce_list = super(DifferentDjangoVersion, self).__reduce__() + data = reduce_list[-1] + data[DJANGO_VERSION_PICKLE_KEY] = str(float(get_major_version()) - 0.1) + return reduce_list + + p = DifferentDjangoVersion(title="FooBar") + with warnings.catch_warnings(record=True) as recorded: + pickle.loads(pickle.dumps(p)) + msg = force_text(recorded.pop().message) + self.assertEqual(msg, + "Pickled model instance's Django version %s does not " + "match the current version %s." + % (str(float(get_major_version()) - 0.1), get_version())) |