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 for sort and find parameters type #544

Merged
merged 12 commits into from
May 2, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 214 additions & 2 deletions integration/query_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
// 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"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"

"github.com/FerretDB/FerretDB/integration/shareddata"
Expand All @@ -23,8 +39,7 @@ func TestUnknownFilterOperator(t *testing.T) {

func TestQueryCount(t *testing.T) {
t.Parallel()
providers := []shareddata.Provider{shareddata.Scalars, shareddata.Composites}
ctx, collection := setup(t, providers...)
ctx, collection := setup(t, shareddata.Scalars, shareddata.Composites)

for name, tc := range map[string]struct {
command any
Expand Down Expand Up @@ -67,3 +82,200 @@ func TestQueryCount(t *testing.T) {
})
}
}

func TestQueryBadFindType(t *testing.T) {
t.Parallel()
ctx, collection := setup(t, shareddata.Scalars, shareddata.Composites)

for name, tc := range map[string]struct {
command bson.D
err *mongo.CommandError
}{
"Document": {
command: bson.D{
{"find", bson.D{}},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type object",
},
},
"Array": {
command: bson.D{
{"find", primitive.A{}},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type array",
},
},
"Double": {
command: bson.D{
{"find", 3.14},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type double",
},
},
"DoubleWhole": {
command: bson.D{
{"find", 42.0},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type double",
},
},
"Binary": {
command: bson.D{
{"find", primitive.Binary{}},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type binData",
},
},
"ObjectID": {
command: bson.D{
{"find", primitive.ObjectID{}},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type objectId",
},
},
"Bool": {
command: bson.D{
{"find", true},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type bool",
},
},
"Date": {
command: bson.D{
{"find", time.Now()},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type date",
},
},
"Null": {
command: bson.D{
{"find", nil},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type null",
},
},
"Regex": {
command: bson.D{
{"find", primitive.Regex{Pattern: "/foo/"}},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type regex",
},
},
"Int": {
command: bson.D{
{"find", int32(42)},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type int",
},
},
"Timestamp": {
command: bson.D{
{"find", primitive.Timestamp{}},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type timestamp",
},
},
"Long": {
command: bson.D{
{"find", int64(42)},
{"projection", bson.D{{"value", "some"}}},
},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "collection name has invalid type long",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

var actual bson.D
err := collection.Database().RunCommand(ctx, tc.command).Decode(&actual)
require.Error(t, err)
AssertEqualError(t, *tc.err, err)
})
}
}

func TestQueryBadSortType(t *testing.T) {
t.Parallel()
ctx, collection := setup(t, shareddata.Scalars, shareddata.Composites)

for name, tc := range map[string]struct {
command bson.D
err *mongo.CommandError
}{
"BadSortType": {
command: bson.D{
{"find", collection.Name()},
{"projection", bson.D{{"value", "some"}}},
{"sort", "123"},
},
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "Expected field sortto be of type object",
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

var actual bson.D
err := collection.Database().RunCommand(ctx, tc.command).Decode(&actual)
require.Error(t, err)
AssertEqualError(t, *tc.err, err)
})
}
}
1 change: 1 addition & 0 deletions internal/handlers/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (

ErrBadValue = ErrorCode(2) // BadValue
ErrFailedToParse = ErrorCode(9) // FailedToParse
ErrTypeMismatch = ErrorCode(14) // TypeMismatch
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
ErrNamespaceNotFound = ErrorCode(26) // NamespaceNotFound
ErrNamespaceExists = ErrorCode(48) // NamespaceExists
ErrCommandNotFound = ErrorCode(59) // CommandNotFound
Expand Down
34 changes: 19 additions & 15 deletions internal/handlers/common/errorcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading