Skip to content

Commit a3cb6e0

Browse files
Format test file with black
Co-Authored-By: Nishant Singh <saysnishant@gmail.com>
1 parent f477263 commit a3cb6e0

File tree

1 file changed

+90
-69
lines changed

1 file changed

+90
-69
lines changed

testapp/tests/test_aggregate_wrapper_for_postgres.py

Lines changed: 90 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,174 +18,195 @@ def setUp(self) -> None:
1818
self.category2 = baker.make(StoreProductCategory, store=self.store)
1919
self.product_1 = baker.make(StoreProduct, store=self.store, selling_price=50.22)
2020
self.product_2 = baker.make(
21-
StoreProduct, store=self.store, category=self.category1, selling_price=100.33
21+
StoreProduct,
22+
store=self.store,
23+
category=self.category1,
24+
selling_price=100.33,
2225
)
2326
self.product_3 = baker.make(
2427
StoreProduct, store=self.store, category=self.category1, selling_price=75.50
2528
)
2629
self.product_4 = baker.make(
27-
StoreProduct, store=self.store, category=self.category2, selling_price=120.75
30+
StoreProduct,
31+
store=self.store,
32+
category=self.category2,
33+
selling_price=120.75,
2834
)
2935

3036
def test_simple_aggregate(self):
3137
"""Test simple aggregate with Sum"""
3238
queryset = StoreProduct.objects.filter()
33-
aggregate_expressions = {'total_price': Sum("selling_price")}
39+
aggregate_expressions = {"total_price": Sum("selling_price")}
3440
expected_result = queryset.aggregate(**aggregate_expressions)
35-
41+
3642
with self.assertNumQueries(1):
3743
results = QuerysetsSingleQueryFetch(
38-
querysets=[QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions)]
44+
querysets=[
45+
QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions)
46+
]
3947
).execute()
40-
48+
4149
self.assertEqual(len(results), 1)
4250
aggregate_result = results[0]
43-
51+
4452
self.assertEqual(len(aggregate_result), len(expected_result))
45-
self.assertIn('total_price', aggregate_result)
53+
self.assertIn("total_price", aggregate_result)
4654
self.assertAlmostEqual(
47-
float(aggregate_result['total_price']),
48-
float(expected_result['total_price']),
49-
places=2
55+
float(aggregate_result["total_price"]),
56+
float(expected_result["total_price"]),
57+
places=2,
5058
)
5159

5260
def test_multiple_aggregates(self):
5361
"""Test multiple aggregates in a single query"""
5462
queryset = StoreProduct.objects.filter()
5563
aggregate_expressions = {
56-
'total_price': Sum("selling_price"),
57-
'count': Count("id"),
58-
'avg_price': Avg("selling_price"),
59-
'max_price': Max("selling_price"),
60-
'min_price': Min("selling_price"),
64+
"total_price": Sum("selling_price"),
65+
"count": Count("id"),
66+
"avg_price": Avg("selling_price"),
67+
"max_price": Max("selling_price"),
68+
"min_price": Min("selling_price"),
6169
}
6270
expected_result = queryset.aggregate(**aggregate_expressions)
63-
71+
6472
with self.assertNumQueries(1):
6573
results = QuerysetsSingleQueryFetch(
66-
querysets=[QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions)]
74+
querysets=[
75+
QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions)
76+
]
6777
).execute()
68-
78+
6979
self.assertEqual(len(results), 1)
7080
aggregate_result = results[0]
71-
81+
7282
self.assertEqual(len(aggregate_result), len(expected_result))
73-
83+
7484
for key in expected_result.keys():
7585
self.assertIn(key, aggregate_result)
76-
if isinstance(expected_result[key], Decimal) or isinstance(aggregate_result[key], (int, float, Decimal)):
86+
if isinstance(expected_result[key], Decimal) or isinstance(
87+
aggregate_result[key], (int, float, Decimal)
88+
):
7789
self.assertAlmostEqual(
78-
float(aggregate_result[key]),
79-
float(expected_result[key]),
80-
places=2
90+
float(aggregate_result[key]), float(expected_result[key]), places=2
8191
)
8292
else:
8393
self.assertEqual(aggregate_result[key], expected_result[key])
84-
85-
self.assertEqual(aggregate_result['count'], 4)
94+
95+
self.assertEqual(aggregate_result["count"], 4)
8696
self.assertAlmostEqual(
87-
float(aggregate_result['total_price']),
88-
float(Decimal('50.22') + Decimal('100.33') + Decimal('75.50') + Decimal('120.75')),
89-
places=2
97+
float(aggregate_result["total_price"]),
98+
float(
99+
Decimal("50.22")
100+
+ Decimal("100.33")
101+
+ Decimal("75.50")
102+
+ Decimal("120.75")
103+
),
104+
places=2,
90105
)
91106

92107
def test_filtered_aggregate(self):
93108
"""Test aggregate with filter"""
94109
queryset = StoreProduct.objects.filter(category=self.category1)
95110
aggregate_expressions = {
96-
'total_price': Sum("selling_price"),
97-
'count': Count("id"),
111+
"total_price": Sum("selling_price"),
112+
"count": Count("id"),
98113
}
99114
expected_result = queryset.aggregate(**aggregate_expressions)
100-
115+
101116
with self.assertNumQueries(1):
102117
results = QuerysetsSingleQueryFetch(
103-
querysets=[QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions)]
118+
querysets=[
119+
QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions)
120+
]
104121
).execute()
105-
122+
106123
self.assertEqual(len(results), 1)
107124
aggregate_result = results[0]
108-
125+
109126
self.assertEqual(len(aggregate_result), len(expected_result))
110-
127+
111128
for key in expected_result.keys():
112129
self.assertIn(key, aggregate_result)
113-
if isinstance(expected_result[key], Decimal) or isinstance(aggregate_result[key], (int, float, Decimal)):
130+
if isinstance(expected_result[key], Decimal) or isinstance(
131+
aggregate_result[key], (int, float, Decimal)
132+
):
114133
self.assertAlmostEqual(
115-
float(aggregate_result[key]),
116-
float(expected_result[key]),
117-
places=2
134+
float(aggregate_result[key]), float(expected_result[key]), places=2
118135
)
119136
else:
120137
self.assertEqual(aggregate_result[key], expected_result[key])
121-
122-
self.assertEqual(aggregate_result['count'], 2) # Only products in category1
138+
139+
self.assertEqual(aggregate_result["count"], 2) # Only products in category1
123140
self.assertAlmostEqual(
124-
float(aggregate_result['total_price']),
125-
float(Decimal('100.33') + Decimal('75.50')),
126-
places=2
141+
float(aggregate_result["total_price"]),
142+
float(Decimal("100.33") + Decimal("75.50")),
143+
places=2,
127144
)
128145

129146
def test_empty_aggregate(self):
130147
"""Test aggregate on empty queryset"""
131148
queryset = StoreProduct.objects.filter(id=-1) # No matches
132149
aggregate_expressions = {
133-
'total_price': Sum("selling_price"),
134-
'count': Count("id"),
150+
"total_price": Sum("selling_price"),
151+
"count": Count("id"),
135152
}
136153
expected_result = queryset.aggregate(**aggregate_expressions)
137-
154+
138155
with self.assertNumQueries(1):
139156
results = QuerysetsSingleQueryFetch(
140-
querysets=[QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions)]
157+
querysets=[
158+
QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions)
159+
]
141160
).execute()
142-
161+
143162
self.assertEqual(len(results), 1)
144163
aggregate_result = results[0]
145-
164+
146165
self.assertEqual(len(aggregate_result), len(expected_result))
147-
166+
148167
for key in expected_result.keys():
149168
self.assertIn(key, aggregate_result)
150169
self.assertEqual(aggregate_result[key], expected_result[key])
151-
152-
self.assertEqual(aggregate_result['count'], 0)
153-
self.assertIsNone(aggregate_result['total_price'])
170+
171+
self.assertEqual(aggregate_result["count"], 0)
172+
self.assertIsNone(aggregate_result["total_price"])
154173

155174
def test_mix_with_other_querysets(self):
156175
"""Test mixture of aggregate wrapper and other querysets"""
157176
queryset = StoreProduct.objects.filter()
158177
aggregate_expressions = {
159-
'total_price': Sum("selling_price"),
160-
'count': Count("id"),
178+
"total_price": Sum("selling_price"),
179+
"count": Count("id"),
161180
}
162181
expected_result = queryset.aggregate(**aggregate_expressions)
163182
regular_queryset = StoreProductCategory.objects.filter()
164-
183+
165184
with self.assertNumQueries(1):
166185
results = QuerysetsSingleQueryFetch(
167186
querysets=[
168-
QuerysetAggregateWrapper(queryset=queryset, **aggregate_expressions),
169-
regular_queryset
187+
QuerysetAggregateWrapper(
188+
queryset=queryset, **aggregate_expressions
189+
),
190+
regular_queryset,
170191
]
171192
).execute()
172-
193+
173194
self.assertEqual(len(results), 2)
174195
aggregate_result = results[0]
175196
categories = results[1]
176-
197+
177198
self.assertEqual(len(aggregate_result), len(expected_result))
178-
199+
179200
for key in expected_result.keys():
180201
self.assertIn(key, aggregate_result)
181-
if isinstance(expected_result[key], Decimal) or isinstance(aggregate_result[key], (int, float, Decimal)):
202+
if isinstance(expected_result[key], Decimal) or isinstance(
203+
aggregate_result[key], (int, float, Decimal)
204+
):
182205
self.assertAlmostEqual(
183-
float(aggregate_result[key]),
184-
float(expected_result[key]),
185-
places=2
206+
float(aggregate_result[key]), float(expected_result[key]), places=2
186207
)
187208
else:
188209
self.assertEqual(aggregate_result[key], expected_result[key])
189-
210+
190211
regular_categories = list(regular_queryset)
191212
self.assertEqual(len(categories), len(regular_categories))

0 commit comments

Comments
 (0)