2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-03-27 23:54:31 +08:00
|
|
|
import pickle
|
2010-04-15 20:41:08 +08:00
|
|
|
import datetime
|
2010-03-27 23:54:31 +08:00
|
|
|
|
2013-05-21 17:57:24 +08:00
|
|
|
from django.db import models
|
2010-03-27 23:54:31 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2013-05-21 17:57:24 +08:00
|
|
|
from .models import Group, Event, Happening, Container, M2MModel
|
2010-03-27 23:54:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
class PickleabilityTestCase(TestCase):
|
2013-03-29 20:32:21 +08:00
|
|
|
def setUp(self):
|
|
|
|
Happening.objects.create() # make sure the defaults are working (#20158)
|
|
|
|
|
2010-03-27 23:54:31 +08: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 20:41:08 +08: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()))
|
|
|
|
|
|
|
|
def test_lambda_as_default(self):
|
|
|
|
self.assert_pickles(Happening.objects.filter(name="test"))
|
|
|
|
|
2010-04-22 00:34:33 +08:00
|
|
|
def test_standalone_method_as_default(self):
|
|
|
|
self.assert_pickles(Happening.objects.filter(number1=1))
|
|
|
|
|
|
|
|
def test_staticmethod_as_default(self):
|
|
|
|
self.assert_pickles(Happening.objects.filter(number2=1))
|
|
|
|
|
|
|
|
def test_classmethod_as_default(self):
|
2010-04-22 02:07:23 +08:00
|
|
|
self.assert_pickles(Happening.objects.filter(number3=1))
|
2010-04-22 00:34:33 +08:00
|
|
|
|
|
|
|
def test_membermethod_as_default(self):
|
2010-04-22 02:07:23 +08:00
|
|
|
self.assert_pickles(Happening.objects.filter(number4=1))
|
2012-06-22 20:28:15 +08: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 17:57:24 +08: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)
|
|
|
|
m2m_through = M2MModel._meta.get_field_by_name('groups')[0].rel.through
|
|
|
|
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-30 01:19:04 +08:00
|
|
|
dynclass = type(str("DynamicEventSubclass"), (Event, ),
|
2013-05-21 17:57:24 +08: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 16:59:40 +08:00
|
|
|
|
|
|
|
def test_specialized_queryset(self):
|
|
|
|
self.assert_pickles(Happening.objects.values('name'))
|
|
|
|
self.assert_pickles(Happening.objects.values('name').dates('when', 'year'))
|