2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2010-04-15 12:41:08 +00:00
|
|
|
import datetime
|
2015-01-28 12:35:27 +00:00
|
|
|
import pickle
|
2015-07-16 01:18:07 +00:00
|
|
|
import unittest
|
2014-06-06 11:10:20 +00:00
|
|
|
import warnings
|
2010-03-27 15:54:31 +00:00
|
|
|
|
|
|
|
from django.test import TestCase
|
2015-07-16 01:18:07 +00:00
|
|
|
from django.utils import six
|
2014-06-06 11:10:20 +00:00
|
|
|
from django.utils.encoding import force_text
|
2015-04-01 23:15:27 +00:00
|
|
|
from django.utils.version import get_version
|
2010-03-27 15:54:31 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import Container, Event, Group, Happening, M2MModel
|
2010-03-27 15:54:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PickleabilityTestCase(TestCase):
|
2013-03-29 12:32:21 +00:00
|
|
|
def setUp(self):
|
2013-11-02 21:02:56 +00:00
|
|
|
Happening.objects.create() # make sure the defaults are working (#20158)
|
2013-03-29 12:32:21 +00:00
|
|
|
|
2010-03-27 15:54:31 +00:00
|
|
|
def assert_pickles(self, qs):
|
|
|
|
self.assertEqual(list(pickle.loads(pickle.dumps(qs))), list(qs))
|
|
|
|
|
|
|
|
def test_related_field(self):
|
|
|
|
g = Group.objects.create(name="Ponies Who Own Maybachs")
|
|
|
|
self.assert_pickles(Event.objects.filter(group=g.id))
|
2010-04-15 12:41:08 +00:00
|
|
|
|
|
|
|
def test_datetime_callable_default_all(self):
|
|
|
|
self.assert_pickles(Happening.objects.all())
|
|
|
|
|
|
|
|
def test_datetime_callable_default_filter(self):
|
|
|
|
self.assert_pickles(Happening.objects.filter(when=datetime.datetime.now()))
|
|
|
|
|
2015-07-16 01:18:07 +00:00
|
|
|
def test_string_as_default(self):
|
2010-04-15 12:41:08 +00:00
|
|
|
self.assert_pickles(Happening.objects.filter(name="test"))
|
|
|
|
|
2010-04-21 16:34:33 +00:00
|
|
|
def test_standalone_method_as_default(self):
|
|
|
|
self.assert_pickles(Happening.objects.filter(number1=1))
|
|
|
|
|
2015-07-16 01:18:07 +00:00
|
|
|
@unittest.skipIf(six.PY2, "Field doesn't exist on Python 2.")
|
2010-04-21 16:34:33 +00:00
|
|
|
def test_staticmethod_as_default(self):
|
|
|
|
self.assert_pickles(Happening.objects.filter(number2=1))
|
|
|
|
|
2015-02-23 07:31:58 +00:00
|
|
|
def test_filter_reverse_fk(self):
|
|
|
|
self.assert_pickles(Group.objects.filter(event=1))
|
|
|
|
|
2012-06-22 12:28:15 +00:00
|
|
|
def test_doesnotexist_exception(self):
|
|
|
|
# Ticket #17776
|
|
|
|
original = Event.DoesNotExist("Doesn't exist")
|
|
|
|
unpickled = pickle.loads(pickle.dumps(original))
|
|
|
|
|
|
|
|
# Exceptions are not equal to equivalent instances of themselves, so
|
|
|
|
# can't just use assertEqual(original, unpickled)
|
|
|
|
self.assertEqual(original.__class__, unpickled.__class__)
|
|
|
|
self.assertEqual(original.args, unpickled.args)
|
2013-05-21 09:57:24 +00:00
|
|
|
|
2014-01-22 09:53:41 +00:00
|
|
|
def test_manager_pickle(self):
|
|
|
|
pickle.loads(pickle.dumps(Happening.objects))
|
|
|
|
|
2013-05-21 09:57:24 +00:00
|
|
|
def test_model_pickle(self):
|
|
|
|
"""
|
|
|
|
Test that a model not defined on module level is pickleable.
|
|
|
|
"""
|
|
|
|
original = Container.SomeModel(pk=1)
|
|
|
|
dumped = pickle.dumps(original)
|
|
|
|
reloaded = pickle.loads(dumped)
|
|
|
|
self.assertEqual(original, reloaded)
|
|
|
|
# Also, deferred dynamic model works
|
|
|
|
Container.SomeModel.objects.create(somefield=1)
|
|
|
|
original = Container.SomeModel.objects.defer('somefield')[0]
|
|
|
|
dumped = pickle.dumps(original)
|
|
|
|
reloaded = pickle.loads(dumped)
|
|
|
|
self.assertEqual(original, reloaded)
|
|
|
|
self.assertEqual(original.somefield, reloaded.somefield)
|
|
|
|
|
|
|
|
def test_model_pickle_m2m(self):
|
|
|
|
"""
|
|
|
|
Test intentionally the automatically created through model.
|
|
|
|
"""
|
|
|
|
m1 = M2MModel.objects.create()
|
|
|
|
g1 = Group.objects.create(name='foof')
|
|
|
|
m1.groups.add(g1)
|
2015-02-26 14:19:17 +00:00
|
|
|
m2m_through = M2MModel._meta.get_field('groups').remote_field.through
|
2013-05-21 09:57:24 +00:00
|
|
|
original = m2m_through.objects.get()
|
|
|
|
dumped = pickle.dumps(original)
|
|
|
|
reloaded = pickle.loads(dumped)
|
|
|
|
self.assertEqual(original, reloaded)
|
|
|
|
|
|
|
|
def test_model_pickle_dynamic(self):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
2013-07-29 17:19:04 +00:00
|
|
|
dynclass = type(str("DynamicEventSubclass"), (Event, ),
|
2013-05-21 09:57:24 +00:00
|
|
|
{'Meta': Meta, '__module__': Event.__module__})
|
|
|
|
original = dynclass(pk=1)
|
|
|
|
dumped = pickle.dumps(original)
|
|
|
|
reloaded = pickle.loads(dumped)
|
|
|
|
self.assertEqual(original, reloaded)
|
|
|
|
self.assertIs(reloaded.__class__, dynclass)
|
2013-07-26 08:59:40 +00:00
|
|
|
|
|
|
|
def test_specialized_queryset(self):
|
|
|
|
self.assert_pickles(Happening.objects.values('name'))
|
|
|
|
self.assert_pickles(Happening.objects.values('name').dates('when', 'year'))
|
2015-01-30 07:26:13 +00:00
|
|
|
# With related field (#14515)
|
2014-12-24 13:58:32 +00:00
|
|
|
self.assert_pickles(
|
|
|
|
Event.objects.select_related('group').order_by('title').values_list('title', 'group__name')
|
|
|
|
)
|
|
|
|
|
2013-09-14 07:33:12 +00:00
|
|
|
def test_pickle_prefetch_related_idempotence(self):
|
|
|
|
g = Group.objects.create(name='foo')
|
|
|
|
groups = Group.objects.prefetch_related('event_set')
|
|
|
|
|
|
|
|
# First pickling
|
|
|
|
groups = pickle.loads(pickle.dumps(groups))
|
|
|
|
self.assertQuerysetEqual(groups, [g], lambda x: x)
|
|
|
|
|
|
|
|
# Second pickling
|
|
|
|
groups = pickle.loads(pickle.dumps(groups))
|
|
|
|
self.assertQuerysetEqual(groups, [g], lambda x: x)
|
2014-06-06 11:10:20 +00:00
|
|
|
|
2015-05-29 11:45:36 +00:00
|
|
|
def test_pickle_prefetch_related_with_m2m_and_objects_deletion(self):
|
|
|
|
"""
|
|
|
|
#24831 -- Cached properties on ManyToOneRel created in QuerySet.delete()
|
|
|
|
caused subsequent QuerySet pickling to fail.
|
|
|
|
"""
|
|
|
|
g = Group.objects.create(name='foo')
|
|
|
|
m2m = M2MModel.objects.create()
|
|
|
|
m2m.groups.add(g)
|
|
|
|
Group.objects.all().delete()
|
|
|
|
|
|
|
|
m2ms = M2MModel.objects.prefetch_related('groups')
|
|
|
|
m2ms = pickle.loads(pickle.dumps(m2ms))
|
|
|
|
self.assertQuerysetEqual(m2ms, [m2m], lambda x: x)
|
|
|
|
|
2014-06-06 11:10:20 +00:00
|
|
|
def test_missing_django_version_unpickling(self):
|
|
|
|
"""
|
|
|
|
#21430 -- Verifies a warning is raised for querysets that are
|
|
|
|
unpickled without a Django version
|
|
|
|
"""
|
|
|
|
qs = Group.missing_django_version_objects.all()
|
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
|
|
|
pickle.loads(pickle.dumps(qs))
|
|
|
|
msg = force_text(recorded.pop().message)
|
|
|
|
self.assertEqual(msg,
|
|
|
|
"Pickled queryset instance's Django version is not specified.")
|
|
|
|
|
|
|
|
def test_unsupported_unpickle(self):
|
|
|
|
"""
|
|
|
|
#21430 -- Verifies a warning is raised for querysets that are
|
|
|
|
unpickled with a different Django version than the current
|
|
|
|
"""
|
|
|
|
qs = Group.previous_django_version_objects.all()
|
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
|
|
|
pickle.loads(pickle.dumps(qs))
|
|
|
|
msg = force_text(recorded.pop().message)
|
2015-04-01 23:15:27 +00:00
|
|
|
self.assertEqual(
|
|
|
|
msg,
|
|
|
|
"Pickled queryset instance's Django version 1.0 does not "
|
|
|
|
"match the current version %s." % get_version()
|
|
|
|
)
|