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 more validation and tests for $unset #2853

Merged
merged 10 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
211 changes: 200 additions & 11 deletions integration/aggregate_documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,7 @@ func TestAggregateProjectErrors(t *testing.T) {
ctx, collection := setup.Setup(t)

_, err := collection.Aggregate(ctx, tc.pipeline)

if tc.altMessage != "" {
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
return
}

AssertEqualCommandError(t, *tc.err, err)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}
Expand Down Expand Up @@ -460,13 +454,208 @@ func TestAggregateSetErrors(t *testing.T) {
ctx, collection := setup.Setup(t)

_, err := collection.Aggregate(ctx, tc.pipeline)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}

if tc.altMessage != "" {
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
return
func TestAggregateUnsetErrors(t *testing.T) {
t.Parallel()

for name, tc := range map[string]struct { //nolint:vet // used for test only
pipeline bson.A // required, aggregation pipeline stages

err *mongo.CommandError // required
altMessage string // optional, alternative error message
skip string // optional, skip test with a specified reason
}{
"EmptyString": {
pipeline: bson.A{
bson.D{{"$unset", ""}},
},
err: &mongo.CommandError{
Code: 40352,
Name: "Location40352",
Message: "Invalid $unset :: caused by :: FieldPath cannot be constructed with empty string",
},
},
"InvalidType": {
pipeline: bson.A{
bson.D{{"$unset", bson.D{}}},
},
err: &mongo.CommandError{
Code: 31002,
Name: "Location31002",
Message: "$unset specification must be a string or an array",
},
},
"PathEmptyKey": {
pipeline: bson.A{
bson.D{{"$unset", "v..foo"}},
},
err: &mongo.CommandError{
Code: 15998,
Name: "Location15998",
Message: "Invalid $unset :: caused by :: FieldPath field names may not be empty strings.",
},
},
"PathEmptySuffixKey": {
pipeline: bson.A{
bson.D{{"$unset", "v."}},
},
err: &mongo.CommandError{
Code: 40353,
Name: "Location40353",
Message: "Invalid $unset :: caused by :: FieldPath must not end with a '.'.",
},
},
"PathEmptyPrefixKey": {
pipeline: bson.A{
bson.D{{"$unset", ".v"}},
},
err: &mongo.CommandError{
Code: 15998,
Name: "Location15998",
Message: "Invalid $unset :: caused by :: FieldPath field names may not be empty strings.",
},
},
"PathDollarPrefix": {
pipeline: bson.A{
bson.D{{"$unset", "$v"}},
},
err: &mongo.CommandError{
Code: 16410,
Name: "Location16410",
Message: "Invalid $unset :: caused by :: FieldPath field names may not start with '$'. Consider using $getField or $setField.",
},
},
"ArrayEmpty": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{}}},
},
err: &mongo.CommandError{
Code: 31119,
Name: "Location31119",
Message: "$unset specification must be a string or an array with at least one field",
},
altMessage: "$unset specification must be a string or an array with at least one field",
},
"ArrayInvalidType": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{"field1", 1}}},
},
err: &mongo.CommandError{
Code: 31120,
Name: "Location31120",
Message: "$unset specification must be a string or an array containing only string values",
},
},
"ArrayEmptyString": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{""}}},
},
err: &mongo.CommandError{
Code: 40352,
Name: "Location40352",
Message: "Invalid $unset :: caused by :: FieldPath cannot be constructed with empty string",
},
},
"ArrayPathDuplicate": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{"v", "v"}}},
},
err: &mongo.CommandError{
Code: 31250,
Name: "Location31250",
Message: "Invalid $unset :: caused by :: Path collision at v",
},
},
"ArrayPathOverwrites": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{"v", "v.foo"}}},
},
err: &mongo.CommandError{
Code: 31249,
Name: "Location31249",
Message: "Invalid $unset :: caused by :: Path collision at v.foo remaining portion foo",
},
},
"ArrayPathOverwritesRemaining": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{"v", "v.foo.bar"}}},
},
err: &mongo.CommandError{
Code: 31249,
Name: "Location31249",
Message: "Invalid $unset :: caused by :: Path collision at v.foo.bar remaining portion foo.bar",
},
},
"ArrayPathCollision": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{"v.foo", "v"}}},
},
err: &mongo.CommandError{
Code: 31250,
Name: "Location31250",
Message: "Invalid $unset :: caused by :: Path collision at v",
},
},
"ArrayPathEmptyKey": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{"v..foo"}}},
},
err: &mongo.CommandError{
Code: 15998,
Name: "Location15998",
Message: "Invalid $unset :: caused by :: FieldPath field names may not be empty strings.",
},
},
"ArrayPathEmptySuffixKey": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{"v."}}},
},
err: &mongo.CommandError{
Code: 40353,
Name: "Location40353",
Message: "Invalid $unset :: caused by :: FieldPath must not end with a '.'.",
},
},
"ArrayPathEmptyPrefixKey": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{".v"}}},
},
err: &mongo.CommandError{
Code: 15998,
Name: "Location15998",
Message: "Invalid $unset :: caused by :: FieldPath field names may not be empty strings.",
},
},
"ArrayPathDollarPrefix": {
pipeline: bson.A{
bson.D{{"$unset", bson.A{"$v"}}},
},
err: &mongo.CommandError{
Code: 16410,
Name: "Location16410",
Message: "Invalid $unset :: caused by :: FieldPath field names may not start with '$'. Consider using $getField or $setField.",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
if tc.skip != "" {
t.Skip(tc.skip)
}

AssertEqualCommandError(t, *tc.err, err)
t.Parallel()

require.NotNil(t, tc.pipeline, "pipeline must not be nil")
require.NotNil(t, tc.err, "err must not be nil")

ctx, collection := setup.Setup(t)

_, err := collection.Aggregate(ctx, tc.pipeline)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}
30 changes: 30 additions & 0 deletions integration/findandmodify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,36 @@ func TestFindAndModifyCommandErrors(t *testing.T) {
Message: "The update path 'v.' contains an empty field name, which is not allowed.",
},
},
"ConflictCollision": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", false}}}}},
{"update", bson.D{
{"$set", bson.D{{"v", "val"}}},
{"$min", bson.D{{"v.foo", "val"}}},
}},
},
err: &mongo.CommandError{
Code: 40,
Name: "ConflictingUpdateOperators",
Message: "Updating the path 'v.foo' would create a conflict at 'v'",
},
altMessage: "Updating the path 'v' would create a conflict at 'v'",
},
"ConflictOverwrite": {
command: bson.D{
{"query", bson.D{{"_id", bson.D{{"$exists", false}}}}},
{"update", bson.D{
{"$set", bson.D{{"v.foo", "val"}}},
{"$min", bson.D{{"v", "val"}}},
}},
},
err: &mongo.CommandError{
Code: 40,
Name: "ConflictingUpdateOperators",
Message: "Updating the path 'v' would create a conflict at 'v'",
},
altMessage: "Updating the path 'v.foo' would create a conflict at 'v.foo'",
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
Expand Down