Files
docdb/integration/insert_command_test.go
zeekayandHanzo Dev 06d5eca873 Declare operations on the group that serves them
zip's registration is a verb method on the group, so an operation reads as the
group answering it rather than a function taking the group, and a route that is
not a typed operation says which method it answers with. The package-level form
and the Router interface it existed to accept any target through are both gone.

The toolchain and the pin move together, deliberately. Below zip v1.36.50 the
JSON encoder was selected on a build tag, so a repo whose go directive rises
first writes a wire that is neither the old release's nor the new one's until the
pin catches up.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 06:11:42 -07:00

208 lines
5.8 KiB
Go

// Copyright 2021 DocDB 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/hanzoai/docdb/integration/setup"
"github.com/hanzoai/docdb/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 DocDB, ignored if empty
failsForDocDB string
}{
"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: `Duplicate key violation on the requested collection: Index '_id_'`,
},
"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: `Duplicate key violation on the requested collection: Index '_id_'`,
},
"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",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/295",
},
"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",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/295",
},
"InsertDuplicateID": {
toInsert: []any{
bson.D{{"_id", "foo"}, {"_id", "bar"}},
},
ordered: false,
werr: &mongo.WriteError{
Code: 2,
Message: "can't have multiple _id fields in one document",
},
altMessage: `invalid key: "_id" (duplicate keys are not allowed)`,
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/293",
},
} {
t.Run(name, func(tt *testing.T) {
var t testing.TB = tt
if tc.failsForDocDB != "" {
t = setup.FailsForDocDB(tt, tc.failsForDocDB)
}
tt.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(tt, 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"},
})
expected := mongo.WriteError{
Message: "E11000 duplicate key error collection: TestInsertIDDifferentTypes.TestInsertIDDifferentTypes index: _id_ dup key: { _id: 1 }",
Code: 11000,
}
AssertEqualAltWriteError(
t,
expected,
`Duplicate key violation on the requested collection: Index '_id_'`,
err,
)
_, err = collection.InsertOne(ctx, bson.D{
{"_id", float32(1)},
{"v", "foo3"},
})
expected = mongo.WriteError{
Message: "E11000 duplicate key error collection: TestInsertIDDifferentTypes.TestInsertIDDifferentTypes index: _id_ dup key: { _id: 1.0 }",
Code: 11000,
}
AssertEqualAltWriteError(
t,
expected,
`Duplicate key violation on the requested collection: Index '_id_'`,
err,
)
}