@@ -29,16 +29,16 @@ func TestDB_schemaFieldKey(t *testing.T) {
29
29
t .Run ("empty name or type" , func (t * testing.T ) {
30
30
_ , err := db .schemaFieldKey ("" , "" )
31
31
if err == nil {
32
- t .Errorf ("error not returned, but expected" )
32
+ t .Error ("error not returned, but expected" )
33
33
}
34
34
_ , err = db .schemaFieldKey ("" , "type" )
35
35
if err == nil {
36
- t .Errorf ("error not returned, but expected" )
36
+ t .Error ("error not returned, but expected" )
37
37
}
38
38
39
39
_ , err = db .schemaFieldKey ("test" , "" )
40
40
if err == nil {
41
- t .Errorf ("error not returned, but expected" )
41
+ t .Error ("error not returned, but expected" )
42
42
}
43
43
})
44
44
@@ -82,7 +82,7 @@ func TestDB_schemaFieldKey(t *testing.T) {
82
82
83
83
_ , err = db .schemaFieldKey ("the-field" , "another-type" )
84
84
if err == nil {
85
- t .Errorf ("error not returned, but expected" )
85
+ t .Error ("error not returned, but expected" )
86
86
}
87
87
})
88
88
}
@@ -124,3 +124,119 @@ func TestDB_schemaIndexPrefix(t *testing.T) {
124
124
}
125
125
})
126
126
}
127
+
128
+ // TestDB_RenameIndex checks if index name is correctly changed.
129
+ func TestDB_RenameIndex (t * testing.T ) {
130
+
131
+ t .Run ("empty names" , func (t * testing.T ) {
132
+ db , cleanupFunc := newTestDB (t )
133
+ defer cleanupFunc ()
134
+
135
+ // empty names
136
+ renamed , err := db .RenameIndex ("" , "" )
137
+ if err == nil {
138
+ t .Error ("error not returned, but expected" )
139
+ }
140
+ if renamed {
141
+ t .Fatal ("index should not be renamed" )
142
+ }
143
+
144
+ // empty index name
145
+ renamed , err = db .RenameIndex ("" , "new" )
146
+ if err == nil {
147
+ t .Error ("error not returned, but expected" )
148
+ }
149
+ if renamed {
150
+ t .Fatal ("index should not be renamed" )
151
+ }
152
+
153
+ // empty new index name
154
+ renamed , err = db .RenameIndex ("current" , "" )
155
+ if err == nil {
156
+ t .Error ("error not returned, but expected" )
157
+ }
158
+ if renamed {
159
+ t .Fatal ("index should not be renamed" )
160
+ }
161
+ })
162
+
163
+ t .Run ("same names" , func (t * testing.T ) {
164
+ db , cleanupFunc := newTestDB (t )
165
+ defer cleanupFunc ()
166
+
167
+ renamed , err := db .RenameIndex ("index1" , "index1" )
168
+ if err != nil {
169
+ t .Error (err )
170
+ }
171
+ if renamed {
172
+ t .Fatal ("index should not be renamed" )
173
+ }
174
+ })
175
+
176
+ t .Run ("unknown name" , func (t * testing.T ) {
177
+ db , cleanupFunc := newTestDB (t )
178
+ defer cleanupFunc ()
179
+
180
+ renamed , err := db .RenameIndex ("index1" , "index1new" )
181
+ if err != nil {
182
+ t .Error (err )
183
+ }
184
+ if renamed {
185
+ t .Fatal ("index should not be renamed" )
186
+ }
187
+ })
188
+
189
+ t .Run ("valid names" , func (t * testing.T ) {
190
+ db , cleanupFunc := newTestDB (t )
191
+ defer cleanupFunc ()
192
+
193
+ // initial indexes
194
+ key1 , err := db .schemaIndexPrefix ("index1" )
195
+ if err != nil {
196
+ t .Fatal (err )
197
+ }
198
+ key2 , err := db .schemaIndexPrefix ("index2" )
199
+ if err != nil {
200
+ t .Fatal (err )
201
+ }
202
+
203
+ // name the first one
204
+ renamed , err := db .RenameIndex ("index1" , "index1new" )
205
+ if err != nil {
206
+ t .Fatal (err )
207
+ }
208
+ if ! renamed {
209
+ t .Fatal ("index not renamed" )
210
+ }
211
+
212
+ // validate that the index key stays the same
213
+ key1same , err := db .schemaIndexPrefix ("index1new" )
214
+ if err != nil {
215
+ t .Fatal (err )
216
+ }
217
+ if key1 != key1same {
218
+ t .Fatal ("indexes renamed, but keys are not the same" )
219
+ }
220
+
221
+ // validate that the independent index is not changed
222
+ key2same , err := db .schemaIndexPrefix ("index2" )
223
+ if err != nil {
224
+ t .Fatal (err )
225
+ }
226
+ if key2 != key2same {
227
+ t .Fatal ("independent index key has changed" )
228
+ }
229
+
230
+ // validate that it is safe to create a new index with previous name
231
+ key1renew , err := db .schemaIndexPrefix ("index1" )
232
+ if err != nil {
233
+ t .Fatal (err )
234
+ }
235
+ if key1 == key1renew {
236
+ t .Fatal ("renewed index and the original one have the same key" )
237
+ }
238
+ if key2 == key1renew {
239
+ t .Fatal ("renewed index and the independent one have the same key" )
240
+ }
241
+ })
242
+ }
0 commit comments