summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests
diff options
context:
space:
mode:
authorhui shang <shangdahao@gmail.com>2017-12-28 07:56:24 +0800
committerTim Graham <timograham@gmail.com>2017-12-27 18:56:24 -0500
commitf1aa58479cdc6051dd2e97feca2d584c43aee1e7 (patch)
tree1f59109f6c02462716ae76bb45c26548c918164b /tests/invalid_models_tests
parent1d00923848d504c6132019492b8d5a6cdf8261db (diff)
Fixed #28714 -- Added system checks for invalid model field names in Meta.indexes.
Thanks Gabriel for the report and Adam Johnson for the review.
Diffstat (limited to 'tests/invalid_models_tests')
-rw-r--r--tests/invalid_models_tests/test_models.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py
index 4a3e5e3a02..71e1b83a3f 100644
--- a/tests/invalid_models_tests/test_models.py
+++ b/tests/invalid_models_tests/test_models.py
@@ -220,6 +220,59 @@ class UniqueTogetherTests(SimpleTestCase):
@isolate_apps('invalid_models_tests')
+class IndexesTests(SimpleTestCase):
+
+ def test_pointing_to_missing_field(self):
+ class Model(models.Model):
+ class Meta:
+ indexes = [models.Index(fields=['missing_field'], name='name')]
+
+ self.assertEqual(Model.check(), [
+ Error(
+ "'indexes' refers to the nonexistent field 'missing_field'.",
+ obj=Model,
+ id='models.E012',
+ ),
+ ])
+
+ def test_pointing_to_m2m_field(self):
+ class Model(models.Model):
+ m2m = models.ManyToManyField('self')
+
+ class Meta:
+ indexes = [models.Index(fields=['m2m'], name='name')]
+
+ self.assertEqual(Model.check(), [
+ Error(
+ "'indexes' refers to a ManyToManyField 'm2m', but "
+ "ManyToManyFields are not permitted in 'indexes'.",
+ obj=Model,
+ id='models.E013',
+ ),
+ ])
+
+ def test_pointing_to_non_local_field(self):
+ class Foo(models.Model):
+ field1 = models.IntegerField()
+
+ class Bar(Foo):
+ field2 = models.IntegerField()
+
+ class Meta:
+ indexes = [models.Index(fields=['field2', 'field1'], name='name')]
+
+ self.assertEqual(Bar.check(), [
+ Error(
+ "'indexes' refers to field 'field1' which is not local to "
+ "model 'Bar'.",
+ hint='This issue may be caused by multi-table inheritance.',
+ obj=Bar,
+ id='models.E016',
+ ),
+ ])
+
+
+@isolate_apps('invalid_models_tests')
class FieldNamesTests(SimpleTestCase):
def test_ending_with_underscore(self):