forked from FerretDB/FerretDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_command_test.go
186 lines (164 loc) · 5.43 KB
/
insert_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
// 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/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
)
func TestInsertCommandErrors(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct { //nolint:vet // used for testing only
toInsert []any // required, slice of bson.D to insert
ordered any // required, sets it to `ordered`
cerr *mongo.CommandError // optional, expected command error from MongoDB
werr *mongo.WriteError // optional, expected write error from MongoDB
altMessage string // optional, alternative error message for FerretDB, ignored if empty
skip string // optional, skip test with a specified reason
}{
"InsertOrderedInvalid": {
toInsert: []any{
bson.D{{"_id", "foo"}},
},
ordered: "foo",
cerr: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'insert.ordered' is the wrong type 'string', expected type 'bool'",
},
altMessage: "BSON field 'ordered' is the wrong type 'string', expected type 'bool'",
},
"InsertDuplicateKey": {
toInsert: []any{
bson.D{{"_id", "double"}},
},
ordered: false,
werr: &mongo.WriteError{
Code: 11000,
Message: `E11000 duplicate key error collection: ` +
`TestInsertCommandErrors-InsertDuplicateKey.TestInsertCommandErrors-InsertDuplicateKey index: _id_ dup key: { _id: "double" }`,
},
altMessage: "E11000 duplicate key error collection: TestInsertCommandErrors-InsertDuplicateKey.TestInsertCommandErrors-InsertDuplicateKey",
},
"InsertDuplicateKeyOrdered": {
toInsert: []any{
bson.D{{"foo", "bar"}},
bson.D{{"_id", "double"}},
},
ordered: true,
werr: &mongo.WriteError{
Code: 11000,
Index: 1,
Message: `E11000 duplicate key error collection: ` +
`TestInsertCommandErrors-InsertDuplicateKeyOrdered.TestInsertCommandErrors-InsertDuplicateKeyOrdered index: _id_ dup key: { _id: "double" }`,
},
altMessage: `E11000 duplicate key error collection: TestInsertCommandErrors-InsertDuplicateKeyOrdered.TestInsertCommandErrors-InsertDuplicateKeyOrdered`,
},
"InsertArray": {
toInsert: []any{
bson.D{{"a", int32(1)}},
bson.A{},
},
ordered: true,
cerr: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'insert.documents.1' is the wrong type 'array', expected type 'object'",
},
},
"InsertArrayAsDocumentID": {
toInsert: []any{
bson.D{{"_id", bson.A{int32(42), int32(42)}}},
},
ordered: false,
werr: &mongo.WriteError{
Code: 53,
Message: "The '_id' value cannot be of type array",
},
},
"InsertRegexAsDocumentID": {
toInsert: []any{
bson.D{{"_id", primitive.Regex{Pattern: "foo", Options: "i"}}},
},
ordered: false,
werr: &mongo.WriteError{
Code: 53,
Message: "The '_id' value cannot be of type regex",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
if tc.skip != "" {
t.Skip(tc.skip)
}
t.Parallel()
require.NotNil(t, tc.toInsert, "toInsert must not be nil")
require.NotNil(t, tc.ordered, "ordered must not be nil")
ctx, collection := setup.Setup(t, shareddata.Scalars, shareddata.Composites)
var res bson.D
err := collection.Database().RunCommand(ctx, bson.D{
{"insert", collection.Name()},
{"documents", tc.toInsert},
{"ordered", tc.ordered},
}).Decode(&res)
assert.Nil(t, res)
if tc.cerr != nil {
AssertEqualAltCommandError(t, *tc.cerr, tc.altMessage, err)
return
}
if tc.werr != nil {
AssertEqualAltWriteError(t, *tc.werr, tc.altMessage, err)
return
}
require.NoError(t, err)
})
}
}
func TestInsertIDDifferentTypes(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t)
_, err := collection.InsertOne(ctx, bson.D{
{"_id", int64(1)},
{"v", "foo2"},
})
require.NoError(t, err)
_, err = collection.InsertOne(ctx, bson.D{
{"_id", int32(1)},
{"v", "foo1"},
})
AssertEqualAltWriteError(t, mongo.WriteError{
Message: "E11000 duplicate key error collection: TestInsertIDDifferentTypes.TestInsertIDDifferentTypes index: _id_ dup key: { _id: 1 }",
Code: 11000,
},
"E11000 duplicate key error collection: TestInsertIDDifferentTypes.TestInsertIDDifferentTypes",
err,
)
_, err = collection.InsertOne(ctx, bson.D{
{"_id", float32(1)},
{"v", "foo3"},
})
AssertEqualAltWriteError(t, mongo.WriteError{
Message: "E11000 duplicate key error collection: TestInsertIDDifferentTypes.TestInsertIDDifferentTypes index: _id_ dup key: { _id: 1.0 }",
Code: 11000,
},
"E11000 duplicate key error collection: TestInsertIDDifferentTypes.TestInsertIDDifferentTypes",
err,
)
}