@@ -61,17 +61,17 @@ func TestParseIndex(t *testing.T) {
61
61
t .Fatalf ("failed to parse user index, got error %v" , err )
62
62
}
63
63
64
- results := map [ string ] schema.Index {
65
- "idx_user_indices_name" : {
64
+ results := [] * schema.Index {
65
+ {
66
66
Name : "idx_user_indices_name" ,
67
67
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "Name" }}},
68
68
},
69
- "idx_name" : {
69
+ {
70
70
Name : "idx_name" ,
71
71
Class : "UNIQUE" ,
72
72
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "Name2" , UniqueIndex : "idx_name" }}},
73
73
},
74
- "idx_user_indices_name3" : {
74
+ {
75
75
Name : "idx_user_indices_name3" ,
76
76
Type : "btree" ,
77
77
Where : "name3 != 'jinzhu'" ,
@@ -82,19 +82,19 @@ func TestParseIndex(t *testing.T) {
82
82
Length : 10 ,
83
83
}},
84
84
},
85
- "idx_user_indices_name4" : {
85
+ {
86
86
Name : "idx_user_indices_name4" ,
87
87
Class : "UNIQUE" ,
88
88
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "Name4" , UniqueIndex : "idx_user_indices_name4" }}},
89
89
},
90
- "idx_user_indices_name5" : {
90
+ {
91
91
Name : "idx_user_indices_name5" ,
92
92
Class : "FULLTEXT" ,
93
93
Comment : "hello , world" ,
94
94
Where : "age > 10" ,
95
95
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "Name5" }}},
96
96
},
97
- "profile" : {
97
+ {
98
98
Name : "profile" ,
99
99
Comment : "hello , world" ,
100
100
Where : "age > 10" ,
@@ -104,21 +104,21 @@ func TestParseIndex(t *testing.T) {
104
104
Expression : "ABS(age)" ,
105
105
}},
106
106
},
107
- "idx_id" : {
107
+ {
108
108
Name : "idx_id" ,
109
109
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "MemberNumber" }}, {Field : & schema.Field {Name : "OID" , UniqueIndex : "idx_oid" }}},
110
110
},
111
- "idx_oid" : {
111
+ {
112
112
Name : "idx_oid" ,
113
113
Class : "UNIQUE" ,
114
114
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "OID" , UniqueIndex : "idx_oid" }}},
115
115
},
116
- "type" : {
116
+ {
117
117
Name : "type" ,
118
118
Type : "" ,
119
119
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "Name7" }}},
120
120
},
121
- "idx_user_indices_name8" : {
121
+ {
122
122
Name : "idx_user_indices_name8" ,
123
123
Type : "" ,
124
124
Fields : []schema.IndexOption {
@@ -127,7 +127,16 @@ func TestParseIndex(t *testing.T) {
127
127
{Field : & schema.Field {Name : "Name8" }, Collate : "utf8" },
128
128
},
129
129
},
130
- "idx_user_indices_comp_id0" : {
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
+ {
131
140
Name : "idx_user_indices_comp_id0" ,
132
141
Type : "" ,
133
142
Fields : []schema.IndexOption {{
@@ -136,7 +145,7 @@ func TestParseIndex(t *testing.T) {
136
145
Field : & schema.Field {Name : "Data0B" },
137
146
}},
138
147
},
139
- "idx_user_indices_comp_id1" : {
148
+ {
140
149
Name : "idx_user_indices_comp_id1" ,
141
150
Fields : []schema.IndexOption {{
142
151
Field : & schema.Field {Name : "Data1A" },
@@ -146,7 +155,7 @@ func TestParseIndex(t *testing.T) {
146
155
Field : & schema.Field {Name : "Data1C" },
147
156
}},
148
157
},
149
- "idx_user_indices_comp_id2" : {
158
+ {
150
159
Name : "idx_user_indices_comp_id2" ,
151
160
Class : "UNIQUE" ,
152
161
Fields : []schema.IndexOption {{
@@ -157,15 +166,6 @@ func TestParseIndex(t *testing.T) {
157
166
Field : & schema.Field {Name : "Data2B" },
158
167
}},
159
168
},
160
- "idx_user_indices_idx_compname_1" : {
161
- Class : "UNIQUE" ,
162
- Name : "idx_user_indices_idx_compname_1" ,
163
- Option : "NULLS NOT DISTINCT" ,
164
- Fields : []schema.IndexOption {
165
- {Field : & schema.Field {Name : "CompName1" , NotNull : true }},
166
- {Field : & schema.Field {Name : "CompName2" }},
167
- },
168
- },
169
169
}
170
170
171
171
CheckIndices (t , results , user .ParseIndexes ())
@@ -195,17 +195,17 @@ func TestParseIndexWithUniqueIndexAndUnique(t *testing.T) {
195
195
t .Fatalf ("failed to parse user index, got error %v" , err )
196
196
}
197
197
indices := indexSchema .ParseIndexes ()
198
- CheckIndices ( t , map [ string ] schema.Index {
199
- "idx_index_tests_field_a" : {
198
+ expectedIndices := [] * schema.Index {
199
+ {
200
200
Name : "idx_index_tests_field_a" ,
201
201
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "FieldA" , Unique : true }}},
202
202
},
203
- "idx_index_tests_field_c" : {
203
+ {
204
204
Name : "idx_index_tests_field_c" ,
205
205
Class : "UNIQUE" ,
206
206
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "FieldC" , UniqueIndex : "idx_index_tests_field_c" }}},
207
207
},
208
- "idx_index_tests_field_d" : {
208
+ {
209
209
Name : "idx_index_tests_field_d" ,
210
210
Class : "UNIQUE" ,
211
211
Fields : []schema.IndexOption {
@@ -214,63 +214,62 @@ func TestParseIndexWithUniqueIndexAndUnique(t *testing.T) {
214
214
{Field : & schema.Field {Name : "FieldD" }},
215
215
},
216
216
},
217
- "uniq_field_e1_e2" : {
217
+ {
218
218
Name : "uniq_field_e1_e2" ,
219
219
Class : "UNIQUE" ,
220
220
Fields : []schema.IndexOption {
221
221
{Field : & schema.Field {Name : "FieldE1" }},
222
222
{Field : & schema.Field {Name : "FieldE2" }},
223
223
},
224
224
},
225
- "idx_index_tests_field_f1" : {
226
- Name : "idx_index_tests_field_f1" ,
227
- Fields : []schema.IndexOption {{Field : & schema.Field {Name : "FieldF1" }}},
228
- },
229
- "uniq_field_f1_f2" : {
225
+ {
230
226
Name : "uniq_field_f1_f2" ,
231
227
Class : "UNIQUE" ,
232
228
Fields : []schema.IndexOption {
233
229
{Field : & schema.Field {Name : "FieldF1" }},
234
230
{Field : & schema.Field {Name : "FieldF2" }},
235
231
},
236
232
},
237
- "idx_index_tests_field_g" : {
233
+ {
234
+ Name : "idx_index_tests_field_f1" ,
235
+ Fields : []schema.IndexOption {{Field : & schema.Field {Name : "FieldF1" }}},
236
+ },
237
+ {
238
238
Name : "idx_index_tests_field_g" ,
239
239
Class : "UNIQUE" ,
240
240
Fields : []schema.IndexOption {{Field : & schema.Field {Name : "FieldG" , Unique : true , UniqueIndex : "idx_index_tests_field_g" }}},
241
241
},
242
- "uniq_field_h1_h2" : {
242
+ {
243
243
Name : "uniq_field_h1_h2" ,
244
244
Class : "UNIQUE" ,
245
245
Fields : []schema.IndexOption {
246
246
{Field : & schema.Field {Name : "FieldH1" , Unique : true }},
247
247
{Field : & schema.Field {Name : "FieldH2" }},
248
248
},
249
249
},
250
- }, indices )
250
+ }
251
+ CheckIndices (t , expectedIndices , indices )
251
252
}
252
253
253
- func CheckIndices (t * testing.T , expected , actual map [string ]schema.Index ) {
254
- for k , ei := range expected {
255
- t .Run (k , func (t * testing.T ) {
256
- ai , ok := actual [k ]
257
- if ! ok {
258
- t .Errorf ("expected index %q but actual missing" , k )
259
- return
260
- }
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 ]
261
263
tests .AssertObjEqual (t , ai , ei , "Name" , "Class" , "Type" , "Where" , "Comment" , "Option" )
264
+
262
265
if len (ei .Fields ) != len (ai .Fields ) {
263
- t .Errorf ("expected index %q field length is %d but actual %d" , k , 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 ))
264
267
return
265
268
}
266
269
for i , ef := range ei .Fields {
267
270
af := ai .Fields [i ]
268
271
tests .AssertObjEqual (t , af , ef , "Name" , "Unique" , "UniqueIndex" , "Expression" , "Sort" , "Collate" , "Length" , "NotNull" )
269
272
}
270
273
})
271
- delete (actual , k )
272
- }
273
- for k := range actual {
274
- t .Errorf ("unexpected index %q" , k )
275
274
}
276
275
}
0 commit comments