Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return command error from findAndModify #2646

Merged
merged 17 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add uncovered test case for update with upsert
  • Loading branch information
chilagrow committed May 19, 2023
commit c005d0ae65d7d73fec56418bf88e65362ad308bc
21 changes: 14 additions & 7 deletions integration/update_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ import (

// updateCompatTestCase describes update compatibility test case.
type updateCompatTestCase struct {
update bson.D // required if replace is nil
replace bson.D // required if update is nil
filter bson.D // defaults to bson.D{{"_id", id}}
resultType compatTestCaseResultType // defaults to nonEmptyResult
providers []shareddata.Provider // defaults to shareddata.AllProviders()
update bson.D // required if replace is nil
replace bson.D // required if update is nil
filter bson.D // defaults to bson.D{{"_id", id}}
replaceOpts *options.ReplaceOptions // defaults to nil
resultType compatTestCaseResultType // defaults to nonEmptyResult
providers []shareddata.Provider // defaults to shareddata.AllProviders()

skip string // skips test if non-empty
skipForTigris string // skips test for Tigris if non-empty
Expand Down Expand Up @@ -111,8 +112,8 @@ func testUpdateCompat(t *testing.T, testCases map[string]updateCompatTestCase) {
targetUpdateRes, targetErr = targetCollection.UpdateOne(ctx, filter, update)
compatUpdateRes, compatErr = compatCollection.UpdateOne(ctx, filter, update)
} else {
targetUpdateRes, targetErr = targetCollection.ReplaceOne(ctx, filter, replace)
compatUpdateRes, compatErr = compatCollection.ReplaceOne(ctx, filter, replace)
targetUpdateRes, targetErr = targetCollection.ReplaceOne(ctx, filter, replace, tc.replaceOpts)
compatUpdateRes, compatErr = compatCollection.ReplaceOne(ctx, filter, replace, tc.replaceOpts)
}

if targetErr != nil {
Expand Down Expand Up @@ -441,6 +442,12 @@ func TestUpdateCompat(t *testing.T) {
"ReplaceEmptyDocument": {
replace: bson.D{},
},
"ReplaceNonExistentUpsert": {
filter: bson.D{{"non-existent", "no-match"}},
replace: bson.D{{"_id", "new"}},
replaceOpts: &options.ReplaceOptions{Upsert: pointer.ToBool(true)},
resultType: emptyResult,
},
}

testUpdateCompat(t, testCases)
Expand Down
18 changes: 17 additions & 1 deletion integration/update_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"math"
"testing"

"github.com/AlekSi/pointer"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
Expand All @@ -34,6 +36,7 @@ func TestUpdateFieldSet(t *testing.T) {
for name, tc := range map[string]struct {
id string
update bson.D
opts *options.UpdateOptions
expected bson.D
err *mongo.WriteError
stat *mongo.UpdateResult
Expand All @@ -59,13 +62,26 @@ func TestUpdateFieldSet(t *testing.T) {
UpsertedCount: 0,
},
},
"NonExistentUpsert": {
id: "non-existent",
update: bson.D{{"$set", bson.D{{"v", int32(42)}}}},
opts: &options.UpdateOptions{Upsert: pointer.ToBool(true)},
expected: bson.D{{"_id", "non-existent"}, {"v", int32(42)}},

stat: &mongo.UpdateResult{
MatchedCount: 0,
ModifiedCount: 0,
UpsertedCount: 1,
UpsertedID: "non-existent",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t, shareddata.Scalars, shareddata.Composites)

res, err := collection.UpdateOne(ctx, bson.D{{"_id", tc.id}}, tc.update)
res, err := collection.UpdateOne(ctx, bson.D{{"_id", tc.id}}, tc.update, tc.opts)
if tc.err != nil {
require.Nil(t, tc.expected)
AssertEqualAltWriteError(t, *tc.err, tc.alt, err)
Expand Down