mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #29979, Refs #17337 -- Extracted AutoField field logic into a mixin and refactored AutoFields.
This reduces duplication by allowing AutoField, BigAutoField and SmallAutoField to inherit from IntegerField, BigIntegerField and SmallIntegerField respectively. Doing so also allows for enabling the max_length warning check and minimum/maximum value validation for auto fields, as well as providing a mixin that can be used for other possible future auto field types such as a theoretical UUIDAutoField.
This commit is contained in:
committed by
Mariusz Felisiak
parent
b10d322c41
commit
21e559495b
@@ -1,32 +1,32 @@
|
||||
from django.test import TestCase
|
||||
from django.db import models
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from .models import AutoModel, BigAutoModel, SmallAutoModel
|
||||
from .test_integerfield import (
|
||||
BigIntegerFieldTests, IntegerFieldTests, SmallIntegerFieldTests,
|
||||
)
|
||||
|
||||
|
||||
class AutoFieldTests(TestCase):
|
||||
class AutoFieldTests(IntegerFieldTests):
|
||||
model = AutoModel
|
||||
|
||||
def test_invalid_value(self):
|
||||
tests = [
|
||||
(TypeError, ()),
|
||||
(TypeError, []),
|
||||
(TypeError, {}),
|
||||
(TypeError, set()),
|
||||
(TypeError, object()),
|
||||
(TypeError, complex()),
|
||||
(ValueError, 'non-numeric string'),
|
||||
(ValueError, b'non-numeric byte-string'),
|
||||
]
|
||||
for exception, value in tests:
|
||||
with self.subTest(value=value):
|
||||
msg = "Field 'value' expected a number but got %r." % (value,)
|
||||
with self.assertRaisesMessage(exception, msg):
|
||||
self.model.objects.create(value=value)
|
||||
|
||||
|
||||
class BigAutoFieldTests(AutoFieldTests):
|
||||
class BigAutoFieldTests(BigIntegerFieldTests):
|
||||
model = BigAutoModel
|
||||
|
||||
|
||||
class SmallAutoFieldTests(AutoFieldTests):
|
||||
class SmallAutoFieldTests(SmallIntegerFieldTests):
|
||||
model = SmallAutoModel
|
||||
|
||||
|
||||
class AutoFieldInheritanceTests(SimpleTestCase):
|
||||
|
||||
def test_isinstance_of_autofield(self):
|
||||
for field in (models.BigAutoField, models.SmallAutoField):
|
||||
with self.subTest(field.__name__):
|
||||
self.assertIsInstance(field(), models.AutoField)
|
||||
|
||||
def test_issubclass_of_autofield(self):
|
||||
for field in (models.BigAutoField, models.SmallAutoField):
|
||||
with self.subTest(field.__name__):
|
||||
self.assertTrue(issubclass(field, models.AutoField))
|
||||
|
||||
Reference in New Issue
Block a user