summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2019-03-01 17:09:33 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-03-01 17:09:33 +0100
commit440505cb2cadbe1a5b9fba246bcde6c04f51d07e (patch)
treeefa7f88f683ecf7246a06600dd677b832c326e7b /tests/invalid_models_tests
parent4dcbe6eb2de38a856dae39928692e46fbcf5c475 (diff)
Fixed #29408 -- Added validation of related fields and lookups in model Meta.ordering.
Diffstat (limited to 'tests/invalid_models_tests')
-rw-r--r--tests/invalid_models_tests/test_models.py86
1 files changed, 85 insertions, 1 deletions
diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py
index 5b7042e127..320e799a6e 100644
--- a/tests/invalid_models_tests/test_models.py
+++ b/tests/invalid_models_tests/test_models.py
@@ -5,9 +5,10 @@ from django.core.checks import Error, Warning
from django.core.checks.model_checks import _check_lazy_references
from django.core.exceptions import ImproperlyConfigured
from django.db import connection, connections, models
+from django.db.models.functions import Lower
from django.db.models.signals import post_init
from django.test import SimpleTestCase
-from django.test.utils import isolate_apps, override_settings
+from django.test.utils import isolate_apps, override_settings, register_lookup
def get_max_column_name_length():
@@ -665,6 +666,89 @@ class OtherModelTests(SimpleTestCase):
)
])
+ def test_ordering_pointing_to_missing_related_field(self):
+ class Model(models.Model):
+ test = models.IntegerField()
+
+ class Meta:
+ ordering = ('missing_related__id',)
+
+ self.assertEqual(Model.check(), [
+ Error(
+ "'ordering' refers to the nonexistent field, related field "
+ "or lookup 'missing_related__id'.",
+ obj=Model,
+ id='models.E015',
+ )
+ ])
+
+ def test_ordering_pointing_to_missing_related_model_field(self):
+ class Parent(models.Model):
+ pass
+
+ class Child(models.Model):
+ parent = models.ForeignKey(Parent, models.CASCADE)
+
+ class Meta:
+ ordering = ('parent__missing_field',)
+
+ self.assertEqual(Child.check(), [
+ Error(
+ "'ordering' refers to the nonexistent field, related field "
+ "or lookup 'parent__missing_field'.",
+ obj=Child,
+ id='models.E015',
+ )
+ ])
+
+ def test_ordering_pointing_to_non_related_field(self):
+ class Child(models.Model):
+ parent = models.IntegerField()
+
+ class Meta:
+ ordering = ('parent__missing_field',)
+
+ self.assertEqual(Child.check(), [
+ Error(
+ "'ordering' refers to the nonexistent field, related field "
+ "or lookup 'parent__missing_field'.",
+ obj=Child,
+ id='models.E015',
+ )
+ ])
+
+ def test_ordering_pointing_to_two_related_model_field(self):
+ class Parent2(models.Model):
+ pass
+
+ class Parent1(models.Model):
+ parent2 = models.ForeignKey(Parent2, models.CASCADE)
+
+ class Child(models.Model):
+ parent1 = models.ForeignKey(Parent1, models.CASCADE)
+
+ class Meta:
+ ordering = ('parent1__parent2__missing_field',)
+
+ self.assertEqual(Child.check(), [
+ Error(
+ "'ordering' refers to the nonexistent field, related field "
+ "or lookup 'parent1__parent2__missing_field'.",
+ obj=Child,
+ id='models.E015',
+ )
+ ])
+
+ def test_ordering_allows_registered_lookups(self):
+ class Model(models.Model):
+ test = models.CharField(max_length=100)
+
+ class Meta:
+ ordering = ('test__lower',)
+
+ with register_lookup(models.CharField, Lower):
+ self.assertEqual(Model.check(), [])
+
def test_ordering_pointing_to_foreignkey_field(self):
class Parent(models.Model):
pass