summaryrefslogtreecommitdiff
path: root/tests/update
diff options
context:
space:
mode:
authorDavid Wobrock <david.wobrock@gmail.com>2022-09-26 22:59:25 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-10-31 09:55:51 +0100
commita320aab5129f4019b3c1d28b7a3b509582bc56f9 (patch)
tree31d140537fbf59616871a04eeb4d19fde54761f9 /tests/update
parentc01e76c95caa1bd2995f1b6c186cbe8726c92ef9 (diff)
Fixed #16211 -- Added logical NOT support to F expressions.
Diffstat (limited to 'tests/update')
-rw-r--r--tests/update/models.py1
-rw-r--r--tests/update/tests.py30
2 files changed, 29 insertions, 2 deletions
diff --git a/tests/update/models.py b/tests/update/models.py
index d7452dc302..d71fc887c7 100644
--- a/tests/update/models.py
+++ b/tests/update/models.py
@@ -10,6 +10,7 @@ class DataPoint(models.Model):
name = models.CharField(max_length=20)
value = models.CharField(max_length=20)
another_value = models.CharField(max_length=20, blank=True)
+ is_active = models.BooleanField(default=True)
class RelatedPoint(models.Model):
diff --git a/tests/update/tests.py b/tests/update/tests.py
index 2162f5164d..e88eeda96d 100644
--- a/tests/update/tests.py
+++ b/tests/update/tests.py
@@ -2,7 +2,7 @@ import unittest
from django.core.exceptions import FieldError
from django.db import IntegrityError, connection, transaction
-from django.db.models import CharField, Count, F, IntegerField, Max
+from django.db.models import Case, CharField, Count, F, IntegerField, Max, When
from django.db.models.functions import Abs, Concat, Lower
from django.test import TestCase
from django.test.utils import register_lookup
@@ -81,7 +81,7 @@ class AdvancedTests(TestCase):
def setUpTestData(cls):
cls.d0 = DataPoint.objects.create(name="d0", value="apple")
cls.d2 = DataPoint.objects.create(name="d2", value="banana")
- cls.d3 = DataPoint.objects.create(name="d3", value="banana")
+ cls.d3 = DataPoint.objects.create(name="d3", value="banana", is_active=False)
cls.r1 = RelatedPoint.objects.create(name="r1", data=cls.d3)
def test_update(self):
@@ -249,6 +249,32 @@ class AdvancedTests(TestCase):
Bar.objects.annotate(abs_id=Abs("m2m_foo")).order_by("abs_id").update(x=3)
self.assertEqual(Bar.objects.get().x, 3)
+ def test_update_negated_f(self):
+ DataPoint.objects.update(is_active=~F("is_active"))
+ self.assertCountEqual(
+ DataPoint.objects.values_list("name", "is_active"),
+ [("d0", False), ("d2", False), ("d3", True)],
+ )
+ DataPoint.objects.update(is_active=~F("is_active"))
+ self.assertCountEqual(
+ DataPoint.objects.values_list("name", "is_active"),
+ [("d0", True), ("d2", True), ("d3", False)],
+ )
+
+ def test_update_negated_f_conditional_annotation(self):
+ DataPoint.objects.annotate(
+ is_d2=Case(When(name="d2", then=True), default=False)
+ ).update(is_active=~F("is_d2"))
+ self.assertCountEqual(
+ DataPoint.objects.values_list("name", "is_active"),
+ [("d0", True), ("d2", False), ("d3", True)],
+ )
+
+ def test_updating_non_conditional_field(self):
+ msg = "Cannot negate non-conditional expressions."
+ with self.assertRaisesMessage(TypeError, msg):
+ DataPoint.objects.update(is_active=~F("name"))
+
@unittest.skipUnless(
connection.vendor == "mysql",