From b423873cb7574a8088e32d3e23f4d01a99fefeb2 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 23 Jan 2020 10:28:36 +0100 Subject: [PATCH] Refs #31197 -- Added tests for combined expressions in CheckConstraint.check. Thanks Adam Johnson for the report. Fixed in 306b6875209cfedce2536a6679e69adee7c9bc6a. --- tests/migrations/test_operations.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 2a57b43ec4..02971d7a9f 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -1816,6 +1816,34 @@ class OperationTests(OperationTestBase): Pony(pink=3, weight=3.0), ]) + @skipUnlessDBFeature('supports_table_check_constraints') + def test_add_constraint_combinable(self): + app_label = 'test_addconstraint_combinable' + operations = [ + CreateModel( + 'Book', + fields=[ + ('id', models.AutoField(primary_key=True)), + ('read', models.PositiveIntegerField()), + ('unread', models.PositiveIntegerField()), + ], + ), + ] + from_state = self.apply_operations(app_label, ProjectState(), operations) + constraint = models.CheckConstraint( + check=models.Q(read=(100 - models.F('unread'))), + name='test_addconstraint_combinable_sum_100', + ) + operation = migrations.AddConstraint('Book', constraint) + to_state = from_state.clone() + operation.state_forwards(app_label, to_state) + with connection.schema_editor() as editor: + operation.database_forwards(app_label, editor, from_state, to_state) + Book = to_state.apps.get_model(app_label, 'Book') + with self.assertRaises(IntegrityError), transaction.atomic(): + Book.objects.create(read=70, unread=10) + Book.objects.create(read=70, unread=30) + @skipUnlessDBFeature('supports_table_check_constraints') def test_remove_constraint(self): project_state = self.set_up_test_model("test_removeconstraint", constraints=[