summaryrefslogtreecommitdiff
path: root/tests/constraints
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2023-09-22 06:01:11 +0200
committerGitHub <noreply@github.com>2023-09-22 06:01:11 +0200
commit779cd28acb1f7eb06f629c0ea4ded99b5ebb670a (patch)
treebbb25f0c59749eb0d1688bd65f595ee8a0e0d0c0 /tests/constraints
parent78b5c9075348aa12da2e024f6ece29d1d652dfdd (diff)
Fixed #34840 -- Avoided casting string base fields on PostgreSQL.
Thanks Alex Vandiver for the report. Regression in 09ffc5c1212d4ced58b708cbbf3dfbfb77b782ca.
Diffstat (limited to 'tests/constraints')
-rw-r--r--tests/constraints/tests.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
index 55397449d9..e1b95baf60 100644
--- a/tests/constraints/tests.py
+++ b/tests/constraints/tests.py
@@ -995,6 +995,42 @@ class UniqueConstraintTests(TestCase):
exclude={"name"},
)
+ def test_validate_nullable_textfield_with_isnull_true(self):
+ is_null_constraint = models.UniqueConstraint(
+ "price",
+ "discounted_price",
+ condition=models.Q(unit__isnull=True),
+ name="uniq_prices_no_unit",
+ )
+ is_not_null_constraint = models.UniqueConstraint(
+ "price",
+ "discounted_price",
+ condition=models.Q(unit__isnull=False),
+ name="uniq_prices_unit",
+ )
+
+ Product.objects.create(price=2, discounted_price=1)
+ Product.objects.create(price=4, discounted_price=3, unit="ng/mL")
+
+ msg = "Constraint “uniq_prices_no_unit” is violated."
+ with self.assertRaisesMessage(ValidationError, msg):
+ is_null_constraint.validate(
+ Product, Product(price=2, discounted_price=1, unit=None)
+ )
+ is_null_constraint.validate(
+ Product, Product(price=2, discounted_price=1, unit="ng/mL")
+ )
+ is_null_constraint.validate(Product, Product(price=4, discounted_price=3))
+
+ msg = "Constraint “uniq_prices_unit” is violated."
+ with self.assertRaisesMessage(ValidationError, msg):
+ is_not_null_constraint.validate(
+ Product,
+ Product(price=4, discounted_price=3, unit="μg/mL"),
+ )
+ is_not_null_constraint.validate(Product, Product(price=4, discounted_price=3))
+ is_not_null_constraint.validate(Product, Product(price=2, discounted_price=1))
+
def test_name(self):
constraints = get_constraints(UniqueConstraintProduct._meta.db_table)
expected_name = "name_color_uniq"