2014-06-06 19:10:20 +08:00
|
|
|
import pickle
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import DJANGO_VERSION_PICKLE_KEY, models
|
2016-01-26 20:03:54 +08:00
|
|
|
from django.test import TestCase
|
2015-04-02 07:15:27 +08:00
|
|
|
from django.utils.version import get_version
|
2014-06-06 19:10:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
2015-11-14 04:54:05 +08:00
|
|
|
msg = "Pickled model instance's Django version is not specified."
|
|
|
|
with self.assertRaisesMessage(RuntimeWarning, msg):
|
2014-06-06 19:10:20 +08:00
|
|
|
pickle.loads(pickle.dumps(p))
|
|
|
|
|
|
|
|
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]
|
2015-04-02 07:15:27 +08:00
|
|
|
data[DJANGO_VERSION_PICKLE_KEY] = '1.0'
|
2014-06-06 19:10:20 +08:00
|
|
|
return reduce_list
|
|
|
|
|
|
|
|
p = DifferentDjangoVersion(title="FooBar")
|
2015-11-14 04:54:05 +08:00
|
|
|
msg = "Pickled model instance's Django version 1.0 does not match the current version %s." % get_version()
|
|
|
|
with self.assertRaisesMessage(RuntimeWarning, msg):
|
2014-06-06 19:10:20 +08:00
|
|
|
pickle.loads(pickle.dumps(p))
|