diff options
| author | Nick Pope <nick.pope@flightdataservices.com> | 2017-12-11 15:36:33 +0000 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-08-20 09:22:25 +0200 |
| commit | 21e559495b8255bba1e8a4429cd083246ab90457 (patch) | |
| tree | 7c28928a42b896f05be9282461dcc0e1db08c03e /tests | |
| parent | b10d322c41f66dc7c77c36f90a3532269b25ea93 (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')
| -rw-r--r-- | tests/invalid_models_tests/test_ordinary_fields.py | 15 | ||||
| -rw-r--r-- | tests/model_fields/test_autofield.py | 42 | ||||
| -rw-r--r-- | tests/model_fields/test_integerfield.py | 2 | ||||
| -rw-r--r-- | tests/validation/models.py | 5 |
4 files changed, 41 insertions, 23 deletions
diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index a3231ee36a..5bb1847a70 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -38,6 +38,21 @@ class AutoFieldTests(SimpleTestCase): ), ]) + def test_max_length_warning(self): + class Model(models.Model): + auto = models.AutoField(primary_key=True, max_length=2) + + field = Model._meta.get_field('auto') + self.assertEqual(field.check(), [ + DjangoWarning( + "'max_length' is ignored when used with %s." + % field.__class__.__name__, + hint="Remove 'max_length' from field", + obj=field, + id='fields.W122', + ), + ]) + @isolate_apps('invalid_models_tests') class BinaryFieldTests(SimpleTestCase): 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)) diff --git a/tests/model_fields/test_integerfield.py b/tests/model_fields/test_integerfield.py index c72cb48015..c0bb7627cf 100644 --- a/tests/model_fields/test_integerfield.py +++ b/tests/model_fields/test_integerfield.py @@ -125,7 +125,7 @@ class IntegerFieldTests(TestCase): ranged_value_field.run_validators(max_backend_value + 1) def test_types(self): - instance = self.model(value=0) + instance = self.model(value=1) self.assertIsInstance(instance.value, int) instance.save() self.assertIsInstance(instance.value, int) diff --git a/tests/validation/models.py b/tests/validation/models.py index 953370bc37..e8e18cfbce 100644 --- a/tests/validation/models.py +++ b/tests/validation/models.py @@ -130,4 +130,7 @@ try: auto2 = models.AutoField(primary_key=True) except AssertionError as exc: assertion_error = exc -assert str(assertion_error) == "Model validation.MultipleAutoFields can't have more than one AutoField." +assert str(assertion_error) == ( + "Model validation.MultipleAutoFields can't have more than one " + "auto-generated field." +) |
