|
| 1 | +from decimal import Decimal |
| 2 | + |
| 3 | +from django.db.models import Sum, Count, Avg, Max, Min |
| 4 | +from django.test import TransactionTestCase |
| 5 | +from model_bakery import baker |
| 6 | + |
| 7 | +from django_querysets_single_query_fetch.service import ( |
| 8 | + QuerysetsSingleQueryFetch, |
| 9 | + QuerysetAggregateWrapper, |
| 10 | +) |
| 11 | +from testapp.models import OnlineStore, StoreProduct, StoreProductCategory |
| 12 | + |
| 13 | + |
| 14 | +class QuerysetAggregateWrapperPostgresTestCase(TransactionTestCase): |
| 15 | + def setUp(self) -> None: |
| 16 | + self.store = baker.make(OnlineStore) |
| 17 | + self.category1 = baker.make(StoreProductCategory, store=self.store) |
| 18 | + self.category2 = baker.make(StoreProductCategory, store=self.store) |
| 19 | + self.product_1 = baker.make(StoreProduct, store=self.store, selling_price=50.22) |
| 20 | + self.product_2 = baker.make( |
| 21 | + StoreProduct, store=self.store, category=self.category1, selling_price=100.33 |
| 22 | + ) |
| 23 | + self.product_3 = baker.make( |
| 24 | + StoreProduct, store=self.store, category=self.category1, selling_price=75.50 |
| 25 | + ) |
| 26 | + self.product_4 = baker.make( |
| 27 | + StoreProduct, store=self.store, category=self.category2, selling_price=120.75 |
| 28 | + ) |
| 29 | + |
| 30 | + def test_simple_aggregate(self): |
| 31 | + """Test simple aggregate with Sum""" |
| 32 | + queryset = StoreProduct.objects.filter() |
| 33 | + aggregate_queryset = queryset.aggregate(total_price=Sum("selling_price")) |
| 34 | + |
| 35 | + with self.assertNumQueries(1): |
| 36 | + results = QuerysetsSingleQueryFetch( |
| 37 | + querysets=[QuerysetAggregateWrapper(queryset=queryset, **aggregate_queryset)] |
| 38 | + ).execute() |
| 39 | + |
| 40 | + self.assertEqual(len(results), 1) |
| 41 | + aggregate_result = results[0] |
| 42 | + |
| 43 | + self.assertEqual(len(aggregate_result), len(aggregate_queryset)) |
| 44 | + self.assertIn('total_price', aggregate_result) |
| 45 | + self.assertAlmostEqual( |
| 46 | + float(aggregate_result['total_price']), |
| 47 | + float(aggregate_queryset['total_price']), |
| 48 | + places=2 |
| 49 | + ) |
| 50 | + |
| 51 | + def test_multiple_aggregates(self): |
| 52 | + """Test multiple aggregates in a single query""" |
| 53 | + queryset = StoreProduct.objects.filter() |
| 54 | + aggregate_queryset = queryset.aggregate( |
| 55 | + total_price=Sum("selling_price"), |
| 56 | + count=Count("id"), |
| 57 | + avg_price=Avg("selling_price"), |
| 58 | + max_price=Max("selling_price"), |
| 59 | + min_price=Min("selling_price"), |
| 60 | + ) |
| 61 | + |
| 62 | + with self.assertNumQueries(1): |
| 63 | + results = QuerysetsSingleQueryFetch( |
| 64 | + querysets=[QuerysetAggregateWrapper(queryset=queryset, **aggregate_queryset)] |
| 65 | + ).execute() |
| 66 | + |
| 67 | + self.assertEqual(len(results), 1) |
| 68 | + aggregate_result = results[0] |
| 69 | + |
| 70 | + self.assertEqual(len(aggregate_result), len(aggregate_queryset)) |
| 71 | + |
| 72 | + for key in aggregate_queryset.keys(): |
| 73 | + self.assertIn(key, aggregate_result) |
| 74 | + if isinstance(aggregate_queryset[key], Decimal) or isinstance(aggregate_result[key], (int, float, Decimal)): |
| 75 | + self.assertAlmostEqual( |
| 76 | + float(aggregate_result[key]), |
| 77 | + float(aggregate_queryset[key]), |
| 78 | + places=2 |
| 79 | + ) |
| 80 | + else: |
| 81 | + self.assertEqual(aggregate_result[key], aggregate_queryset[key]) |
| 82 | + |
| 83 | + self.assertEqual(aggregate_result['count'], 4) |
| 84 | + self.assertAlmostEqual( |
| 85 | + float(aggregate_result['total_price']), |
| 86 | + float(Decimal('50.22') + Decimal('100.33') + Decimal('75.50') + Decimal('120.75')), |
| 87 | + places=2 |
| 88 | + ) |
| 89 | + |
| 90 | + def test_filtered_aggregate(self): |
| 91 | + """Test aggregate with filter""" |
| 92 | + queryset = StoreProduct.objects.filter(category=self.category1) |
| 93 | + aggregate_queryset = queryset.aggregate( |
| 94 | + total_price=Sum("selling_price"), |
| 95 | + count=Count("id"), |
| 96 | + ) |
| 97 | + |
| 98 | + with self.assertNumQueries(1): |
| 99 | + results = QuerysetsSingleQueryFetch( |
| 100 | + querysets=[QuerysetAggregateWrapper(queryset=queryset, **aggregate_queryset)] |
| 101 | + ).execute() |
| 102 | + |
| 103 | + self.assertEqual(len(results), 1) |
| 104 | + aggregate_result = results[0] |
| 105 | + |
| 106 | + self.assertEqual(len(aggregate_result), len(aggregate_queryset)) |
| 107 | + |
| 108 | + for key in aggregate_queryset.keys(): |
| 109 | + self.assertIn(key, aggregate_result) |
| 110 | + if isinstance(aggregate_queryset[key], Decimal) or isinstance(aggregate_result[key], (int, float, Decimal)): |
| 111 | + self.assertAlmostEqual( |
| 112 | + float(aggregate_result[key]), |
| 113 | + float(aggregate_queryset[key]), |
| 114 | + places=2 |
| 115 | + ) |
| 116 | + else: |
| 117 | + self.assertEqual(aggregate_result[key], aggregate_queryset[key]) |
| 118 | + |
| 119 | + self.assertEqual(aggregate_result['count'], 2) # Only products in category1 |
| 120 | + self.assertAlmostEqual( |
| 121 | + float(aggregate_result['total_price']), |
| 122 | + float(Decimal('100.33') + Decimal('75.50')), |
| 123 | + places=2 |
| 124 | + ) |
| 125 | + |
| 126 | + def test_empty_aggregate(self): |
| 127 | + """Test aggregate on empty queryset""" |
| 128 | + queryset = StoreProduct.objects.filter(id=-1) # No matches |
| 129 | + aggregate_queryset = queryset.aggregate( |
| 130 | + total_price=Sum("selling_price"), |
| 131 | + count=Count("id"), |
| 132 | + ) |
| 133 | + |
| 134 | + with self.assertNumQueries(1): |
| 135 | + results = QuerysetsSingleQueryFetch( |
| 136 | + querysets=[QuerysetAggregateWrapper(queryset=queryset, **aggregate_queryset)] |
| 137 | + ).execute() |
| 138 | + |
| 139 | + self.assertEqual(len(results), 1) |
| 140 | + aggregate_result = results[0] |
| 141 | + |
| 142 | + self.assertEqual(len(aggregate_result), len(aggregate_queryset)) |
| 143 | + |
| 144 | + for key in aggregate_queryset.keys(): |
| 145 | + self.assertIn(key, aggregate_result) |
| 146 | + self.assertEqual(aggregate_result[key], aggregate_queryset[key]) |
| 147 | + |
| 148 | + self.assertEqual(aggregate_result['count'], 0) |
| 149 | + self.assertIsNone(aggregate_result['total_price']) |
| 150 | + |
| 151 | + def test_mix_with_other_querysets(self): |
| 152 | + """Test mixture of aggregate wrapper and other querysets""" |
| 153 | + aggregate_queryset = StoreProduct.objects.filter().aggregate( |
| 154 | + total_price=Sum("selling_price"), |
| 155 | + count=Count("id"), |
| 156 | + ) |
| 157 | + regular_queryset = StoreProductCategory.objects.filter() |
| 158 | + |
| 159 | + with self.assertNumQueries(1): |
| 160 | + results = QuerysetsSingleQueryFetch( |
| 161 | + querysets=[ |
| 162 | + QuerysetAggregateWrapper(queryset=StoreProduct.objects.filter(), **aggregate_queryset), |
| 163 | + regular_queryset |
| 164 | + ] |
| 165 | + ).execute() |
| 166 | + |
| 167 | + self.assertEqual(len(results), 2) |
| 168 | + aggregate_result = results[0] |
| 169 | + categories = results[1] |
| 170 | + |
| 171 | + self.assertEqual(len(aggregate_result), len(aggregate_queryset)) |
| 172 | + |
| 173 | + for key in aggregate_queryset.keys(): |
| 174 | + self.assertIn(key, aggregate_result) |
| 175 | + if isinstance(aggregate_queryset[key], Decimal) or isinstance(aggregate_result[key], (int, float, Decimal)): |
| 176 | + self.assertAlmostEqual( |
| 177 | + float(aggregate_result[key]), |
| 178 | + float(aggregate_queryset[key]), |
| 179 | + places=2 |
| 180 | + ) |
| 181 | + else: |
| 182 | + self.assertEqual(aggregate_result[key], aggregate_queryset[key]) |
| 183 | + |
| 184 | + regular_categories = list(regular_queryset) |
| 185 | + self.assertEqual(len(categories), len(regular_categories)) |
0 commit comments