summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRamiro Morales <ramiro@users.noreply.github.com>2018-09-13 13:29:48 -0300
committerTim Graham <timograham@gmail.com>2018-09-13 12:29:48 -0400
commit1b1f64ee5a78cc217fead52cbae23114502cf564 (patch)
treec6baf64db6c3b7a381fd45639b189ef072e4a960 /tests
parentc52ecbda615594750ae59b789313a29893950b3d (diff)
Refs #14357 -- Deprecated Meta.ordering affecting GROUP BY queries.
Thanks Ramiro Morales for contributing to the patch.
Diffstat (limited to 'tests')
-rw-r--r--tests/aggregation_regress/tests.py31
-rw-r--r--tests/ordering/tests.py11
-rw-r--r--tests/queries/test_explain.py6
-rw-r--r--tests/queries/tests.py2
4 files changed, 38 insertions, 12 deletions
diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py
index 7f03d66bab..893b22ae69 100644
--- a/tests/aggregation_regress/tests.py
+++ b/tests/aggregation_regress/tests.py
@@ -11,8 +11,11 @@ from django.db.models import (
Avg, Case, Count, DecimalField, F, IntegerField, Max, Q, StdDev, Sum,
Value, Variance, When,
)
-from django.test import TestCase, skipUnlessAnyDBFeature, skipUnlessDBFeature
+from django.test import (
+ TestCase, ignore_warnings, skipUnlessAnyDBFeature, skipUnlessDBFeature,
+)
from django.test.utils import Approximate
+from django.utils.deprecation import RemovedInDjango31Warning
from .models import (
Alfa, Author, Book, Bravo, Charlie, Clues, Entries, HardbackBook, ItemTag,
@@ -106,6 +109,7 @@ class AggregationTests(TestCase):
for attr, value in kwargs.items():
self.assertEqual(getattr(obj, attr), value)
+ @ignore_warnings(category=RemovedInDjango31Warning)
def test_annotation_with_value(self):
values = Book.objects.filter(
name='Practical Django Projects',
@@ -213,6 +217,7 @@ class AggregationTests(TestCase):
{'pages__sum': 3703}
)
+ @ignore_warnings(category=RemovedInDjango31Warning)
def test_annotation(self):
# Annotations get combined with extra select clauses
obj = Book.objects.annotate(mean_auth_age=Avg("authors__age")).extra(
@@ -306,7 +311,8 @@ class AggregationTests(TestCase):
# If an annotation isn't included in the values, it can still be used
# in a filter
- qs = Book.objects.annotate(n_authors=Count('authors')).values('name').filter(n_authors__gt=2)
+ with ignore_warnings(category=RemovedInDjango31Warning):
+ qs = Book.objects.annotate(n_authors=Count('authors')).values('name').filter(n_authors__gt=2)
self.assertSequenceEqual(
qs, [
{"name": 'Python Web Development with Django'}
@@ -450,6 +456,7 @@ class AggregationTests(TestCase):
with self.assertRaisesMessage(FieldError, msg):
Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('foo'))
+ @ignore_warnings(category=RemovedInDjango31Warning)
def test_more(self):
# Old-style count aggregations can be mixed with new-style
self.assertEqual(
@@ -679,7 +686,7 @@ class AggregationTests(TestCase):
)
# Regression for #10127 - Empty select_related() works with annotate
- qs = Book.objects.filter(rating__lt=4.5).select_related().annotate(Avg('authors__age'))
+ qs = Book.objects.filter(rating__lt=4.5).select_related().annotate(Avg('authors__age')).order_by('name')
self.assertQuerysetEqual(
qs,
[
@@ -803,6 +810,7 @@ class AggregationTests(TestCase):
with self.assertRaisesMessage(ValueError, msg):
Author.objects.annotate(book_contact_set=Avg('friends__age'))
+ @ignore_warnings(category=RemovedInDjango31Warning)
def test_pickle(self):
# Regression for #10197 -- Queries with aggregates can be pickled.
# First check that pickling is possible at all. No crash = success
@@ -933,7 +941,9 @@ class AggregationTests(TestCase):
{'n_pages': 2078},
)
- qs = HardbackBook.objects.annotate(n_authors=Count('book_ptr__authors')).values('name', 'n_authors')
+ qs = HardbackBook.objects.annotate(
+ n_authors=Count('book_ptr__authors'),
+ ).values('name', 'n_authors').order_by('name')
self.assertSequenceEqual(
qs,
[
@@ -945,7 +955,7 @@ class AggregationTests(TestCase):
],
)
- qs = HardbackBook.objects.annotate(n_authors=Count('authors')).values('name', 'n_authors')
+ qs = HardbackBook.objects.annotate(n_authors=Count('authors')).values('name', 'n_authors').order_by('name')
self.assertSequenceEqual(
qs,
[
@@ -1005,7 +1015,7 @@ class AggregationTests(TestCase):
def test_values_annotate_values(self):
qs = Book.objects.values("name").annotate(
n_authors=Count("authors")
- ).values_list("pk", flat=True)
+ ).values_list("pk", flat=True).order_by('name')
self.assertEqual(list(qs), list(Book.objects.values_list("pk", flat=True)))
def test_having_group_by(self):
@@ -1015,7 +1025,7 @@ class AggregationTests(TestCase):
n_authors=Count("authors")
).filter(
pages__gt=F("n_authors")
- ).values_list("name", flat=True)
+ ).values_list("name", flat=True).order_by('name')
# Results should be the same, all Books have more pages than authors
self.assertEqual(
list(qs), list(Book.objects.values_list("name", flat=True))
@@ -1035,7 +1045,7 @@ class AggregationTests(TestCase):
def test_annotation_disjunction(self):
qs = Book.objects.annotate(n_authors=Count("authors")).filter(
Q(n_authors=2) | Q(name="Python Web Development with Django")
- )
+ ).order_by('name')
self.assertQuerysetEqual(
qs, [
"Artificial Intelligence: A Modern Approach",
@@ -1052,7 +1062,7 @@ class AggregationTests(TestCase):
Q(name="The Definitive Guide to Django: Web Development Done Right") |
(Q(name="Artificial Intelligence: A Modern Approach") & Q(n_authors=3))
)
- )
+ ).order_by('name')
self.assertQuerysetEqual(
qs,
[
@@ -1200,6 +1210,7 @@ class AggregationTests(TestCase):
{'book__count__max': 2}
)
+ @ignore_warnings(category=RemovedInDjango31Warning)
def test_annotate_joins(self):
"""
The base table's join isn't promoted to LOUTER. This could
@@ -1436,6 +1447,7 @@ class AggregationTests(TestCase):
query.filter(q1 | q2)
self.assertEqual(len(q2.children), 1)
+ @ignore_warnings(category=RemovedInDjango31Warning)
def test_fobj_group_by(self):
"""
An F() object referring to related column works correctly in group by.
@@ -1513,6 +1525,7 @@ class JoinPromotionTests(TestCase):
qs = Charlie.objects.annotate(Count('alfa__name'))
self.assertIn(' LEFT OUTER JOIN ', str(qs.query))
+ @ignore_warnings(category=RemovedInDjango31Warning)
def test_non_nullable_fk_not_promoted(self):
qs = Book.objects.annotate(Count('contact__name'))
self.assertIn(' INNER JOIN ', str(qs.query))
diff --git a/tests/ordering/tests.py b/tests/ordering/tests.py
index 8c07a27428..16e5cc9b2d 100644
--- a/tests/ordering/tests.py
+++ b/tests/ordering/tests.py
@@ -1,9 +1,10 @@
from datetime import datetime
from operator import attrgetter
-from django.db.models import DateTimeField, F, Max, OuterRef, Subquery
+from django.db.models import Count, DateTimeField, F, Max, OuterRef, Subquery
from django.db.models.functions import Upper
from django.test import TestCase
+from django.utils.deprecation import RemovedInDjango31Warning
from .models import Article, Author, OrderedByFArticle, Reference
@@ -403,3 +404,11 @@ class OrderingTests(TestCase):
articles, ['Article 1', 'Article 4', 'Article 3', 'Article 2'],
attrgetter('headline')
)
+
+ def test_deprecated_values_annotate(self):
+ msg = (
+ "Article QuerySet won't use Meta.ordering in Django 3.1. Add "
+ ".order_by('-pub_date', 'headline') to retain the current query."
+ )
+ with self.assertRaisesMessage(RemovedInDjango31Warning, msg):
+ list(Article.objects.values('author').annotate(Count('headline')))
diff --git a/tests/queries/test_explain.py b/tests/queries/test_explain.py
index ad4ca988ee..9428bd88e9 100644
--- a/tests/queries/test_explain.py
+++ b/tests/queries/test_explain.py
@@ -2,8 +2,11 @@ import unittest
from django.db import NotSupportedError, connection, transaction
from django.db.models import Count
-from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
+from django.test import (
+ TestCase, ignore_warnings, skipIfDBFeature, skipUnlessDBFeature,
+)
from django.test.utils import CaptureQueriesContext
+from django.utils.deprecation import RemovedInDjango31Warning
from .models import Tag
@@ -11,6 +14,7 @@ from .models import Tag
@skipUnlessDBFeature('supports_explaining_query_execution')
class ExplainTests(TestCase):
+ @ignore_warnings(category=RemovedInDjango31Warning)
def test_basic(self):
querysets = [
Tag.objects.filter(name='test'),
diff --git a/tests/queries/tests.py b/tests/queries/tests.py
index 384bda4c77..65917f84fb 100644
--- a/tests/queries/tests.py
+++ b/tests/queries/tests.py
@@ -1156,7 +1156,7 @@ class Queries1Tests(TestCase):
def test_ticket_20250(self):
# A negated Q along with an annotated queryset failed in Django 1.4
qs = Author.objects.annotate(Count('item'))
- qs = qs.filter(~Q(extra__value=0))
+ qs = qs.filter(~Q(extra__value=0)).order_by('name')
self.assertIn('SELECT', str(qs.query))
self.assertQuerysetEqual(