summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDavid Sanders <dsanders11@ucsbalum.com>2016-06-29 12:17:40 -0400
committerTim Graham <timograham@gmail.com>2016-06-29 12:36:37 -0400
commit06acb3445f6a2decf17f9b7be91a6637024e02c1 (patch)
treedd3c170de297e519f1dd97eaba32cde6fbe80836 /tests
parent18571aefe6dcb7d69b910ff57d83221a739c790c (diff)
Added a test for updating an annotated queryset.
Diffstat (limited to 'tests')
-rw-r--r--tests/update/tests.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/update/tests.py b/tests/update/tests.py
index 4379809c08..3dc97c9173 100644
--- a/tests/update/tests.py
+++ b/tests/update/tests.py
@@ -1,5 +1,7 @@
from __future__ import unicode_literals
+from django.core.exceptions import FieldError
+from django.db.models import F, Max
from django.test import TestCase
from .models import A, B, Bar, D, DataPoint, Foo, RelatedPoint
@@ -138,3 +140,21 @@ class AdvancedTests(TestCase):
self.assertEqual(bar_qs[0].foo_id, a_foo.target)
bar_qs.update(foo=b_foo)
self.assertEqual(bar_qs[0].foo_id, b_foo.target)
+
+ def test_update_annotated_queryset(self):
+ """
+ Update of a queryset that's been annotated.
+ """
+ # Trivial annotated update
+ qs = DataPoint.objects.annotate(alias=F('value'))
+ self.assertEqual(qs.update(another_value='foo'), 3)
+ # Update where annotation is used for filtering
+ qs = DataPoint.objects.annotate(alias=F('value')).filter(alias='apple')
+ self.assertEqual(qs.update(another_value='foo'), 1)
+ # Update where annotation is used in update parameters
+ qs = DataPoint.objects.annotate(alias=F('value'))
+ self.assertEqual(qs.update(another_value=F('alias')), 3)
+ # Update where aggregation annotation is used in update parameters
+ qs = DataPoint.objects.annotate(max=Max('value'))
+ with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'):
+ qs.update(another_value=F('max'))