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

Add tests to findAndModify on $exists operator #2422

Merged
merged 16 commits into from
Apr 19, 2023
271 changes: 252 additions & 19 deletions integration/findandmodify_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,171 @@ func TestFindAndModifyCompatUpdate(t *testing.T) {
},
skipForTigris: "schema validation would fail",
},
"InvalidOperator": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", false}}}}},
{"update", bson.D{{"$invalid", "non-existent-field"}}},
},
},
"OperatorConflict": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", false}}}}},
{"update", bson.D{
{"$set", bson.D{{"v", 4}}},
{"$inc", bson.D{{"v", 4}}},
}},
},
},
}

testFindAndModifyCompat(t, testCases)
}

func TestFindAndModifyCompatUpdateSet(t *testing.T) {
t.Parallel()

testCases := map[string]findAndModifyCompatTestCase{
"NonExistentExistsTrue": {
command: bson.D{
{"query", bson.D{{"non-existent", bson.D{{"$exists", true}}}}},
{"update", bson.D{{"$set", bson.D{{"v", "foo"}}}}},
},
},
"NonExistentExistsFalse": {
command: bson.D{
{"query", bson.D{{"non-existent", bson.D{{"$exists", false}}}}},
{"update", bson.D{{"$set", bson.D{{"v", "foo"}}}}},
},
},
"ExistsTrue": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", true}}}}},
{"update", bson.D{{"$set", bson.D{{"v", "foo"}}}}},
},
},
"ExistsFalse": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", false}}}}},
{"update", bson.D{{"$set", bson.D{{"v", "foo"}}}}},
},
},
}

testFindAndModifyCompat(t, testCases)
}

func TestFindAndModifyCompatUpdateUnset(t *testing.T) {
t.Parallel()

testCases := map[string]findAndModifyCompatTestCase{
"NonExistentExistsTrue": {
command: bson.D{
{"query", bson.D{{"non-existent", bson.D{{"$exists", true}}}}},
{"update", bson.D{{"$unset", "v"}}},
},
},
"NonExistentExistsFalse": {
command: bson.D{
{"query", bson.D{{"non-existent", bson.D{{"$exists", false}}}}},
{"update", bson.D{{"$unset", "v"}}},
},
},
"ExistsTrue": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", true}}}}},
{"update", bson.D{{"$unset", "v"}}},
},
},
"ExistsFalse": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", false}}}}},
{"update", bson.D{{"$unset", "v"}}},
},
},
"UnsetNonExistentField": {
command: bson.D{
{"query", bson.D{{"_id", "double"}}},
{"update", bson.D{{"$unset", "non-existent-field"}}},
},
},
}

testFindAndModifyCompat(t, testCases)
}

func TestFindAndModifyCompatUpdateCurrentDate(t *testing.T) {
t.Parallel()

testCases := map[string]findAndModifyCompatTestCase{
"NotDocument": {
command: bson.D{
{"query", bson.D{{"_id", "datetime"}}},
{"update", bson.D{{"$currentDate", 1}}},
},
},
"UnknownOption": {
command: bson.D{
{"query", bson.D{{"_id", "datetime"}}},
{"update", bson.D{{"$currentDate", bson.D{{"v", bson.D{{"foo", int32(1)}}}}}}},
},
},
"NotDate": {
command: bson.D{
{"query", bson.D{{"_id", "datetime"}}},
{"update", bson.D{{"$currentDate", bson.D{{"v", bson.D{{"$type", int32(1)}}}}}}},
},
},
"UnknownType": {
command: bson.D{
{"query", bson.D{{"_id", "datetime"}}},
{"update", bson.D{{"$currentDate", bson.D{{"v", bson.D{{"$type", "unknown"}}}}}}},
},
},
"InvalidType": {
command: bson.D{
{"query", bson.D{{"_id", "datetime"}}},
{"update", bson.D{{"$currentDate", bson.D{{"v", 1}}}}},
},
},
}

testFindAndModifyCompat(t, testCases)
}

func TestFindAndModifyCompatUpdateRename(t *testing.T) {
t.Parallel()

testCases := map[string]findAndModifyCompatTestCase{
"NotDocument": {
command: bson.D{
{"query", bson.D{{"_id", "int64"}}},
{"update", bson.D{{"$rename", 1}}},
},
},
"NonStringTargetField": {
command: bson.D{
{"query", bson.D{{"_id", "int64"}}},
{"update", bson.D{{"$rename", bson.D{{"v", 0}}}}},
},
},
"SameTargetField": {
command: bson.D{
{"query", bson.D{{"_id", "int64"}}},
{"update", bson.D{{"$rename", bson.D{{"v", "v"}}}}},
},
},
"DuplicateSource": {
command: bson.D{
{"query", bson.D{{"_id", "int64"}}},
{"update", bson.D{{"$rename", bson.D{{"v", "w"}, {"v", "x"}}}}},
},
},
"DuplicateTarget": {
command: bson.D{
{"query", bson.D{{"_id", "int64"}}},
{"update", bson.D{{"$rename", bson.D{{"v", "w"}, {"x", "w"}}}}},
},
},
}

testFindAndModifyCompat(t, testCases)
Expand Down Expand Up @@ -244,21 +409,6 @@ func TestFindAndModifyCompatUpsert(t *testing.T) {
t.Parallel()

testCases := map[string]findAndModifyCompatTestCase{
"Upsert": {
command: bson.D{
{"query", bson.D{{"_id", "double"}}},
{"update", bson.D{{"$set", bson.D{{"v", 43.13}}}}},
{"upsert", true},
},
},
"UpsertNew": {
command: bson.D{
{"query", bson.D{{"_id", "double"}}},
{"update", bson.D{{"$set", bson.D{{"v", 43.13}}}}},
{"upsert", true},
{"new", true},
},
},
w84thesun marked this conversation as resolved.
Show resolved Hide resolved
"UpsertNoSuchDocument": {
command: bson.D{
{"query", bson.D{{"_id", "no-such-doc"}}},
Expand Down Expand Up @@ -319,18 +469,101 @@ func TestFindAndModifyCompatUpsert(t *testing.T) {
{"update", bson.D{{"v", "replaced"}}},
},
},
"UnsetForNonExisting": {
}

testFindAndModifyCompat(t, testCases)
}

func TestFindAndModifyCompatUpsertSet(t *testing.T) {
t.Parallel()

testCases := map[string]findAndModifyCompatTestCase{
"Upsert": {
command: bson.D{
{"query", bson.D{{"_id", "double"}}},
{"update", bson.D{{"$set", bson.D{{"v", 43.13}}}}},
{"upsert", true},
},
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
},
"UpsertNew": {
command: bson.D{
{"query", bson.D{{"_id", "double"}}},
{"update", bson.D{{"$set", bson.D{{"v", 43.13}}}}},
{"upsert", true},
{"new", true},
},
},
"UpsertNonExistent": {
command: bson.D{
{"query", bson.D{{"_id", "non-existent"}}},
{"upsert", true},
{"update", bson.D{{"$set", bson.D{{"v", "43"}}}}},
},
},
"UpsertNewNonExistent": {
command: bson.D{
{"query", bson.D{{"_id", "non-existent"}}},
{"upsert", true},
{"update", bson.D{{"$set", bson.D{{"v", "43"}}}}},
{"new", true},
},
},
"NonExistentExistsFalse": {
command: bson.D{
{"query", bson.D{{"non-existent", bson.D{{"$exists", false}}}}},
{"upsert", true},
{"update", bson.D{{"$set", bson.D{{"v", "foo"}}}}},
},
},
"ExistsTrue": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", true}}}}},
{"upsert", true},
{"update", bson.D{{"$unset", "invalid"}}},
{"update", bson.D{{"$set", bson.D{{"v", "foo"}}}}},
},
},
"UnsetForExisting": {
}

testFindAndModifyCompat(t, testCases)
}

func TestFindAndModifyCompatUpsertUnset(t *testing.T) {
t.Parallel()

testCases := map[string]findAndModifyCompatTestCase{
"NonExistentExistsTrue": {
command: bson.D{
{"query", bson.D{{"non-existent", bson.D{{"$exists", true}}}}},
{"upsert", true},
{"update", bson.D{{"$unset", "v"}}},
},
},
"NonExistentExistsFalse": {
command: bson.D{
{"query", bson.D{{"non-existent", bson.D{{"$exists", false}}}}},
{"upsert", true},
{"update", bson.D{{"$unset", "v"}}},
},
},
"ExistsTrue": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", true}}}}},
{"upsert", true},
{"update", bson.D{{"$unset", "v"}}},
},
},
"ExistsFalse": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", false}}}}},
{"upsert", true},
{"update", bson.D{{"$unset", "invalid"}}},
{"update", bson.D{{"$unset", "v"}}},
},
},
"UnsetNonExistentField": {
command: bson.D{
{"query", bson.D{{"_id", "double"}}},
{"upsert", true},
{"update", bson.D{{"$unset", "non-existent-field"}}},
},
},
}
Expand Down
22 changes: 22 additions & 0 deletions integration/findandmodify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,28 @@ func TestFindAndModifyUpsertComplex(t *testing.T) {
},
skipForTigris: "schema validation would fail",
},
"ExistsFalse": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", false}}}}},
{"upsert", true},
{"update", bson.D{{"$set", bson.D{{"v", "foo"}}}}},
},
lastErrorObject: bson.D{
{"n", int32(1)},
{"updatedExisting", false},
},
},
"NonExistentExistsTrue": {
command: bson.D{
{"query", bson.D{{"non-existent", bson.D{{"$exists", true}}}}},
{"upsert", true},
{"update", bson.D{{"$set", bson.D{{"v", "foo"}}}}},
},
lastErrorObject: bson.D{
{"n", int32(1)},
{"updatedExisting", false},
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/common/findandmodify.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func GetFindAndModifyParams(doc *types.Document, l *zap.Logger) (*FindAndModifyP
)
}

hasUpdateOperators, err := HasSupportedUpdateModifiers(update)
hasUpdateOperators, err := HasSupportedUpdateModifiers(command, update)
if err != nil {
return nil, err
}
Expand Down
Loading