Skip to content

Commit

Permalink
Merge branch 'main' into write-crc32
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored May 24, 2023
2 parents a922e9e + 72503d4 commit 1da7fd6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion integration/distinct_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/FerretDB/FerretDB/integration/shareddata"
)

// distinctCompatTestCase describes count compatibility test case.
// distinctCompatTestCase describes distinct compatibility test case.
type distinctCompatTestCase struct {
field string // required
filter bson.D // required
Expand Down
108 changes: 108 additions & 0 deletions integration/distinct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"

"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
)

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

ctx, coll := setup.Setup(t, shareddata.Scalars, shareddata.Composites)

for name, tc := range map[string]struct {
command any // required
collName any // optional
filter bson.D // required
err mongo.CommandError // required
}{
"EmptyCollection": {
command: "a",
filter: bson.D{},
collName: "",
err: mongo.CommandError{
Code: 73,
Name: "InvalidNamespace",
Message: "Invalid namespace specified 'TestDistinctErrors.'",
},
},
"CollectionTypeObject": {
command: "a",
filter: bson.D{},
collName: bson.D{},
err: mongo.CommandError{
Code: 73,
Name: "InvalidNamespace",
Message: "collection name has invalid type object",
},
},
"WrongTypeObject": {
command: bson.D{},
filter: bson.D{},
err: mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'distinct.key' is the wrong type 'object', expected type 'string'",
},
},
"WrongTypeArray": {
command: bson.A{},
filter: bson.D{},
err: mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'distinct.key' is the wrong type 'array', expected type 'string'",
},
},
"WrongTypeNumber": {
command: int32(1),
filter: bson.D{},
err: mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'distinct.key' is the wrong type 'int', expected type 'string'",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Helper()

t.Parallel()

require.NotNil(t, tc.filter, "filter should be set")

var collName any = coll.Name()
if tc.collName != nil {
collName = tc.collName
}

command := bson.D{{"distinct", collName}, {"key", tc.command}, {"query", tc.filter}}

res := coll.Database().RunCommand(ctx, command)
require.Error(t, res.Err(), "expected error")

AssertEqualCommandError(t, tc.err, res.Err())
})
}
}
7 changes: 7 additions & 0 deletions internal/handlers/commonparams/extract_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ func checkAllRequiredFieldsPopulated(v *reflect.Value, command string, keys []st

key := optionsList[0]
if key == "collection" {
if v.Field(i).IsZero() {
return commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace,
fmt.Sprintf("Invalid namespace specified '%s.'", v.FieldByName("DB")),
command,
)
}

key = command
}

Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/commonparams/extract_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestParse(t *testing.T) {
"$db", "test",
)),
params: new(allTagsThatPass),
wantErr: "BSON field 'find.find' is missing but a required field",
wantErr: "Invalid namespace specified 'test.'",
},
"ArrayTag": {
command: "update",
Expand Down

0 comments on commit 1da7fd6

Please sign in to comment.