diff options
author | Gagaro <gagaro42@gmail.com> | 2022-01-31 16:04:13 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-05-10 11:22:23 +0200 |
commit | 667105877e6723c6985399803a364848891513cc (patch) | |
tree | b6b3a9fe9f2c8767bc6f6a68f0580eef021b2b55 /tests/invalid_models_tests | |
parent | 441103a04d1d167dc870eaaf90e3fba974f67c93 (diff) | |
download | django-667105877e6723c6985399803a364848891513cc.tar.gz |
Fixed #30581 -- Added support for Meta.constraints validation.
Thanks Simon Charette, Keryn Knight, and Mariusz Felisiak for reviews.
Diffstat (limited to 'tests/invalid_models_tests')
-rw-r--r-- | tests/invalid_models_tests/test_models.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py index 5ea830d0ec..08aca62850 100644 --- a/tests/invalid_models_tests/test_models.py +++ b/tests/invalid_models_tests/test_models.py @@ -2198,6 +2198,66 @@ class ConstraintsTests(TestCase): ] self.assertCountEqual(errors, expected_errors) + def test_check_constraint_raw_sql_check(self): + class Model(models.Model): + class Meta: + required_db_features = {"supports_table_check_constraints"} + constraints = [ + models.CheckConstraint(check=models.Q(id__gt=0), name="q_check"), + models.CheckConstraint( + check=models.ExpressionWrapper( + models.Q(price__gt=20), + output_field=models.BooleanField(), + ), + name="expression_wrapper_check", + ), + models.CheckConstraint( + check=models.expressions.RawSQL( + "id = 0", + params=(), + output_field=models.BooleanField(), + ), + name="raw_sql_check", + ), + models.CheckConstraint( + check=models.Q( + models.ExpressionWrapper( + models.Q( + models.expressions.RawSQL( + "id = 0", + params=(), + output_field=models.BooleanField(), + ) + ), + output_field=models.BooleanField(), + ) + ), + name="nested_raw_sql_check", + ), + ] + + expected_warnings = ( + [ + Warning( + "Check constraint 'raw_sql_check' contains RawSQL() expression and " + "won't be validated during the model full_clean().", + hint="Silence this warning if you don't care about it.", + obj=Model, + id="models.W045", + ), + Warning( + "Check constraint 'nested_raw_sql_check' contains RawSQL() " + "expression and won't be validated during the model full_clean().", + hint="Silence this warning if you don't care about it.", + obj=Model, + id="models.W045", + ), + ] + if connection.features.supports_table_check_constraints + else [] + ) + self.assertEqual(Model.check(databases=self.databases), expected_warnings) + def test_unique_constraint_with_condition(self): class Model(models.Model): age = models.IntegerField() |