Debian Codegraph @

tests/test_combinators.py

raw
1 import pytest
2 from django.db.models import Value
3 from django.db.models.aggregates import Sum
4 from django.test import TestCase
5
6 from django_cte import CTE, with_cte
7
8 from .models import Order
9
10
11 class TestCTECombinators(TestCase):
12
13 def test_cte_union_query(self):
14 one = CTE(
15 Order.objects
16 .values("region_id")
17 .annotate(total=Sum("amount")),
18 name="one"
19 )
20 two = CTE(
21 Order.objects
22 .values("region_id")
23 .annotate(total=Sum("amount") * 2),
24 name="two"
25 )
26
27 earths = with_cte(
28 one,
29 select=one.join(
30 Order.objects.filter(region_id="earth"),
31 region=one.col.region_id
32 )
33 .annotate(region_total=one.col.total)
34 .values_list("amount", "region_id", "region_total")
35 )
36 mars = with_cte(
37 two,
38 select=two.join(
39 Order.objects.filter(region_id="mars"),
40 region=two.col.region_id
41 )
42 .annotate(region_total=two.col.total)
43 .values_list("amount", "region_id", "region_total")
44 )
45 combined = earths.union(mars, all=True)
46 print(combined.query)
47
48 self.assertEqual(sorted(combined), [
49 (30, 'earth', 126),
50 (31, 'earth', 126),
51 (32, 'earth', 126),
52 (33, 'earth', 126),
53 (40, 'mars', 246),
54 (41, 'mars', 246),
55 (42, 'mars', 246),
56 ])
57
58 # queries used in union should still work on their own
59 print(earths.query)
60 self.assertEqual(sorted(earths),[
61 (30, 'earth', 126),
62 (31, 'earth', 126),
63 (32, 'earth', 126),
64 (33, 'earth', 126),
65 ])
66 print(mars.query)
67 self.assertEqual(sorted(mars),[
68 (40, 'mars', 246),
69 (41, 'mars', 246),
70 (42, 'mars', 246),
71 ])
72
73 def test_cte_union_with_non_cte_query(self):
74 one = CTE(
75 Order.objects
76 .values("region_id")
77 .annotate(total=Sum("amount")),
78 )
79
80 earths = with_cte(
81 one,
82 select=one.join(
83 Order.objects.filter(region_id="earth"),
84 region=one.col.region_id
85 ).annotate(region_total=one.col.total)
86 )
87 plain_mars = (
88 Order.objects.filter(region_id="mars")
89 .annotate(region_total=Value(0))
90 )
91 # Note: this does not work in the opposite order. A CTE query
92 # must come first to invoke custom CTE combinator logic.
93 combined = earths.union(plain_mars, all=True) \
94 .values_list("amount", "region_id", "region_total")
95 print(combined.query)
96
97 self.assertEqual(sorted(combined), [
98 (30, 'earth', 126),
99 (31, 'earth', 126),
100 (32, 'earth', 126),
101 (33, 'earth', 126),
102 (40, 'mars', 0),
103 (41, 'mars', 0),
104 (42, 'mars', 0),
105 ])
106
107 def test_cte_union_with_duplicate_names(self):
108 cte_sun = CTE(
109 Order.objects
110 .filter(region__parent="sun")
111 .values("region_id")
112 .annotate(total=Sum("amount")),
113 )
114 cte_proxima = CTE(
115 Order.objects
116 .filter(region__parent="proxima centauri")
117 .values("region_id")
118 .annotate(total=2 * Sum("amount")),
119 )
120
121 orders_sun = with_cte(
122 cte_sun,
123 select=cte_sun.join(Order, region=cte_sun.col.region_id)
124 .annotate(region_total=cte_sun.col.total)
125 )
126 orders_proxima = with_cte(
127 cte_proxima,
128 select=cte_proxima.join(Order, region=cte_proxima.col.region_id)
129 .annotate(region_total=cte_proxima.col.total)
130 )
131
132 msg = "Found two or more CTEs named 'cte'"
133 with pytest.raises(ValueError, match=msg):
134 orders_sun.union(orders_proxima)
135
136 def test_cte_union_of_same_cte(self):
137 cte = CTE(
138 Order.objects
139 .filter(region__parent="sun")
140 .values("region_id")
141 .annotate(total=Sum("amount")),
142 )
143
144 orders_big = with_cte(
145 cte,
146 select=cte.join(Order, region=cte.col.region_id)
147 .annotate(region_total=3 * cte.col.total)
148 )
149 orders_small = with_cte(
150 cte,
151 select=cte.join(Order, region=cte.col.region_id)
152 .annotate(region_total=cte.col.total)
153 )
154
155 orders = orders_big.union(orders_small) \
156 .values_list("amount", "region_id", "region_total")
157 print(orders.query)
158
159 self.assertEqual(sorted(orders), [
160 (10, 'mercury', 33),
161 (10, 'mercury', 99),
162 (11, 'mercury', 33),
163 (11, 'mercury', 99),
164 (12, 'mercury', 33),
165 (12, 'mercury', 99),
166 (20, 'venus', 86),
167 (20, 'venus', 258),
168 (21, 'venus', 86),
169 (21, 'venus', 258),
170 (22, 'venus', 86),
171 (22, 'venus', 258),
172 (23, 'venus', 86),
173 (23, 'venus', 258),
174 (30, 'earth', 126),
175 (30, 'earth', 378),
176 (31, 'earth', 126),
177 (31, 'earth', 378),
178 (32, 'earth', 126),
179 (32, 'earth', 378),
180 (33, 'earth', 126),
181 (33, 'earth', 378),
182 (40, 'mars', 123),
183 (40, 'mars', 369),
184 (41, 'mars', 123),
185 (41, 'mars', 369),
186 (42, 'mars', 123),
187 (42, 'mars', 369)
188 ])
189
190 def test_cte_intersection(self):
191 cte_big = CTE(
192 Order.objects
193 .values("region_id")
194 .annotate(total=Sum("amount")),
195 name='big'
196 )
197 cte_small = CTE(
198 Order.objects
199 .values("region_id")
200 .annotate(total=Sum("amount")),
201 name='small'
202 )
203 orders_big = with_cte(
204 cte_big,
205 select=cte_big.join(Order, region=cte_big.col.region_id)
206 .annotate(region_total=cte_big.col.total)
207 .filter(region_total__gte=86)
208 )
209 orders_small = with_cte(
210 cte_small,
211 select=cte_small.join(Order, region=cte_small.col.region_id)
212 .annotate(region_total=cte_small.col.total)
213 .filter(region_total__lte=123)
214 )
215
216 orders = orders_small.intersection(orders_big) \
217 .values_list("amount", "region_id", "region_total")
218 print(orders.query)
219
220 self.assertEqual(sorted(orders), [
221 (20, 'venus', 86),
222 (21, 'venus', 86),
223 (22, 'venus', 86),
224 (23, 'venus', 86),
225 (40, 'mars', 123),
226 (41, 'mars', 123),
227 (42, 'mars', 123),
228 ])
229
230 def test_cte_difference(self):
231 cte_big = CTE(
232 Order.objects
233 .values("region_id")
234 .annotate(total=Sum("amount")),
235 name='big'
236 )
237 cte_small = CTE(
238 Order.objects
239 .values("region_id")
240 .annotate(total=Sum("amount")),
241 name='small'
242 )
243 orders_big = with_cte(
244 cte_big,
245 select=cte_big.join(Order, region=cte_big.col.region_id)
246 .annotate(region_total=cte_big.col.total)
247 .filter(region_total__gte=86)
248 )
249 orders_small = with_cte(
250 cte_small,
251 select=cte_small.join(Order, region=cte_small.col.region_id)
252 .annotate(region_total=cte_small.col.total)
253 .filter(region_total__lte=123)
254 )
255
256 orders = orders_small.difference(orders_big) \
257 .values_list("amount", "region_id", "region_total")
258 print(orders.query)
259
260 self.assertEqual(sorted(orders), [
261 (1, 'moon', 6),
262 (2, 'moon', 6),
263 (3, 'moon', 6),
264 (10, 'mercury', 33),
265 (10, 'proxima centauri b', 33),
266 (11, 'mercury', 33),
267 (11, 'proxima centauri b', 33),
268 (12, 'mercury', 33),
269 (12, 'proxima centauri b', 33),
270 ])