diff options
| author | Simon Charette <charette.s@gmail.com> | 2024-02-19 00:20:13 -0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-02-29 12:22:17 +0100 |
| commit | f82c67aa217c8dd4320ef697b04a6da1681aa799 (patch) | |
| tree | d48d1a748c81269c3bc18cc5c25395da7096eb5e /tests/postgres_tests | |
| parent | 0fb104dda287431f5ab74532e45e8471e22b58c8 (diff) | |
Fixed #35234 -- Added system checks for invalid model field names in ExclusionConstraint.expressions.
Diffstat (limited to 'tests/postgres_tests')
| -rw-r--r-- | tests/postgres_tests/test_constraints.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py index e5a8e9dbe9..ee0be3cbb3 100644 --- a/tests/postgres_tests/test_constraints.py +++ b/tests/postgres_tests/test_constraints.py @@ -2,12 +2,17 @@ import datetime from unittest import mock from django.contrib.postgres.indexes import OpClass +from django.core.checks import Error from django.core.exceptions import ValidationError from django.db import IntegrityError, NotSupportedError, connection, transaction from django.db.models import ( + CASCADE, + CharField, CheckConstraint, + DateField, Deferrable, F, + ForeignKey, Func, IntegerField, Model, @@ -328,6 +333,57 @@ class ExclusionConstraintTests(PostgreSQLTestCase): include="invalid", ) + @isolate_apps("postgres_tests") + def test_check(self): + class Author(Model): + name = CharField(max_length=255) + alias = CharField(max_length=255) + + class Meta: + app_label = "postgres_tests" + + class Book(Model): + title = CharField(max_length=255) + published_date = DateField() + author = ForeignKey(Author, CASCADE) + + class Meta: + app_label = "postgres_tests" + constraints = [ + ExclusionConstraint( + name="exclude_check", + expressions=[ + (F("title"), RangeOperators.EQUAL), + (F("published_date__year"), RangeOperators.EQUAL), + ("published_date__month", RangeOperators.EQUAL), + (F("author__name"), RangeOperators.EQUAL), + ("author__alias", RangeOperators.EQUAL), + ("nonexistent", RangeOperators.EQUAL), + ], + ) + ] + + self.assertCountEqual( + Book.check(databases=self.databases), + [ + Error( + "'constraints' refers to the nonexistent field 'nonexistent'.", + obj=Book, + id="models.E012", + ), + Error( + "'constraints' refers to the joined field 'author__alias'.", + obj=Book, + id="models.E041", + ), + Error( + "'constraints' refers to the joined field 'author__name'.", + obj=Book, + id="models.E041", + ), + ], + ) + def test_repr(self): constraint = ExclusionConstraint( name="exclude_overlapping", |
