Debian Codegraph @

schema/index_test.go

raw
1 package schema_test
2
3 import (
4 "sync"
5 "testing"
6
7 "gorm.io/gorm/schema"
8 "gorm.io/gorm/utils/tests"
9 )
10
11 type UserIndex struct {
12 Name string `gorm:"index"`
13 Name2 string `gorm:"index:idx_name,unique"`
14 Name3 string `gorm:"index:,sort:desc,collate:utf8,type:btree,length:10,where:name3 != 'jinzhu'"`
15 Name4 string `gorm:"uniqueIndex"`
16 Name5 int64 `gorm:"index:,class:FULLTEXT,comment:hello \\, world,where:age > 10"`
17 Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"`
18 Age int64 `gorm:"index:profile,expression:ABS(age),option:WITH PARSER parser_name"`
19 OID int64 `gorm:"index:idx_id;index:idx_oid,unique"`
20 MemberNumber string `gorm:"index:idx_id,priority:1"`
21 Name7 string `gorm:"index:type"`
22 Name8 string `gorm:"index:,length:10;index:,collate:utf8"`
23
24 CompName1 string `gorm:"index:,unique,composite:idx_compname_1,option:NULLS NOT DISTINCT;not null"`
25 CompName2 string `gorm:"index:,composite:idx_compname_1"`
26
27 // Composite Index: Flattened structure.
28 Data0A string `gorm:"index:,composite:comp_id0"`
29 Data0B string `gorm:"index:,composite:comp_id0"`
30
31 // Composite Index: Nested structure.
32 Data1A string `gorm:"index:,composite:comp_id1"`
33 CompIdxLevel1C
34
35 // Composite Index: Unique and priority.
36 Data2A string `gorm:"index:,unique,composite:comp_id2,priority:2"`
37 CompIdxLevel2C
38 }
39
40 type CompIdxLevel1C struct {
41 CompIdxLevel1B
42 Data1C string `gorm:"index:,composite:comp_id1"`
43 }
44
45 type CompIdxLevel1B struct {
46 Data1B string `gorm:"index:,composite:comp_id1"`
47 }
48
49 type CompIdxLevel2C struct {
50 CompIdxLevel2B
51 Data2C string `gorm:"index:,unique,composite:comp_id2,priority:1"`
52 }
53
54 type CompIdxLevel2B struct {
55 Data2B string `gorm:"index:,unique,composite:comp_id2,priority:3"`
56 }
57
58 func TestParseIndex(t *testing.T) {
59 user, err := schema.Parse(&UserIndex{}, &sync.Map{}, schema.NamingStrategy{})
60 if err != nil {
61 t.Fatalf("failed to parse user index, got error %v", err)
62 }
63
64 results := []*schema.Index{
65 {
66 Name: "idx_user_indices_name",
67 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name"}}},
68 },
69 {
70 Name: "idx_name",
71 Class: "UNIQUE",
72 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2", UniqueIndex: "idx_name"}}},
73 },
74 {
75 Name: "idx_user_indices_name3",
76 Type: "btree",
77 Where: "name3 != 'jinzhu'",
78 Fields: []schema.IndexOption{{
79 Field: &schema.Field{Name: "Name3"},
80 Sort: "desc",
81 Collate: "utf8",
82 Length: 10,
83 }},
84 },
85 {
86 Name: "idx_user_indices_name4",
87 Class: "UNIQUE",
88 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4", UniqueIndex: "idx_user_indices_name4"}}},
89 },
90 {
91 Name: "idx_user_indices_name5",
92 Class: "FULLTEXT",
93 Comment: "hello , world",
94 Where: "age > 10",
95 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name5"}}},
96 },
97 {
98 Name: "profile",
99 Comment: "hello , world",
100 Where: "age > 10",
101 Option: "WITH PARSER parser_name",
102 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name6"}}, {
103 Field: &schema.Field{Name: "Age"},
104 Expression: "ABS(age)",
105 }},
106 },
107 {
108 Name: "idx_id",
109 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID", UniqueIndex: "idx_oid"}}},
110 },
111 {
112 Name: "idx_oid",
113 Class: "UNIQUE",
114 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID", UniqueIndex: "idx_oid"}}},
115 },
116 {
117 Name: "type",
118 Type: "",
119 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name7"}}},
120 },
121 {
122 Name: "idx_user_indices_name8",
123 Type: "",
124 Fields: []schema.IndexOption{
125 {Field: &schema.Field{Name: "Name8"}, Length: 10},
126 // Note: Duplicate Columns
127 {Field: &schema.Field{Name: "Name8"}, Collate: "utf8"},
128 },
129 },
130 {
131 Class: "UNIQUE",
132 Name: "idx_user_indices_idx_compname_1",
133 Option: "NULLS NOT DISTINCT",
134 Fields: []schema.IndexOption{
135 {Field: &schema.Field{Name: "CompName1", NotNull: true}},
136 {Field: &schema.Field{Name: "CompName2"}},
137 },
138 },
139 {
140 Name: "idx_user_indices_comp_id0",
141 Type: "",
142 Fields: []schema.IndexOption{{
143 Field: &schema.Field{Name: "Data0A"},
144 }, {
145 Field: &schema.Field{Name: "Data0B"},
146 }},
147 },
148 {
149 Name: "idx_user_indices_comp_id1",
150 Fields: []schema.IndexOption{{
151 Field: &schema.Field{Name: "Data1A"},
152 }, {
153 Field: &schema.Field{Name: "Data1B"},
154 }, {
155 Field: &schema.Field{Name: "Data1C"},
156 }},
157 },
158 {
159 Name: "idx_user_indices_comp_id2",
160 Class: "UNIQUE",
161 Fields: []schema.IndexOption{{
162 Field: &schema.Field{Name: "Data2C"},
163 }, {
164 Field: &schema.Field{Name: "Data2A"},
165 }, {
166 Field: &schema.Field{Name: "Data2B"},
167 }},
168 },
169 }
170
171 CheckIndices(t, results, user.ParseIndexes())
172 }
173
174 func TestParseIndexWithUniqueIndexAndUnique(t *testing.T) {
175 type IndexTest struct {
176 FieldA string `gorm:"unique;index"` // unique and index
177 FieldB string `gorm:"unique"` // unique
178
179 FieldC string `gorm:"index:,unique"` // uniqueIndex
180 FieldD string `gorm:"uniqueIndex;index"` // uniqueIndex and index
181
182 FieldE1 string `gorm:"uniqueIndex:uniq_field_e1_e2"` // mul uniqueIndex
183 FieldE2 string `gorm:"uniqueIndex:uniq_field_e1_e2"`
184
185 FieldF1 string `gorm:"uniqueIndex:uniq_field_f1_f2;index"` // mul uniqueIndex and index
186 FieldF2 string `gorm:"uniqueIndex:uniq_field_f1_f2;"`
187
188 FieldG string `gorm:"unique;uniqueIndex"` // unique and uniqueIndex
189
190 FieldH1 string `gorm:"unique;uniqueIndex:uniq_field_h1_h2"` // unique and mul uniqueIndex
191 FieldH2 string `gorm:"uniqueIndex:uniq_field_h1_h2"` // unique and mul uniqueIndex
192 }
193 indexSchema, err := schema.Parse(&IndexTest{}, &sync.Map{}, schema.NamingStrategy{})
194 if err != nil {
195 t.Fatalf("failed to parse user index, got error %v", err)
196 }
197 indices := indexSchema.ParseIndexes()
198 expectedIndices := []*schema.Index{
199 {
200 Name: "idx_index_tests_field_a",
201 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "FieldA", Unique: true}}},
202 },
203 {
204 Name: "idx_index_tests_field_c",
205 Class: "UNIQUE",
206 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "FieldC", UniqueIndex: "idx_index_tests_field_c"}}},
207 },
208 {
209 Name: "idx_index_tests_field_d",
210 Class: "UNIQUE",
211 Fields: []schema.IndexOption{
212 {Field: &schema.Field{Name: "FieldD"}},
213 // Note: Duplicate Columns
214 {Field: &schema.Field{Name: "FieldD"}},
215 },
216 },
217 {
218 Name: "uniq_field_e1_e2",
219 Class: "UNIQUE",
220 Fields: []schema.IndexOption{
221 {Field: &schema.Field{Name: "FieldE1"}},
222 {Field: &schema.Field{Name: "FieldE2"}},
223 },
224 },
225 {
226 Name: "uniq_field_f1_f2",
227 Class: "UNIQUE",
228 Fields: []schema.IndexOption{
229 {Field: &schema.Field{Name: "FieldF1"}},
230 {Field: &schema.Field{Name: "FieldF2"}},
231 },
232 },
233 {
234 Name: "idx_index_tests_field_f1",
235 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "FieldF1"}}},
236 },
237 {
238 Name: "idx_index_tests_field_g",
239 Class: "UNIQUE",
240 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "FieldG", Unique: true, UniqueIndex: "idx_index_tests_field_g"}}},
241 },
242 {
243 Name: "uniq_field_h1_h2",
244 Class: "UNIQUE",
245 Fields: []schema.IndexOption{
246 {Field: &schema.Field{Name: "FieldH1", Unique: true}},
247 {Field: &schema.Field{Name: "FieldH2"}},
248 },
249 },
250 }
251 CheckIndices(t, expectedIndices, indices)
252 }
253
254 func CheckIndices(t *testing.T, expected, actual []*schema.Index) {
255 if len(expected) != len(actual) {
256 t.Errorf("expected %d indices, but got %d", len(expected), len(actual))
257 return
258 }
259
260 for i, ei := range expected {
261 t.Run(ei.Name, func(t *testing.T) {
262 ai := actual[i]
263 tests.AssertObjEqual(t, ai, ei, "Name", "Class", "Type", "Where", "Comment", "Option")
264
265 if len(ei.Fields) != len(ai.Fields) {
266 t.Errorf("expected index %q field length is %d but actual %d", ei.Name, len(ei.Fields), len(ai.Fields))
267 return
268 }
269 for i, ef := range ei.Fields {
270 af := ai.Fields[i]
271 tests.AssertObjEqual(t, af, ef, "Name", "Unique", "UniqueIndex", "Expression", "Sort", "Collate", "Length", "NotNull")
272 }
273 })
274 }
275 }