This repository has been archived by the owner on May 31, 2022. It is now read-only.
forked from skeema/tengo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
233 lines (225 loc) · 5.77 KB
/
index_test.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package tengo
import (
"testing"
)
func TestIndexDefinition(t *testing.T) {
// Test definition using a combination of MySQL 8's new features
index := Index{
Name: "test_idx",
Parts: []IndexPart{
{ColumnName: "col_a", PrefixLength: 5, Descending: true},
{Expression: "(`col_b` * 2)"},
},
Unique: true,
Invisible: true,
Comment: "this is a comment",
Type: "BTREE",
}
expected := "UNIQUE KEY `test_idx` (`col_a`(5) DESC,((`col_b` * 2))) COMMENT 'this is a comment' /*!80000 INVISIBLE */"
actual := index.Definition(FlavorMySQL80)
if expected != actual {
t.Errorf("Index.Definition() expected %q, instead found %q", expected, actual)
}
// Test panic on illegal field values
index.PrimaryKey = true
index.Unique = false
var didPanic bool
defer func() {
if recover() != nil {
didPanic = true
}
}()
index.Definition(FlavorMySQL80)
if !didPanic {
t.Errorf("Expected Index.Definition() to panic on non-unique primary key, but it did not")
}
}
func TestIndexRedundantTo(t *testing.T) {
columns := []*Column{
{Name: "col0"},
{Name: "col1"},
{Name: "col2"},
{Name: "col3"},
{Name: "col4"},
}
indexes := []*Index{
{
Name: "0_first_three_pk",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[1].Name},
{ColumnName: columns[2].Name},
},
PrimaryKey: true,
Unique: true,
Type: "BTREE",
},
{
Name: "1_first_three_uniq",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[1].Name},
{ColumnName: columns[2].Name},
},
Unique: true,
Type: "BTREE",
},
{
Name: "2_first_three",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[1].Name},
{ColumnName: columns[2].Name},
},
Type: "BTREE",
},
{
Name: "3_first_three_subp",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[1].Name},
{ColumnName: columns[2].Name, PrefixLength: 20},
},
Type: "BTREE",
},
{
Name: "4_first_two_uniq_subp",
Parts: []IndexPart{
{ColumnName: columns[0].Name, PrefixLength: 5},
{ColumnName: columns[1].Name, PrefixLength: 10},
},
Unique: true,
Type: "BTREE",
},
{
Name: "5_first_two_subp",
Parts: []IndexPart{
{ColumnName: columns[0].Name, PrefixLength: 3},
{ColumnName: columns[1].Name, PrefixLength: 12},
},
Type: "BTREE",
},
{
Name: "6_mix_three",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[4].Name},
{ColumnName: columns[2].Name},
},
Type: "BTREE",
},
{
Name: "7_ft_first",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
},
Type: "FULLTEXT",
},
{
Name: "8_ft_first_two",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[1].Name},
},
Type: "FULLTEXT",
},
nil, // position 9
{
Name: "10_first_three_invis",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[1].Name},
{ColumnName: columns[2].Name},
},
Type: "BTREE",
Invisible: true,
},
{
Name: "11_first_three_desc",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[1].Name, Descending: true},
{ColumnName: columns[2].Name},
},
Type: "BTREE",
},
{
Name: "12_first_two_expr",
Parts: []IndexPart{
{ColumnName: columns[0].Name},
{ColumnName: columns[1].Name},
{Expression: "(`col2` + `col4`)"},
},
Type: "BTREE",
},
}
testCases := []struct {
receiver int
other int
expectRedundant bool
}{
{0, 1, false},
{1, 0, true},
{1, 1, true},
{2, 0, true},
{2, 1, true},
{3, 2, true},
{3, 1, true},
{2, 3, false},
{4, 3, false},
{4, 1, false}, // unique not redundant to larger index with same first cols, due to uniqueness constraint aspect
{4, 0, false}, // same as previous, but even when compared to the primary key
{1, 4, false},
{5, 4, false},
{5, 3, true},
{5, 6, false},
{6, 5, false},
{0, 7, false},
{7, 0, false},
{7, 8, false},
{8, 7, false},
{8, 8, true},
{9, 6, false},
{6, 9, false},
{2, 10, false}, // visible never redundant to invisible
{10, 2, true}, // invisible can be redundant to visible
{11, 0, false}, // desc col not redundant to asc col
{2, 11, false}, // asc col not redundant to desc col
{12, 0, false}, // expression not redundant to col
{5, 12, true}, // leftmost cols redundant to same leftmost cols even if expr after them
}
for _, tc := range testCases {
actualRedundant := indexes[tc.receiver].RedundantTo(indexes[tc.other])
if actualRedundant != tc.expectRedundant {
t.Errorf("Expected idx[%d].RedundantTo(idx[%d]) == %t, instead found %t", tc.receiver, tc.other, tc.expectRedundant, actualRedundant)
}
}
}
func TestIndexComparisonNil(t *testing.T) {
var idx1, idx2 *Index
idx2 = aTable(1).SecondaryIndexes[0]
if idx1.Equals(idx2) {
t.Error("Expected nil.Equals(non-nil) to return false, but it returned true")
}
if !idx1.Equals(idx1) {
t.Error("Expected nil.Equals(nil) to return true, but it returned false")
}
if idx1.EqualsIgnoringVisibility(idx2) {
t.Error("Expected nil.EqualsIgnoringVisibility(non-nil) to return false, but it returned true")
}
if !idx1.EqualsIgnoringVisibility(idx1) {
t.Error("Expected nil.EqualsIgnoringVisibility(nil) to return true, but it returned false")
}
if idx1.Equivalent(idx2) {
t.Error("Expected nil.Equivalent(non-nil) to return false, but it returned true")
}
if !idx1.Equivalent(idx1) {
t.Error("Expected nil.Equivalent(nil) to return true, but it returned false")
}
if idx1.RedundantTo(idx2) {
t.Error("Expected nil.Equivalent(non-nil) to return false, but it returned true")
}
if idx1.RedundantTo(idx1) {
t.Error("Expected nil.RedundantTo(nil) to return false, but it returned true")
}
}