Skip to content

Django test failure: test_validate_case_when #362

@timgraham

Description

@timgraham

A tested added in django/django@b3bb723 fails, even after adopting in for MongoDB (MongoDB doesn't support isnull constraints; see commit "constraints edits for partial indexes" on the Django fork):

diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
index f93f9f228c..665f052e72 100644
--- a/tests/constraints/tests.py
+++ b/tests/constraints/tests.py
@@ -1079,16 +1079,16 @@ class UniqueConstraintTests(TestCase):
         )
 
     def test_validate_case_when(self):
-        UniqueConstraintProduct.objects.create(name="p1")
+        UniqueConstraintProduct.objects.create(name="p1", color="blue")
         constraint = models.UniqueConstraint(
-            Case(When(color__isnull=True, then=F("name"))),
-            name="name_without_color_uniq",
+            Case(When(color="blue", then=F("name"))),
+            name="blue_names_uniq",
         )
-        msg = "Constraint “name_without_color_uniq” is violated."
+        msg = "Constraint “blue_names_uniq” is violated."
         with self.assertRaisesMessage(ValidationError, msg):
             constraint.validate(
                 UniqueConstraintProduct,
-                UniqueConstraintProduct(name="p1"),
+                UniqueConstraintProduct(name="p1", color="blue"),
             )
         constraint.validate(
             UniqueConstraintProduct,
./tests/runtests.py --settings=test_mongo --debug-sql constraints.tests.UniqueConstraintTests.test_validate_case_when
...
======================================================================
ERROR: test_validate_case_when (constraints.tests.UniqueConstraintTests.test_validate_case_when)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/tests/constraints/tests.py", line 1093, in test_validate_case_when
    constraint.validate(
  File "/home/tim/code/django/django/db/models/constraints.py", line 711, in validate
    raise ValidationError(
django.core.exceptions.ValidationError: ['Constraint “blue_names_uniq” is violated.']

----------------------------------------------------------------------
(0.004) db.constraints_uniqueconstraintproduct.insert_many([{'name': 'p1', 'color': 'blue', 'age': None, 'updated': None}]);

(0.002) db.constraints_uniqueconstraintproduct.aggregate([{'$match': {'$expr': {'$eq': [{'$switch': {'branches': [{'case': {'$eq': ['$color', 'blue']}, 'then': '$name'}], 'default': NONE}}, {'$switch': {'branches': [{'case': {'$eq': ['blue', 'blue']}, 'then': 'p1'}], 'default': NONE}}]}}}, {'$project': {'a': {'$literal': 1}}}, {'$limit': 1}]);

(0.002) db.constraints_uniqueconstraintproduct.aggregate([{'$match': {'$expr': {'$eq': [{'$switch': {'branches': [{'case': {'$eq': ['$color', 'blue']}, 'then': '$name'}], 'default': NONE}}, {'$switch': {'branches': [{'case': {'$eq': ['green', 'blue']}, 'then': 'p1'}], 'default': NONE}}]}}}, {'$project': {'a': {'$literal': 1}}}, {'$limit': 1}]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions