diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/queryset_pickle | |
parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
download | django-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/queryset_pickle')
-rw-r--r-- | tests/queryset_pickle/__init__.py | 0 | ||||
-rw-r--r-- | tests/queryset_pickle/models.py | 38 | ||||
-rw-r--r-- | tests/queryset_pickle/tests.py | 48 |
3 files changed, 86 insertions, 0 deletions
diff --git a/tests/queryset_pickle/__init__.py b/tests/queryset_pickle/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/queryset_pickle/__init__.py diff --git a/tests/queryset_pickle/models.py b/tests/queryset_pickle/models.py new file mode 100644 index 0000000000..d3500c903b --- /dev/null +++ b/tests/queryset_pickle/models.py @@ -0,0 +1,38 @@ +from __future__ import absolute_import + +import datetime + +from django.db import models +from django.utils.translation import ugettext_lazy as _ + + +def standalone_number(self): + return 1 + +class Numbers(object): + @staticmethod + def get_static_number(self): + return 2 + + @classmethod + def get_class_number(self): + return 3 + + def get_member_number(self): + return 4 + +nn = Numbers() + +class Group(models.Model): + name = models.CharField(_('name'), max_length=100) + +class Event(models.Model): + group = models.ForeignKey(Group) + +class Happening(models.Model): + when = models.DateTimeField(blank=True, default=datetime.datetime.now) + name = models.CharField(blank=True, max_length=100, default=lambda:"test") + number1 = models.IntegerField(blank=True, default=standalone_number) + number2 = models.IntegerField(blank=True, default=Numbers.get_static_number) + number3 = models.IntegerField(blank=True, default=Numbers.get_class_number) + number4 = models.IntegerField(blank=True, default=nn.get_member_number) diff --git a/tests/queryset_pickle/tests.py b/tests/queryset_pickle/tests.py new file mode 100644 index 0000000000..ab32e8f647 --- /dev/null +++ b/tests/queryset_pickle/tests.py @@ -0,0 +1,48 @@ +from __future__ import absolute_import + +import pickle +import datetime + +from django.test import TestCase + +from .models import Group, Event, Happening + + +class PickleabilityTestCase(TestCase): + 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)) + + 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")) + + 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): + self.assert_pickles(Happening.objects.filter(number3=1)) + + def test_membermethod_as_default(self): + self.assert_pickles(Happening.objects.filter(number4=1)) + + 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) |