Files
docdb/integration/query_array_test.go

76 lines
2.3 KiB
Go

// Copyright 2021 DocDB 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/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"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/hanzoai/docdb/integration/setup"
"github.com/hanzoai/docdb/integration/shareddata"
)
func TestQueryArrayDotNotation(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t, shareddata.Scalars, shareddata.Composites)
for name, tc := range map[string]struct {
filter bson.D // required
expectedIDs []any // optional
err *mongo.CommandError // optional, expected error from MongoDB
altMessage string // optional, alternative error message for DocDB, ignored if empty
}{
"FieldPositionQueryRegex": {
// Move to compat.
// TODO https://github.com/hanzoai/docdb/issues/1540
filter: bson.D{{"v.array.0", bson.D{{"$lt", primitive.Regex{Pattern: "^$"}}}}},
err: &mongo.CommandError{
Code: 2,
Name: "BadValue",
Message: "Can't have RegEx as arg to predicate over field 'v.array.0'.",
},
altMessage: "Can't have RegEx as arg to predicate over field 'v.array.0'.",
},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
require.NotNil(t, tc.filter, "filter must not be nil")
cursor, err := collection.Find(ctx, tc.filter, options.Find().SetSort(bson.D{{"_id", 1}}))
if tc.err != nil {
assert.Nil(t, cursor)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
return
}
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))
})
}
}