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 handler tests #2769

Merged
merged 14 commits into from
Jun 5, 2023
Merged
38 changes: 38 additions & 0 deletions integration/commands_administration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,44 @@ func TestCommandsAdministrationDataSize(t *testing.T) {
})
}

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

ctx, collection := setup.Setup(t, shareddata.DocumentsStrings)

for name, tc := range map[string]struct { //nolint:vet // for readability
command bson.D
err *mongo.CommandError
}{
"InvalidNamespace": {
command: bson.D{{"dataSize", "invalid"}},
err: &mongo.CommandError{
Code: 73,
Name: "InvalidNamespace",
Message: "Invalid namespace specified 'invalid'",
},
},
"InvalidNamespaceTypeDocument": {
command: bson.D{{"dataSize", bson.D{}}},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type object",
},
},
} {
name, tc := name, tc

t.Run(name, func(t *testing.T) {
t.Parallel()

res := collection.Database().RunCommand(ctx, tc.command)

AssertEqualCommandError(t, *tc.err, res.Err())
})
}
}

func TestCommandsAdministrationDBStats(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t, shareddata.DocumentsStrings)
Expand Down
39 changes: 39 additions & 0 deletions integration/commands_diagnostic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,45 @@ func TestCommandsDiagnosticValidate(t *testing.T) {
testutil.AssertEqual(t, expected, actual)
}

func TestCommandsDiagnosticValidateError(t *testing.T) {
t.Skip("https://github.com/FerretDB/FerretDB/issues/2704")

t.Parallel()

for name, tc := range map[string]struct { //nolint:vet // for readability
command bson.D
err *mongo.CommandError
}{
"InvalidTypeDocument": {
command: bson.D{{"validate", bson.D{}}},
err: &mongo.CommandError{
Code: 73,
Name: "InvalidNamespace",
Message: "collection name has invalid type object",
},
},
"NonExistentCollection": {
command: bson.D{{"validate", "nonExistentCollection"}},
err: &mongo.CommandError{
Code: 26,
Name: "NamespaceNotFound",
Message: "Collection 'TestCommandsDiagnosticValidateError-NonExistentCollection.nonExistentCollection' does not exist to validate.",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

ctx, collection := setup.Setup(t, shareddata.Doubles)

res := collection.Database().RunCommand(ctx, tc.command)

AssertEqualCommandError(t, *tc.err, res.Err())
})
}
}

func TestCommandsDiagnosticWhatsMyURI(t *testing.T) {
t.Skip("https://github.com/FerretDB/FerretDB/issues/1759")

Expand Down
36 changes: 35 additions & 1 deletion integration/commands_freemonitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func TestCommandsFreeMonitoringSetFreeMonitoring(t *testing.T) {
err *mongo.CommandError
expectedRes bson.D
expectedStatus string

skip string
}{
"Enable": {
command: bson.D{{"setFreeMonitoring", 1}, {"action", "enable"}},
Expand Down Expand Up @@ -88,17 +90,49 @@ func TestCommandsFreeMonitoringSetFreeMonitoring(t *testing.T) {
Message: `Enumeration value '' for field 'setFreeMonitoring.action' is not a valid value.`,
},
},
"DocumentCommand": {
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
command: bson.D{{"setFreeMonitoring", bson.D{}}, {"action", "enable"}},
expectedRes: bson.D{{"ok", float64(1)}},
expectedStatus: "enabled",
},
"NilCommand": {
command: bson.D{{"setFreeMonitoring", nil}, {"action", "enable"}},
expectedRes: bson.D{{"ok", float64(1)}},
expectedStatus: "enabled",
},
"ActionMissing": {
command: bson.D{{"setFreeMonitoring", nil}},
err: &mongo.CommandError{
Code: 40414,
Name: "Location40414",
Message: `BSON field 'setFreeMonitoring.action' is missing but a required field`,
},
skip: "https://github.com/FerretDB/FerretDB/issues/2704",
},
"ActionTypeDocument": {
command: bson.D{{"setFreeMonitoring", 1}, {"action", bson.D{}}},
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "BSON field 'setFreeMonitoring.action' is the wrong type 'object', expected type 'string'",
},
skip: "https://github.com/FerretDB/FerretDB/issues/2704",
},
} {
name, tc := name, tc

t.Run(name, func(t *testing.T) {
// these tests shouldn't be run in parallel, because they work on the same database

if tc.skip != "" {
t.Skip(tc.skip)
}

var actual bson.D
err := s.Collection.Database().RunCommand(s.Ctx, tc.command).Decode(&actual)

if tc.err != nil {
AssertEqualError(t, *tc.err, err)
AssertEqualCommandError(t, *tc.err, err)
return
}

Expand Down
4 changes: 4 additions & 0 deletions integration/explain_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func TestExplainCompatError(t *testing.T) {
command: "find",
filter: bson.D{{"v", int32(42)}},
},
"InvalidCommandGetLog": {
command: "create",
skip: "https://github.com/FerretDB/FerretDB/issues/2704",
},
}

testExplainCompatError(t, testCases)
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/common/setfreemonitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// SetFreeMonitoring is a part of common implementation of the setFreeMonitoring command.
func SetFreeMonitoring(ctx context.Context, msg *wire.OpMsg, provider *state.Provider) (*wire.OpMsg, error) {
func SetFreeMonitoring(_ context.Context, msg *wire.OpMsg, provider *state.Provider) (*wire.OpMsg, error) {
if provider == nil {
panic("provider cannot be equal to nil")
}
Expand Down
32 changes: 23 additions & 9 deletions internal/handlers/pg/msg_datasize.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ package pg

import (
"context"
"strings"
"fmt"
"time"

"github.com/jackc/pgx/v5"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/handlers/commonparams"
"github.com/FerretDB/FerretDB/internal/handlers/pg/pgdb"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
Expand All @@ -47,17 +49,29 @@ func (h *Handler) MsgDataSize(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg

common.Ignored(document, h.L, "estimate")

m := document.Map()
target, ok := m["dataSize"].(string)
if !ok {
return nil, lazyerrors.New("no target collection")
var namespaceParam any

if namespaceParam, err = document.Get(document.Command()); err != nil {
return nil, err
}
targets := strings.Split(target, ".")
if len(targets) != 2 {
return nil, lazyerrors.New("target collection must be like: 'database.collection'")

namespace, ok := namespaceParam.(string)
if !ok {
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrBadValue,
fmt.Sprintf("collection name has invalid type %s", commonparams.AliasFromType(namespaceParam)),
document.Command(),
)
}

db, collection := targets[0], targets[1]
db, collection, err := splitNamespace(namespace)
if err != nil {
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrInvalidNamespace,
fmt.Sprintf("Invalid namespace specified '%s'", namespace),
"dataSize",
)
}

var stats *pgdb.CollStats

Expand Down