Skip to content

Commit 27b07a3

Browse files
authored
Refs #30581 -- Moved CheckConstraint tests for conditional expressions to migrations.test_operations.
This allows avoiding warning in tests about using RawSQL in CheckConstraints.
1 parent c5cc750 commit 27b07a3

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

tests/constraints/models.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ class Meta:
1919
check=models.Q(price__gt=0),
2020
name="%(app_label)s_%(class)s_price_gt_0",
2121
),
22-
models.CheckConstraint(
23-
check=models.expressions.RawSQL(
24-
"price < %s", (1000,), output_field=models.BooleanField()
25-
),
26-
name="%(app_label)s_price_lt_1000_raw",
27-
),
28-
models.CheckConstraint(
29-
check=models.expressions.ExpressionWrapper(
30-
models.Q(price__gt=500) | models.Q(price__lt=500),
31-
output_field=models.BooleanField(),
32-
),
33-
name="%(app_label)s_price_neq_500_wrap",
34-
),
3522
models.CheckConstraint(
3623
check=models.Q(
3724
models.Q(unit__isnull=True) | models.Q(unit__in=["μg/mL", "ng/mL"])

tests/constraints/tests.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,13 @@ def test_database_constraint_unicode(self):
103103
with self.assertRaises(IntegrityError):
104104
Product.objects.create(price=10, discounted_price=7, unit="l")
105105

106-
@skipUnlessDBFeature("supports_table_check_constraints")
107-
def test_database_constraint_expression(self):
108-
Product.objects.create(price=999, discounted_price=5)
109-
with self.assertRaises(IntegrityError):
110-
Product.objects.create(price=1000, discounted_price=5)
111-
112-
@skipUnlessDBFeature("supports_table_check_constraints")
113-
def test_database_constraint_expressionwrapper(self):
114-
Product.objects.create(price=499, discounted_price=5)
115-
with self.assertRaises(IntegrityError):
116-
Product.objects.create(price=500, discounted_price=5)
117-
118106
@skipUnlessDBFeature(
119107
"supports_table_check_constraints", "can_introspect_check_constraints"
120108
)
121109
def test_name(self):
122110
constraints = get_constraints(Product._meta.db_table)
123111
for expected_name in (
124112
"price_gt_discounted_price",
125-
"constraints_price_lt_1000_raw",
126-
"constraints_price_neq_500_wrap",
127113
"constraints_product_price_gt_0",
128114
):
129115
with self.subTest(expected_name):

tests/migrations/test_operations.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,48 @@ def test_create_model_with_constraint(self):
468468
self.assertEqual(definition[1], [])
469469
self.assertEqual(definition[2]["options"]["constraints"], [check_constraint])
470470

471+
@skipUnlessDBFeature("supports_table_check_constraints")
472+
def test_create_model_with_boolean_expression_in_check_constraint(self):
473+
app_label = "test_crmobechc"
474+
rawsql_constraint = models.CheckConstraint(
475+
check=models.expressions.RawSQL(
476+
"price < %s", (1000,), output_field=models.BooleanField()
477+
),
478+
name=f"{app_label}_price_lt_1000_raw",
479+
)
480+
wrapper_constraint = models.CheckConstraint(
481+
check=models.expressions.ExpressionWrapper(
482+
models.Q(price__gt=500) | models.Q(price__lt=500),
483+
output_field=models.BooleanField(),
484+
),
485+
name=f"{app_label}_price_neq_500_wrap",
486+
)
487+
operation = migrations.CreateModel(
488+
"Product",
489+
[
490+
("id", models.AutoField(primary_key=True)),
491+
("price", models.IntegerField(null=True)),
492+
],
493+
options={"constraints": [rawsql_constraint, wrapper_constraint]},
494+
)
495+
496+
project_state = ProjectState()
497+
new_state = project_state.clone()
498+
operation.state_forwards(app_label, new_state)
499+
# Add table.
500+
self.assertTableNotExists(app_label)
501+
with connection.schema_editor() as editor:
502+
operation.database_forwards(app_label, editor, project_state, new_state)
503+
self.assertTableExists(f"{app_label}_product")
504+
insert_sql = f"INSERT INTO {app_label}_product (id, price) VALUES (%d, %d)"
505+
with connection.cursor() as cursor:
506+
with self.assertRaises(IntegrityError):
507+
cursor.execute(insert_sql % (1, 1000))
508+
cursor.execute(insert_sql % (1, 999))
509+
with self.assertRaises(IntegrityError):
510+
cursor.execute(insert_sql % (2, 500))
511+
cursor.execute(insert_sql % (2, 499))
512+
471513
def test_create_model_with_partial_unique_constraint(self):
472514
partial_unique_constraint = models.UniqueConstraint(
473515
fields=["pink"],

0 commit comments

Comments
 (0)