Skip to content

Commit

Permalink
Add documentation for values comparision (#559)
Browse files Browse the repository at this point in the history
Refs #457.
  • Loading branch information
AlekSi committed May 5, 2022
1 parent 1191d9b commit 900bac6
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 59 deletions.
14 changes: 14 additions & 0 deletions integration/query_projection_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// 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 (
Expand Down
133 changes: 132 additions & 1 deletion integration/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

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

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

Expand All @@ -37,6 +38,136 @@ func TestUnknownFilterOperator(t *testing.T) {
AssertEqualError(t, errExpected, err)
}

func TestQuerySort(t *testing.T) {
t.Skip("TODO https://github.com/FerretDB/FerretDB/issues/457")

t.Parallel()
ctx, collection := setup(t, shareddata.Scalars, shareddata.Composites)

for name, tc := range map[string]struct {
sort bson.D
expectedIDs []any
}{
"Asc": {
sort: bson.D{{"value", 1}, {"_id", 1}},
expectedIDs: []any{
"array-empty",
"array-embedded",
"array-null",
"array-three",
"array-three-reverse",
"null",
"double-nan",
"double-negative-infinity",
"int64-min",
"int32-min",
"double-negative-zero",
"double-zero",
"int32-zero",
"int64-zero",
"double-smallest",
"array",
"double-whole",
"int32",
"int64",
"double",
"int32-max",
"int64-max",
"double-max",
"double-positive-infinity",
"string-empty",
"string-whole",
"string-double",
"string",
"document-empty",
"document-null",
"document",
"document-composite",
"document-composite-reverse",
"binary-empty",
"binary",
"objectid-empty",
"objectid",
"bool-false",
"bool-true",
"datetime-year-min",
"datetime-epoch",
"datetime",
"datetime-year-max",
"timestamp-i",
"timestamp",
"regex-empty",
"regex",
},
},
"Desc": {
sort: bson.D{{"value", -1}, {"_id", 1}},
expectedIDs: []any{
"regex",
"regex-empty",
"timestamp",
"timestamp-i",
"datetime-year-max",
"datetime",
"datetime-epoch",
"datetime-year-min",
"bool-true",
"bool-false",
"objectid",
"objectid-empty",
"binary",
"binary-empty",
"array-embedded",
"document-composite-reverse",
"document-composite",
"document",
"document-null",
"document-empty",
"array-three",
"array-three-reverse",
"string",
"string-double",
"string-whole",
"string-empty",
"double-positive-infinity",
"double-max",
"int64-max",
"int32-max",
"double",
"array",
"double-whole",
"int32",
"int64",
"double-smallest",
"double-negative-zero",
"double-zero",
"int32-zero",
"int64-zero",
"int32-min",
"int64-min",
"double-negative-infinity",
"double-nan",
"array-null",
"null",
"array-empty",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

cursor, err := collection.Find(ctx, bson.D{}, options.Find().SetSort(tc.sort))
require.NoError(t, err)

var actual []bson.D
err = cursor.All(ctx, &actual)
require.NoError(t, err)
assert.Equal(t, tc.expectedIDs, CollectIDs(t, actual))
})
}
}

func TestQueryCount(t *testing.T) {
t.Parallel()
ctx, collection := setup(t, shareddata.Scalars, shareddata.Composites)
Expand Down
2 changes: 1 addition & 1 deletion integration/shareddata/bignumbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package shareddata

// BigNumbersData contains big numbers of different types for tests.
//
// This shared data set is not frozen yet, but please add to it only if it is really shared.
// TODO Merge into Scalars. https://github.com/FerretDB/FerretDB/issues/558
var BigNumbersData = &Values[string]{
data: map[string]any{
"int64-big": int64(2 << 61),
Expand Down
6 changes: 6 additions & 0 deletions integration/shareddata/composites.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ var Composites = &Values[string]{
"array-three": bson.A{int32(42), "foo", nil},
"array-embedded": bson.A{bson.A{int32(42), "foo"}, nil},
"array-empty": bson.A{},

// TODO https://github.com/FerretDB/FerretDB/issues/457
// "document-null": bson.D{{"foo", nil}},
// "document-composite-reverse": bson.D{{"array", bson.A{int32(42), "foo", nil}}, {"42", "foo"}, {"foo", int32(42)}},
// "array-three-reverse": bson.A{nil, "foo", int32(42)},
// "array-null": bson.A{nil},
},
}
4 changes: 4 additions & 0 deletions internal/handlers/common/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
)

// matchDocuments returns true if 2 documents are equal.
//
// TODO move into types.Compare.
func matchDocuments(a, b *types.Document) bool {
if a == nil {
log.Panicf("%v is nil", a)
Expand All @@ -43,6 +45,8 @@ func matchDocuments(a, b *types.Document) bool {

// matchArrays returns true if a filter array equals exactly the specified array or
// array contains an element that equals the array.
//
// TODO move into types.Compare.
func matchArrays(filterArr, docArr *types.Array) bool {
if filterArr == nil {
log.Panicf("%v is nil", filterArr)
Expand Down
Loading

0 comments on commit 900bac6

Please sign in to comment.