Fixed a couple of `assert` syntax warnings mistakingly introduced in r16966 and added some tests to prevent future regressions. Thanks to Anssi Kääriäinen for the report.

Refs #12467.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16971 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Julien Phalip 2011-10-13 11:53:59 +00:00
parent 639400d52f
commit c58e5724bc
3 changed files with 20 additions and 4 deletions

View File

@ -510,8 +510,8 @@ class AutoField(Field):
'invalid': _(u"'%s' value must be an integer."), 'invalid': _(u"'%s' value must be an integer."),
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
assert (kwargs.get('primary_key', False) is True, assert kwargs.get('primary_key', False) is True, \
"%ss must have primary_key=True." % self.__class__.__name__) "%ss must have primary_key=True." % self.__class__.__name__
kwargs['blank'] = True kwargs['blank'] = True
Field.__init__(self, *args, **kwargs) Field.__init__(self, *args, **kwargs)
@ -536,8 +536,8 @@ class AutoField(Field):
return int(value) return int(value)
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
assert (not cls._meta.has_auto_field, assert not cls._meta.has_auto_field, \
"A model can't have more than one AutoField.") "A model can't have more than one AutoField."
super(AutoField, self).contribute_to_class(cls, name) super(AutoField, self).contribute_to_class(cls, name)
cls._meta.has_auto_field = True cls._meta.has_auto_field = True
cls._meta.auto_field = self cls._meta.auto_field = self

View File

@ -90,3 +90,13 @@ class GenericIPAddressTestModel(models.Model):
class GenericIPAddrUnpackUniqueTest(models.Model): class GenericIPAddrUnpackUniqueTest(models.Model):
generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True) 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."

View File

@ -12,6 +12,12 @@ class ValidationMessagesTest(TestCase):
f.clean('foo', None) f.clean('foo', None)
except ValidationError, e: except ValidationError, e:
self.assertEqual(e.messages, [u"'foo' value must be an integer."]) 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): def test_integer_field_raises_error_message(self):
f = models.IntegerField() f = models.IntegerField()