forked from FerretDB/FerretDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes_command_test.go
599 lines (550 loc) · 16.4 KB
/
indexes_command_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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package integration
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
)
// TestListIndexesCommandNonExistentNS tests that the listIndexes command returns a particular error
// when the namespace (either database or collection) does not exist.
func TestListIndexesCommandNonExistentNS(t *testing.T) {
t.Parallel()
s := setup.SetupWithOpts(t, &setup.SetupOpts{
Providers: []shareddata.Provider{shareddata.Composites},
})
ctx, collection := s.Ctx, s.Collection
// Calling driver's method collection.Database().Collection("nonexistent").Indexes().List(ctx)
// doesn't return an error for non-existent namespaces.
// So that we should use RunCommand to check the behaviour.
res := collection.Database().RunCommand(ctx, bson.D{{"listIndexes", "nonexistentColl"}})
err := res.Err()
expected := mongo.CommandError{
Code: 26,
Name: "NamespaceNotFound",
Message: "ns does not exist: " + collection.Database().Name() + ".nonexistentColl",
}
AssertEqualCommandError(t, expected, err)
// Drop database and check that the error is correct.
require.NoError(t, collection.Database().Drop(ctx))
res = collection.Database().RunCommand(ctx, bson.D{{"listIndexes", collection.Name()}})
err = res.Err()
expected = mongo.CommandError{
Code: 26,
Name: "NamespaceNotFound",
Message: "ns does not exist: " + collection.Database().Name() + "." + collection.Name(),
}
AssertEqualCommandError(t, expected, err)
}
func TestDropIndexesCommandErrors(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct { //nolint:vet // for readability
toCreate []mongo.IndexModel // optional, if set, create the given indexes before drop is called
toDrop any // required, index to drop
command bson.D // optional, if set it runs this command instead of dropping toDrop
err *mongo.CommandError // required, expected error from MongoDB
altMessage string // optional, alternative error message for FerretDB, ignored if empty
skip string // optional, skip test with a specified reason
}{
"InvalidType": {
toDrop: true,
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'dropIndexes.index' is the wrong type 'bool', expected types '[string, object']",
},
altMessage: `BSON field 'dropIndexes.index' is the wrong type 'bool', expected types '[string, object]'`,
},
"MultipleIndexesByKey": {
toCreate: []mongo.IndexModel{
{Keys: bson.D{{"v", -1}}},
{Keys: bson.D{{"v.foo", -1}}},
},
toDrop: bson.A{bson.D{{"v", -1}}, bson.D{{"v.foo", -1}}},
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'dropIndexes.index' is the wrong type 'array', expected types '[string']",
},
altMessage: `BSON field 'dropIndexes.index' is the wrong type 'array', expected types '[string, object]'`,
},
"NonExistentMultipleIndexes": {
err: &mongo.CommandError{
Code: 27,
Name: "IndexNotFound",
Message: "index not found with name [non-existent]",
},
toDrop: bson.A{"non-existent", "invalid"},
},
"InvalidMultipleIndexType": {
toDrop: bson.A{1},
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'dropIndexes.index' is the wrong type 'array', expected types '[string']",
},
altMessage: `BSON field 'dropIndexes.index' is the wrong type 'array', expected types '[string, object]'`,
},
"InvalidDocumentIndex": {
toDrop: bson.D{{"invalid", "invalid"}},
err: &mongo.CommandError{
Code: 27,
Name: "IndexNotFound",
Message: "can't find index with key: { invalid: \"invalid\" }",
},
},
"NonExistentKey": {
toDrop: bson.D{{"non-existent", 1}},
err: &mongo.CommandError{
Code: 27,
Name: "IndexNotFound",
Message: "can't find index with key: { non-existent: 1 }",
},
},
"DocumentIndexID": {
toDrop: bson.D{{"_id", 1}},
err: &mongo.CommandError{
Code: 72,
Name: "InvalidOptions",
Message: "cannot drop _id index",
},
},
"MissingIndexField": {
command: bson.D{
{"dropIndexes", "collection"},
},
err: &mongo.CommandError{
Code: 40414,
Name: "Location40414",
Message: "BSON field 'dropIndexes.index' is missing but a required field",
},
},
"NonExistentDescendingID": {
toDrop: bson.D{{"_id", -1}},
err: &mongo.CommandError{
Code: 27,
Name: "IndexNotFound",
Message: "can't find index with key: { _id: -1 }",
},
},
"NonExistentMultipleKeyIndex": {
toDrop: bson.D{
{"non-existent1", -1},
{"non-existent2", -1},
},
err: &mongo.CommandError{
Code: 27,
Name: "IndexNotFound",
Message: "can't find index with key: { non-existent1: -1, non-existent2: -1 }",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
if tc.skip != "" {
t.Skip(tc.skip)
}
t.Parallel()
if tc.command != nil {
require.Nil(t, tc.toDrop, "toDrop must be nil when using command")
} else {
require.NotNil(t, tc.toDrop, "toDrop must not be nil")
}
require.NotNil(t, tc.err, "err must not be nil")
s := setup.SetupWithOpts(t, &setup.SetupOpts{
Providers: []shareddata.Provider{shareddata.Composites},
})
ctx, collection := s.Ctx, s.Collection
if tc.toCreate != nil {
_, err := collection.Indexes().CreateMany(ctx, tc.toCreate)
require.NoError(t, err)
}
command := bson.D{
{"dropIndexes", collection.Name()},
{"index", tc.toDrop},
}
if tc.command != nil {
command = tc.command
}
var res bson.D
err := collection.Database().RunCommand(ctx, command).Decode(&res)
assert.Nil(t, res)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}
func TestCreateIndexesCommandInvalidSpec(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
indexes any // optional
missingIndexes bool // optional, if set indexes must be nil
noProvider bool // if set, no provider is added.
err *mongo.CommandError // required, expected error from MongoDB
altMessage string // optional, alternative error message for FerretDB, ignored if empty
skip string // optional, skip test with a specified reason
}{
"EmptyIndexes": {
indexes: bson.A{},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "Must specify at least one index to create",
},
},
"MissingIndexes": {
missingIndexes: true,
err: &mongo.CommandError{
Code: 40414,
Name: "Location40414",
Message: "BSON field 'createIndexes.indexes' is missing but a required field",
},
},
"NilIndexes": {
indexes: nil,
err: &mongo.CommandError{
Code: 10065,
Name: "Location10065",
Message: "invalid parameter: expected an object (indexes)",
},
},
"InvalidTypeObject": {
indexes: bson.D{},
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'createIndexes.indexes' is the wrong type 'object', expected type 'array'",
},
},
"InvalidTypeInt": {
indexes: 42,
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'createIndexes.indexes' is the wrong type 'int', expected type 'array'",
},
},
"InvalidTypeArrayString": {
indexes: bson.A{"invalid"},
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'createIndexes.indexes.0' is the wrong type 'string', expected type 'object'",
},
},
"IDIndex": {
indexes: bson.A{
bson.D{
{"key", bson.D{{"_id", 1}}},
{"name", "_id_"},
{"unique", true},
},
},
err: &mongo.CommandError{
Code: 197,
Name: "InvalidIndexSpecificationOption",
Message: `The field 'unique' is not valid for an _id index specification.` +
` Specification: { key: { _id: 1 }, name: "_id_", unique: true, v: 2 }`,
},
},
"MissingName": {
indexes: bson.A{
bson.D{
{"key", bson.D{{"v", 1}}},
},
},
err: &mongo.CommandError{
Code: 9,
Name: "FailedToParse",
Message: `Error in specification { key: { v: 1 } } :: caused by :: ` +
`The 'name' field is a required property of an index specification`,
},
},
"EmptyName": {
indexes: bson.A{
bson.D{
{"key", bson.D{{"v", -1}}},
{"name", ""},
},
},
err: &mongo.CommandError{
Code: 67,
Name: "CannotCreateIndex",
Message: `Error in specification { key: { v: -1 }, name: "", v: 2 } :: caused by :: index name cannot be empty`,
},
altMessage: `Error in specification { key: { v: -1 }, name: "" } :: caused by :: index name cannot be empty`,
},
"MissingKey": {
indexes: bson.A{
bson.D{},
},
err: &mongo.CommandError{
Code: 9,
Name: "FailedToParse",
Message: `Error in specification {} :: caused by :: The 'key' field is a required property of an index specification`,
},
},
"IdenticalIndex": {
indexes: bson.A{
bson.D{
{"key", bson.D{{"v", 1}}},
{"name", "v_1"},
},
bson.D{
{"key", bson.D{{"v", 1}}},
{"name", "v_1"},
},
},
noProvider: true,
err: &mongo.CommandError{
Code: 68,
Name: "IndexAlreadyExists",
Message: `Identical index already exists: v_1`,
},
},
"SameName": {
indexes: bson.A{
bson.D{
{"key", bson.D{{"foo", -1}}},
{"name", "index-name"},
},
bson.D{
{"key", bson.D{{"bar", -1}}},
{"name", "index-name"},
},
},
noProvider: true,
err: &mongo.CommandError{
Code: 86,
Name: "IndexKeySpecsConflict",
Message: "An existing index has the same name as the requested index. " +
"When index names are not specified, they are auto generated and can " +
"cause conflicts. Please refer to our documentation. " +
"Requested index: { v: 2, key: { bar: -1 }, name: \"index-name\" }, " +
"existing index: { v: 2, key: { foo: -1 }, name: \"index-name\" }",
},
altMessage: "An existing index has the same name as the requested index. " +
"When index names are not specified, they are auto generated and can " +
"cause conflicts. Please refer to our documentation. " +
"Requested index: { key: { bar: -1 }, name: \"index-name\" }, " +
"existing index: { key: { foo: -1 }, name: \"index-name\" }",
},
"SameIndex": {
indexes: bson.A{
bson.D{
{"key", bson.D{{"v", -1}}},
{"name", "foo"},
},
bson.D{
{"key", bson.D{{"v", -1}}},
{"name", "bar"},
},
},
noProvider: true,
err: &mongo.CommandError{
Code: 85,
Name: "IndexOptionsConflict",
Message: "Index already exists with a different name: foo",
},
},
"UniqueTypeDocument": {
indexes: bson.A{
bson.D{
{"key", bson.D{{"v", 1}}},
{"name", "unique_index"},
{"unique", bson.D{}},
},
},
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: `Error in specification { key: { v: 1 }, name: "unique_index", unique: {} } ` +
`:: caused by :: The field 'unique has value unique: {}, which is not convertible to bool`,
},
altMessage: `Error in specification { key: { v: 1 }, name: "unique_index", unique: { } } ` +
`:: caused by :: The field 'unique' has value unique: { }, which is not convertible to bool`,
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
if tc.skip != "" {
t.Skip(tc.skip)
}
t.Parallel()
if tc.missingIndexes {
require.Nil(t, tc.indexes, "indexes must be nil if missingIndexes is true")
}
var providers []shareddata.Provider
if !tc.noProvider {
// one provider is enough to check for errors
providers = append(providers, shareddata.ArrayDocuments)
}
ctx, collection := setup.Setup(t, providers...)
var rest bson.D
if !tc.missingIndexes {
rest = append(rest, bson.E{Key: "indexes", Value: tc.indexes})
}
command := append(bson.D{
{"createIndexes", collection.Name()},
},
rest...,
)
var res bson.D
err := collection.Database().RunCommand(ctx, command).Decode(&res)
require.Nil(t, res)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}
func TestCreateIndexesCommandInvalidCollection(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
collectionName any
indexes any
err *mongo.CommandError
altMessage string
skip string
}{
"InvalidTypeCollection": {
collectionName: 42,
indexes: bson.A{
bson.D{
{"key", bson.D{{"v", 1}}},
{"name", "v_1"},
},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type int",
},
altMessage: "required parameter \"createIndexes\" has type int32 (expected string)",
},
"NilCollection": {
collectionName: nil,
indexes: bson.A{
bson.D{
{"key", bson.D{{"v", 1}}},
{"name", "v_1"},
},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type null",
},
altMessage: "required parameter \"createIndexes\" has type types.NullType (expected string)",
},
"EmptyCollection": {
collectionName: "",
indexes: bson.A{
bson.D{
{"key", bson.D{{"v", 1}}},
{"name", "v_1"},
},
},
err: &mongo.CommandError{
Code: 73,
Name: "InvalidNamespace",
Message: "Invalid namespace specified 'TestCreateIndexesCommandInvalidCollection-EmptyCollection.'",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
if tc.skip != "" {
t.Skip(tc.skip)
}
t.Parallel()
ctx, collection := setup.Setup(t)
command := bson.D{
{"createIndexes", tc.collectionName},
{"indexes", tc.indexes},
}
var res bson.D
err := collection.Database().RunCommand(ctx, command).Decode(&res)
require.Nil(t, res)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}
func TestDropIndexesCommandInvalidCollection(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
collectionName any
indexName any
err *mongo.CommandError
altMessage string
skip string
}{
"NonExistentCollection": {
collectionName: "non-existent",
indexName: "index",
err: &mongo.CommandError{
Code: 26,
Name: "NamespaceNotFound",
Message: "ns not found TestDropIndexesCommandInvalidCollection-NonExistentCollection.non-existent",
},
},
"InvalidTypeCollection": {
collectionName: 42,
indexName: "index",
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type int",
},
altMessage: "required parameter \"dropIndexes\" has type int32 (expected string)",
},
"NilCollection": {
collectionName: nil,
indexName: "index",
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type null",
},
altMessage: "required parameter \"dropIndexes\" has type types.NullType (expected string)",
},
"EmptyCollection": {
collectionName: "",
indexName: "index",
err: &mongo.CommandError{
Code: 73,
Name: "InvalidNamespace",
Message: "Invalid namespace specified 'TestDropIndexesCommandInvalidCollection-EmptyCollection.'",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
if tc.skip != "" {
t.Skip(tc.skip)
}
t.Parallel()
ctx, collection := setup.Setup(t)
command := bson.D{
{"dropIndexes", tc.collectionName},
{"index", tc.indexName},
}
var res bson.D
err := collection.Database().RunCommand(ctx, command).Decode(&res)
require.Nil(t, res)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}