From c58e5724bcebc74ccfbc49a4a29de6fd0fa6103e Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Thu, 13 Oct 2011 11:53:59 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20a=20couple=20of=20`assert`=20syntax=20w?= =?UTF-8?q?arnings=20mistakingly=20introduced=20in=20r16966=20and=20added?= =?UTF-8?q?=20some=20tests=20to=20prevent=20future=20regressions.=20Thanks?= =?UTF-8?q?=20to=20Anssi=20Ka=CC=88a=CC=88ria=CC=88inen=20for=20the=20repo?= =?UTF-8?q?rt.=20Refs=20#12467.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@16971 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/__init__.py | 8 ++++---- tests/modeltests/validation/models.py | 10 ++++++++++ tests/modeltests/validation/test_error_messages.py | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 7e119c2f86..67acbbae76 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -510,8 +510,8 @@ class AutoField(Field): 'invalid': _(u"'%s' value must be an integer."), } def __init__(self, *args, **kwargs): - assert (kwargs.get('primary_key', False) is True, - "%ss must have primary_key=True." % self.__class__.__name__) + assert kwargs.get('primary_key', False) is True, \ + "%ss must have primary_key=True." % self.__class__.__name__ kwargs['blank'] = True Field.__init__(self, *args, **kwargs) @@ -536,8 +536,8 @@ class AutoField(Field): return int(value) def contribute_to_class(self, cls, name): - assert (not cls._meta.has_auto_field, - "A model can't have more than one AutoField.") + assert not cls._meta.has_auto_field, \ + "A model can't have more than one AutoField." super(AutoField, self).contribute_to_class(cls, name) cls._meta.has_auto_field = True cls._meta.auto_field = self diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py index fca2cef261..182e739daf 100644 --- a/tests/modeltests/validation/models.py +++ b/tests/modeltests/validation/models.py @@ -90,3 +90,13 @@ class GenericIPAddressTestModel(models.Model): class GenericIPAddrUnpackUniqueTest(models.Model): generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True) + + +try: + # A model can't have multiple AutoFields + # Refs #12467. + class MultipleAutoFields(models.Model): + auto1 = models.AutoField(primary_key=True) + auto2 = models.AutoField(primary_key=True) +except AssertionError, e: + assert e.message == u"A model can't have more than one AutoField." \ No newline at end of file diff --git a/tests/modeltests/validation/test_error_messages.py b/tests/modeltests/validation/test_error_messages.py index 43b707e632..299a93a41a 100644 --- a/tests/modeltests/validation/test_error_messages.py +++ b/tests/modeltests/validation/test_error_messages.py @@ -12,6 +12,12 @@ class ValidationMessagesTest(TestCase): f.clean('foo', None) except ValidationError, e: self.assertEqual(e.messages, [u"'foo' value must be an integer."]) + # primary_key must be True. Refs #12467. + self.assertRaises(AssertionError, models.AutoField, 'primary_key', False) + try: + models.AutoField(primary_key=False) + except AssertionError, e: + self.assertEqual(e.message, u"AutoFields must have primary_key=True.") def test_integer_field_raises_error_message(self): f = models.IntegerField()