summaryrefslogtreecommitdiff
path: root/tests/model_fields/test_autofield.py
diff options
context:
space:
mode:
authorNick Pope <nick.pope@flightdataservices.com>2017-12-11 15:36:33 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-08-20 09:22:25 +0200
commit21e559495b8255bba1e8a4429cd083246ab90457 (patch)
tree7c28928a42b896f05be9282461dcc0e1db08c03e /tests/model_fields/test_autofield.py
parentb10d322c41f66dc7c77c36f90a3532269b25ea93 (diff)
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.
Diffstat (limited to 'tests/model_fields/test_autofield.py')
-rw-r--r--tests/model_fields/test_autofield.py42
1 files changed, 21 insertions, 21 deletions
diff --git a/tests/model_fields/test_autofield.py b/tests/model_fields/test_autofield.py
index 73220ae62b..db8e79e34b 100644
--- a/tests/model_fields/test_autofield.py
+++ b/tests/model_fields/test_autofield.py
@@ -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))