Skip to content

Commit 20bc52b

Browse files
committed
aggregation, aggregation_regress edits
1 parent 49f13f0 commit 20bc52b

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

tests/aggregation/tests.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,11 +1420,10 @@ def test_aggregation_subquery_annotation(self):
14201420
publisher_qs = Publisher.objects.annotate(
14211421
latest_book_pubdate=Subquery(latest_book_pubdate_qs),
14221422
).annotate(count=Count("book"))
1423-
with self.assertNumQueries(1) as ctx:
1424-
list(publisher_qs)
1425-
self.assertEqual(ctx[0]["sql"].count("SELECT"), 2)
1423+
list(publisher_qs)
1424+
# self.assertEqual(ctx[0]["sql"].count("SELECT"), 2)
14261425
# The GROUP BY should not be by alias either.
1427-
self.assertEqual(ctx[0]["sql"].lower().count("latest_book_pubdate"), 1)
1426+
# self.assertEqual(ctx[0]["sql"].lower().count("latest_book_pubdate"), 1)
14281427

14291428
def test_aggregation_subquery_annotation_exists(self):
14301429
latest_book_pubdate_qs = (
@@ -1659,10 +1658,10 @@ def test_aggregation_subquery_annotation_related_field(self):
16591658
)
16601659
.annotate(count=Count("authors"))
16611660
)
1662-
with self.assertNumQueries(1) as ctx:
1661+
with self.assertNumQueries(1):
16631662
self.assertSequenceEqual(books_qs, [book])
1664-
if connection.features.allows_group_by_select_index:
1665-
self.assertEqual(ctx[0]["sql"].count("SELECT"), 3)
1663+
# if connection.features.allows_group_by_select_index:
1664+
# self.assertEqual(ctx[0]["sql"].count("SELECT"), 3)
16661665

16671666
@skipUnlessDBFeature("supports_subqueries_in_group_by")
16681667
def test_aggregation_nested_subquery_outerref(self):
@@ -2246,7 +2245,7 @@ def test_referenced_subquery_requires_wrapping(self):
22462245
.filter(author=OuterRef("pk"))
22472246
.annotate(total=Count("book"))
22482247
)
2249-
with self.assertNumQueries(1) as ctx:
2248+
with self.assertNumQueries(1):
22502249
aggregate = (
22512250
Author.objects.annotate(
22522251
total_books=Subquery(total_books_qs.values("total"))
@@ -2256,8 +2255,8 @@ def test_referenced_subquery_requires_wrapping(self):
22562255
sum_total_books=Sum("total_books"),
22572256
)
22582257
)
2259-
sql = ctx.captured_queries[0]["sql"].lower()
2260-
self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
2258+
# sql = ctx.captured_queries[0]["sql"].lower()
2259+
# self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
22612260
self.assertEqual(aggregate, {"sum_total_books": 3})
22622261

22632262
def test_referenced_composed_subquery_requires_wrapping(self):
@@ -2266,7 +2265,7 @@ def test_referenced_composed_subquery_requires_wrapping(self):
22662265
.filter(author=OuterRef("pk"))
22672266
.annotate(total=Count("book"))
22682267
)
2269-
with self.assertNumQueries(1) as ctx:
2268+
with self.assertNumQueries(1):
22702269
aggregate = (
22712270
Author.objects.annotate(
22722271
total_books=Subquery(total_books_qs.values("total")),
@@ -2277,8 +2276,8 @@ def test_referenced_composed_subquery_requires_wrapping(self):
22772276
sum_total_books=Sum("total_books_ref"),
22782277
)
22792278
)
2280-
sql = ctx.captured_queries[0]["sql"].lower()
2281-
self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
2279+
# sql = ctx.captured_queries[0]["sql"].lower()
2280+
# self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
22822281
self.assertEqual(aggregate, {"sum_total_books": 3})
22832282

22842283
@skipUnlessDBFeature("supports_over_clause")

tests/aggregation_regress/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Publisher(models.Model):
1919
class ItemTag(models.Model):
2020
tag = models.CharField(max_length=100)
2121
content_type = models.ForeignKey(ContentType, models.CASCADE)
22-
object_id = models.PositiveIntegerField()
22+
object_id = models.CharField(max_length=24)
2323
content_object = GenericForeignKey("content_type", "object_id")
2424

2525

tests/aggregation_regress/tests.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from django.contrib.contenttypes.models import ContentType
88
from django.core.exceptions import FieldError
9-
from django.db import connection
109
from django.db.models import (
1110
Aggregate,
1211
Avg,
@@ -184,7 +183,7 @@ def test_annotation_with_value(self):
184183
)
185184
.annotate(sum_discount=Sum("discount_price"))
186185
)
187-
with self.assertNumQueries(1) as ctx:
186+
with self.assertNumQueries(1):
188187
self.assertSequenceEqual(
189188
values,
190189
[
@@ -194,8 +193,8 @@ def test_annotation_with_value(self):
194193
}
195194
],
196195
)
197-
if connection.features.allows_group_by_select_index:
198-
self.assertIn("GROUP BY 1", ctx[0]["sql"])
196+
# if connection.features.allows_group_by_select_index:
197+
# self.assertIn("GROUP BY 1", ctx[0]["sql"])
199198

200199
def test_aggregates_in_where_clause(self):
201200
"""
@@ -829,7 +828,7 @@ def test_empty(self):
829828
],
830829
)
831830

832-
def test_more_more(self):
831+
def test_more_more1(self):
833832
# Regression for #10113 - Fields mentioned in order_by() must be
834833
# included in the GROUP BY. This only becomes a problem when the
835834
# order_by introduces a new join.
@@ -849,6 +848,7 @@ def test_more_more(self):
849848
lambda b: b.name,
850849
)
851850

851+
def test_more_more2(self):
852852
# Regression for #10127 - Empty select_related() works with annotate
853853
qs = (
854854
Book.objects.filter(rating__lt=4.5)
@@ -877,6 +877,7 @@ def test_more_more(self):
877877
lambda b: (b.name, b.authors__age__avg, b.publisher.name, b.contact.name),
878878
)
879879

880+
def test_more_more3(self):
880881
# Regression for #10132 - If the values() clause only mentioned extra
881882
# (select=) columns, those columns are used for grouping
882883
qs = (
@@ -911,6 +912,7 @@ def test_more_more(self):
911912
],
912913
)
913914

915+
def test_more_more4(self):
914916
# Regression for #10182 - Queries with aggregate calls are correctly
915917
# realiased when used in a subquery
916918
ids = (
@@ -927,6 +929,7 @@ def test_more_more(self):
927929
lambda b: b.name,
928930
)
929931

932+
def test_more_more5(self):
930933
# Regression for #15709 - Ensure each group_by field only exists once
931934
# per query
932935
qstr = str(
@@ -1023,7 +1026,7 @@ def test_pickle(self):
10231026
query,
10241027
)
10251028

1026-
def test_more_more_more(self):
1029+
def test_more_more_more1(self):
10271030
# Regression for #10199 - Aggregate calls clone the original query so
10281031
# the original query can still be used
10291032
books = Book.objects.all()
@@ -1042,6 +1045,7 @@ def test_more_more_more(self):
10421045
lambda b: b.name,
10431046
)
10441047

1048+
def test_more_more_more2(self):
10451049
# Regression for #10248 - Annotations work with dates()
10461050
qs = (
10471051
Book.objects.annotate(num_authors=Count("authors"))
@@ -1056,6 +1060,7 @@ def test_more_more_more(self):
10561060
],
10571061
)
10581062

1063+
def test_more_more_more3(self):
10591064
# Regression for #10290 - extra selects with parameters can be used for
10601065
# grouping.
10611066
qs = (
@@ -1068,6 +1073,7 @@ def test_more_more_more(self):
10681073
qs, [150, 175, 224, 264, 473, 566], lambda b: int(b["sheets"])
10691074
)
10701075

1076+
def test_more_more_more4(self):
10711077
# Regression for 10425 - annotations don't get in the way of a count()
10721078
# clause
10731079
self.assertEqual(
@@ -1077,6 +1083,7 @@ def test_more_more_more(self):
10771083
Book.objects.annotate(Count("publisher")).values("publisher").count(), 6
10781084
)
10791085

1086+
def test_more_more_more5(self):
10801087
# Note: intentionally no order_by(), that case needs tests, too.
10811088
publishers = Publisher.objects.filter(id__in=[self.p1.id, self.p2.id])
10821089
self.assertEqual(sorted(p.name for p in publishers), ["Apress", "Sams"])
@@ -1100,6 +1107,7 @@ def test_more_more_more(self):
11001107
)
11011108
self.assertEqual(sorted(p.name for p in publishers), ["Apress", "Sams"])
11021109

1110+
def test_more_more_more6(self):
11031111
# Regression for 10666 - inherited fields work with annotations and
11041112
# aggregations
11051113
self.assertEqual(
@@ -1152,6 +1160,7 @@ def test_more_more_more(self):
11521160
],
11531161
)
11541162

1163+
def test_more_more_more7(self):
11551164
# Regression for #10766 - Shouldn't be able to reference an aggregate
11561165
# fields in an aggregate() call.
11571166
msg = "Cannot compute Avg('mean_age'): 'mean_age' is an aggregate"

tests/queries/tests.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,12 +3312,12 @@ def test_exclude_nullable_fields(self):
33123312
)
33133313

33143314
def test_exclude_multivalued_exists(self):
3315-
with CaptureQueriesContext(connection) as captured_queries:
3316-
self.assertSequenceEqual(
3317-
Job.objects.exclude(responsibilities__description="Programming"),
3318-
[self.j1],
3319-
)
3320-
self.assertIn("exists", captured_queries[0]["sql"].lower())
3315+
# with CaptureQueriesContext(connection) as captured_queries:
3316+
self.assertSequenceEqual(
3317+
Job.objects.exclude(responsibilities__description="Programming"),
3318+
[self.j1],
3319+
)
3320+
# self.assertIn("exists", captured_queries[0]["sql"].lower())
33213321

33223322
def test_exclude_subquery(self):
33233323
subquery = JobResponsibilities.objects.filter(

0 commit comments

Comments
 (0)