From 04e69598de75550a9227bfac353ff5606b6cbe43 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 15 Jul 2015 21:18:07 -0400 Subject: [PATCH] Refs #24919 -- Made test models serializable for migrations. --- tests/admin_views/models.py | 8 ++-- tests/delete/models.py | 3 +- tests/field_subclassing/fields.py | 2 + tests/forms_tests/models.py | 68 ++++++++++++++++++++++++------- tests/queryset_pickle/models.py | 18 +++----- tests/queryset_pickle/tests.py | 11 ++--- 6 files changed, 72 insertions(+), 38 deletions(-) diff --git a/tests/admin_views/models.py b/tests/admin_views/models.py index 6c009f85d7..0df249febe 100644 --- a/tests/admin_views/models.py +++ b/tests/admin_views/models.py @@ -422,10 +422,12 @@ class Category(models.Model): return '%s:o%s' % (self.id, self.order) +def link_posted_default(): + return datetime.date.today() - datetime.timedelta(days=7) + + class Link(models.Model): - posted = models.DateField( - default=lambda: datetime.date.today() - datetime.timedelta(days=7) - ) + posted = models.DateField(default=link_posted_default) url = models.URLField() post = models.ForeignKey("Post") diff --git a/tests/delete/models.py b/tests/delete/models.py index 408b6b83f8..38e7c7cf0e 100644 --- a/tests/delete/models.py +++ b/tests/delete/models.py @@ -12,7 +12,8 @@ class R(models.Model): return "%s" % self.pk -get_default_r = lambda: R.objects.get_or_create(is_default=True)[0] +def get_default_r(): + return R.objects.get_or_create(is_default=True)[0] class S(models.Model): diff --git a/tests/field_subclassing/fields.py b/tests/field_subclassing/fields.py index d1385646a8..84ecf8d506 100644 --- a/tests/field_subclassing/fields.py +++ b/tests/field_subclassing/fields.py @@ -5,6 +5,7 @@ import warnings from django.db import models from django.utils import six +from django.utils.deconstruct import deconstructible from django.utils.deprecation import RemovedInDjango110Warning from django.utils.encoding import force_text, python_2_unicode_compatible @@ -16,6 +17,7 @@ warnings.filterwarnings( ) +@deconstructible @python_2_unicode_compatible class Small(object): """ diff --git a/tests/forms_tests/models.py b/tests/forms_tests/models.py index 830927aeba..7eb4792213 100644 --- a/tests/forms_tests/models.py +++ b/tests/forms_tests/models.py @@ -10,7 +10,10 @@ from django.db import models from django.utils.encoding import python_2_unicode_compatible callable_default_counter = itertools.count() -callable_default = lambda: next(callable_default_counter) + + +def callable_default(): + return next(callable_default_counter) temp_storage = FileSystemStorage(location=tempfile.mkdtemp()) @@ -68,25 +71,62 @@ class ChoiceOptionModel(models.Model): return 'ChoiceOption %d' % self.pk +def choice_default(): + return ChoiceOptionModel.objects.get_or_create(name='default')[0] + + +def choice_default_list(): + return [choice_default()] + + +def int_default(): + return 1 + + +def int_list_default(): + return [1] + + class ChoiceFieldModel(models.Model): """Model with ForeignKey to another model, for testing ModelForm generation with ModelChoiceField.""" - choice = models.ForeignKey(ChoiceOptionModel, blank=False, - default=lambda: ChoiceOptionModel.objects.get(name='default')) - choice_int = models.ForeignKey(ChoiceOptionModel, blank=False, related_name='choice_int', - default=lambda: 1) - - multi_choice = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice', - default=lambda: ChoiceOptionModel.objects.filter(name='default')) - multi_choice_int = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice_int', - default=lambda: [1]) + choice = models.ForeignKey( + ChoiceOptionModel, + blank=False, + default=choice_default, + ) + choice_int = models.ForeignKey( + ChoiceOptionModel, + blank=False, + related_name='choice_int', + default=int_default, + ) + multi_choice = models.ManyToManyField( + ChoiceOptionModel, + blank=False, + related_name='multi_choice', + default=choice_default_list, + ) + multi_choice_int = models.ManyToManyField( + ChoiceOptionModel, + blank=False, + related_name='multi_choice_int', + default=int_list_default, + ) class OptionalMultiChoiceModel(models.Model): - multi_choice = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='not_relevant', - default=lambda: ChoiceOptionModel.objects.filter(name='default')) - multi_choice_optional = models.ManyToManyField(ChoiceOptionModel, blank=True, - related_name='not_relevant2') + multi_choice = models.ManyToManyField( + ChoiceOptionModel, + blank=False, + related_name='not_relevant', + default=choice_default, + ) + multi_choice_optional = models.ManyToManyField( + ChoiceOptionModel, + blank=True, + related_name='not_relevant2', + ) class FileModel(models.Model): diff --git a/tests/queryset_pickle/models.py b/tests/queryset_pickle/models.py index 77134a5018..bb20e71524 100644 --- a/tests/queryset_pickle/models.py +++ b/tests/queryset_pickle/models.py @@ -1,6 +1,7 @@ import datetime from django.db import DJANGO_VERSION_PICKLE_KEY, models +from django.utils import six from django.utils.translation import ugettext_lazy as _ @@ -13,15 +14,6 @@ class Numbers(object): def get_static_number(): return 2 - @classmethod - def get_class_number(cls): - return 3 - - def get_member_number(self): - return 4 - -nn = Numbers() - class PreviousDjangoVersionQuerySet(models.QuerySet): def __getstate__(self): @@ -51,11 +43,11 @@ class Event(models.Model): class Happening(models.Model): when = models.DateTimeField(blank=True, default=datetime.datetime.now) - name = models.CharField(blank=True, max_length=100, default=lambda: "test") + name = models.CharField(blank=True, max_length=100, default="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) + if six.PY3: + # default serializable on Python 3 only + number2 = models.IntegerField(blank=True, default=Numbers.get_static_number) class Container(object): diff --git a/tests/queryset_pickle/tests.py b/tests/queryset_pickle/tests.py index 6ac51c8431..63f4283870 100644 --- a/tests/queryset_pickle/tests.py +++ b/tests/queryset_pickle/tests.py @@ -2,9 +2,11 @@ from __future__ import unicode_literals import datetime import pickle +import unittest import warnings from django.test import TestCase +from django.utils import six from django.utils.encoding import force_text from django.utils.version import get_version @@ -28,21 +30,16 @@ class PickleabilityTestCase(TestCase): def test_datetime_callable_default_filter(self): self.assert_pickles(Happening.objects.filter(when=datetime.datetime.now())) - def test_lambda_as_default(self): + def test_string_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)) + @unittest.skipIf(six.PY2, "Field doesn't exist on Python 2.") 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_filter_reverse_fk(self): self.assert_pickles(Group.objects.filter(event=1))