-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathtest_aggregates.py
More file actions
143 lines (114 loc) · 5.53 KB
/
Copy pathtest_aggregates.py
File metadata and controls
143 lines (114 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import pytest
from django.db.models import F
from django.test import TestCase
from django_mysql.models import BitAnd, BitOr, BitXor, GroupConcat
from django_mysql.test.utils import override_mysql_variables
from tests.testapp.models import Alphabet, Author
class BitAndTests(TestCase):
def test_implicit_name(self):
Alphabet.objects.bulk_create([Alphabet(a=29), Alphabet(a=15)])
out = Alphabet.objects.all().aggregate(BitAnd("a"))
assert out == {"a__bitand": 13}
def test_explicit_name(self):
Alphabet.objects.bulk_create([Alphabet(a=11), Alphabet(a=24)])
out = Alphabet.objects.all().aggregate(aaa=BitAnd("a"))
assert out == {"aaa": 8}
def test_no_rows(self):
out = Alphabet.objects.all().aggregate(BitAnd("a"))
# Manual:
# "This function returns 18446744073709551615 if there were no matching
# rows. (This is the value of an unsigned BIGINT value with all bits
# set to 1.)"
assert out == {"a__bitand": 18446744073709551615}
class BitOrTests(TestCase):
def test_implicit_name(self):
Alphabet.objects.bulk_create([Alphabet(a=1), Alphabet(a=84)])
out = Alphabet.objects.all().aggregate(BitOr("a"))
assert out == {"a__bitor": 85}
def test_explicit_name(self):
Alphabet.objects.bulk_create([Alphabet(a=29), Alphabet(a=15)])
out = Alphabet.objects.all().aggregate(aaa=BitOr("a"))
assert out == {"aaa": 31}
def test_no_rows(self):
out = Alphabet.objects.all().aggregate(BitOr("a"))
# Manual:
# "This function returns 0 if there were no matching rows."
assert out == {"a__bitor": 0}
class BitXorTests(TestCase):
def test_implicit_name(self):
Alphabet.objects.bulk_create([Alphabet(a=11), Alphabet(a=3)])
out = Alphabet.objects.all().aggregate(BitXor("a"))
assert out == {"a__bitxor": 8}
def test_explicit_name(self):
Alphabet.objects.bulk_create([Alphabet(a=123), Alphabet(a=456)])
out = Alphabet.objects.all().aggregate(aaa=BitXor("a"))
assert out == {"aaa": 435}
def test_no_rows(self):
out = Alphabet.objects.all().aggregate(BitXor("a"))
# Manual:
# "This function returns 0 if there were no matching rows."
assert out == {"a__bitxor": 0}
class GroupConcatTests(TestCase):
def setUp(self):
super().setUp()
self.shakes = Author.objects.create(name="William Shakespeare")
self.jk = Author.objects.create(name="JK Rowling", tutor=self.shakes)
self.grisham = Author.objects.create(name="Grisham", tutor=self.shakes)
self.str_tutee_ids = [str(self.jk.id), str(self.grisham.id)]
def test_basic_aggregate_ids(self):
out = self.shakes.tutees.aggregate(tids=GroupConcat("id"))
assert out == {"tids": self.str_tutee_ids}
def test_distinct_aggregate_ids(self):
out = self.shakes.tutees.aggregate(tids=GroupConcat("id", distinct=True))
assert out == {"tids": set(self.str_tutee_ids)}
def test_basic_annotate_ids(self):
concat = GroupConcat("tutees__id")
shakey = Author.objects.annotate(tids=concat).get(id=self.shakes.id)
concatted_ids = ",".join(self.str_tutee_ids)
assert shakey.tids, concatted_ids
def test_separator(self):
concat = GroupConcat("id", separator=":")
out = self.shakes.tutees.aggregate(tids=concat)
concatted_ids = ":".join(self.str_tutee_ids)
assert out == {"tids": concatted_ids}
def test_separator_empty(self):
concat = GroupConcat("id", separator="")
out = self.shakes.tutees.aggregate(tids=concat)
concatted_ids = "".join(self.str_tutee_ids)
assert out == {"tids": concatted_ids}
def test_separator_big(self):
concat = GroupConcat("id", separator="BIG")
out = self.shakes.tutees.aggregate(tids=concat)
concatted_ids = "BIG".join(self.str_tutee_ids)
assert out == {"tids": concatted_ids}
def test_expression(self):
concat = GroupConcat(F("id") + 1)
out = self.shakes.tutees.aggregate(tids=concat)
concatted_ids = [str(self.jk.id + 1), str(self.grisham.id + 1)]
assert out == {"tids": concatted_ids}
def test_application_order(self):
out = Author.objects.exclude(id=self.shakes.id).aggregate(
tids=GroupConcat("tutor_id", distinct=True)
)
assert out == {"tids": {str(self.shakes.id)}}
@override_mysql_variables(SQL_MODE="ANSI")
def test_separator_ansi_mode(self):
concat = GroupConcat("id", separator=">>")
out = self.shakes.tutees.aggregate(tids=concat)
concatted_ids = ">>".join(self.str_tutee_ids)
assert out == {"tids": concatted_ids}
def test_ordering_invalid(self):
with pytest.raises(ValueError) as excinfo:
self.shakes.tutees.aggregate(tids=GroupConcat("id", ordering="asceding"))
assert "'ordering' must be one of" in str(excinfo.value)
def test_ordering_asc(self):
out = self.shakes.tutees.aggregate(tids=GroupConcat("id", ordering="asc"))
assert out == {"tids": self.str_tutee_ids}
def test_ordering_desc(self):
out = self.shakes.tutees.aggregate(tids=GroupConcat("id", ordering="desc"))
assert out == {"tids": list(reversed(self.str_tutee_ids))}
def test_separator_ordering(self):
concat = GroupConcat("id", separator=":", ordering="asc")
out = self.shakes.tutees.aggregate(tids=concat)
concatted_ids = ":".join(self.str_tutee_ids)
assert out == {"tids": concatted_ids}