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