Files
docdb/integration/query_projection_test.go
zeekayandHanzo Dev 06d5eca873 Declare operations on the group that serves them
zip's registration is a verb method on the group, so an operation reads as the
group answering it rather than a function taking the group, and a route that is
not a typed operation says which method it answers with. The package-level form
and the Router interface it existed to accept any target through are both gone.

The toolchain and the pin move together, deliberately. Below zip v1.36.50 the
JSON encoder was selected on a build tag, so a repo whose go directive rises
first writes a wire that is neither the old release's nor the new one's until the
pin catches up.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 06:11:42 -07:00

305 lines
11 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/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/hanzoai/docdb/integration/setup"
"github.com/hanzoai/docdb/integration/shareddata"
)
func TestQueryProjectionErrors(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t, shareddata.Scalars, shareddata.Composites)
for name, tc := range map[string]struct { //nolint:vet // used for testing only
filter bson.D // required
projection any // required
err *mongo.CommandError // required, expected error from MongoDB
altMessage string // optional, alternative error message for DocDB, ignored if empty
failsForDocDB string
}{
"EmptyKey": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"", true}},
err: &mongo.CommandError{
Code: 40352,
Name: "Location40352",
Message: "FieldPath cannot be constructed with empty string",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"EmptyPath": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"v..d", true}},
err: &mongo.CommandError{
Code: 15998,
Name: "Location15998",
Message: "FieldPath field names may not be empty strings.",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"ExcludeInclude": {
filter: bson.D{},
projection: bson.D{{"foo", false}, {"bar", true}},
err: &mongo.CommandError{
Code: 31253,
Name: "Location31253",
Message: "Cannot do inclusion on field bar in exclusion projection",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"IncludeExclude": {
filter: bson.D{},
projection: bson.D{{"foo", true}, {"bar", false}},
err: &mongo.CommandError{
Code: 31254,
Name: "Location31254",
Message: "Cannot do exclusion on field bar in inclusion projection",
},
altMessage: "Cannot do exclusion on field bar in inclusion projection.",
},
"PositionalOperatorMultiple": {
filter: bson.D{{"_id", "array-numbers-asc"}},
projection: bson.D{{"v.$.foo.$", true}},
err: &mongo.CommandError{
Code: 31394,
Name: "Location31394",
Message: "As of 4.4, it's illegal to specify positional operator " +
"in the middle of a path.Positional projection may only be " +
"used at the end, for example: a.b.$. If the query previously " +
"used a form like a.b.$.d, remove the parts following the '$' and " +
"the results will be equivalent.",
},
altMessage: "Positional projection may only be used at the end, " +
"for example: a.b.$. If the query previously used a form " +
"like a.b.$.d, remove the parts following the '$' and " +
"the results will be equivalent.",
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorMiddle": {
filter: bson.D{{"_id", "array-numbers-asc"}},
projection: bson.D{{"v.$.foo", true}},
err: &mongo.CommandError{
Code: 31394,
Name: "Location31394",
Message: "As of 4.4, it's illegal to specify positional operator " +
"in the middle of a path.Positional projection may only be " +
"used at the end, for example: a.b.$. If the query previously " +
"used a form like a.b.$.d, remove the parts following the '$' and " +
"the results will be equivalent.",
},
altMessage: "Positional projection may only be used at the end, " +
"for example: a.b.$. If the query previously used a form " +
"like a.b.$.d, remove the parts following the '$' and " +
"the results will be equivalent.",
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorWrongLocations": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"$.v.$.foo", true}},
err: &mongo.CommandError{
Code: 31394,
Name: "Location31394",
Message: "As of 4.4, it's illegal to specify positional operator " +
"in the middle of a path.Positional projection may only be " +
"used at the end, for example: a.b.$. If the query previously " +
"used a form like a.b.$.d, remove the parts following the '$' and " +
"the results will be equivalent.",
},
altMessage: "Positional projection may only be " +
"used at the end, for example: a.b.$. If the query previously " +
"used a form like a.b.$.d, remove the parts following the '$' and " +
"the results will be equivalent.",
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorEmptyFilter": {
filter: bson.D{},
projection: bson.D{{"v.$", true}},
err: &mongo.CommandError{
Code: 51246,
Name: "Location51246",
Message: "Executor error during find command :: caused by :: " +
"positional operator '.$' couldn't find a matching element in the array",
},
},
"PositionalOperatorEmptyArrayID": {
filter: bson.D{{"_id", "array-empty"}},
projection: bson.D{{"v.$", true}},
err: &mongo.CommandError{
Code: 51246,
Name: "Location51246",
Message: "Executor error during find command :: caused by :: positional operator '.$' couldn't find a matching element in the array",
},
},
"PositionalOperatorFilterMissingPath": {
// it returns error only if collection contains a doc that matches the filter
// and the filter does not contain positional operator path,
// e.g. missing {v: <val>} in the filter.
filter: bson.D{{"_id", "array"}},
projection: bson.D{{"v.$", true}},
err: &mongo.CommandError{
Code: 51246,
Name: "Location51246",
Message: "Executor error during find command :: caused by :: positional operator '.$' couldn't find a matching element in the array",
},
},
"PositionalOperatorMismatch": {
// positional projection only handles one array at the suffix,
// path prefixes cannot contain array.
filter: bson.D{{"v", 42}},
projection: bson.D{{"v.foo.$", true}},
err: &mongo.CommandError{
Code: 51247,
Name: "Location51247",
Message: "Executor error during find command :: caused by :: positional operator '.$' element mismatch",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorEmptyPath": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"v..$", true}},
err: &mongo.CommandError{
Code: 40353,
Name: "Location40353",
Message: "FieldPath must not end with a '.'.",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorDollarKey": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"$", true}},
err: &mongo.CommandError{
Code: 16410,
Name: "Location16410",
Message: "FieldPath field names may not start with '$'. Consider using $getField or $setField.",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorDollarInKey": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"$v", true}},
err: &mongo.CommandError{
Code: 16410,
Name: "Location16410",
Message: "FieldPath field names may not start with '$'. Consider using $getField or $setField.",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorDollarPrefix": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"$.foo", true}},
err: &mongo.CommandError{
Code: 16410,
Name: "Location16410",
Message: "FieldPath field names may not start with '$'. Consider using $getField or $setField.",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorDotNotationDollarInKey": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"v.$foo", true}},
err: &mongo.CommandError{
Code: 16410,
Name: "Location16410",
Message: "FieldPath field names may not start with '$'. Consider using $getField or $setField.",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorPrefixSuffix": {
filter: bson.D{{"_id", "array-numbers-asc"}},
projection: bson.D{{"$.foo.$", true}},
err: &mongo.CommandError{
Code: 16410,
Name: "Location16410",
Message: "FieldPath field names may not start with '$'. Consider using $getField or $setField.",
},
failsForDocDB: "https://github.com/hanzoai/docdb-DocumentDB/issues/241",
},
"PositionalOperatorExclusion": {
filter: bson.D{{"v", 42}},
projection: bson.D{{"v.$", false}},
err: &mongo.CommandError{
Code: 31395,
Name: "Location31395",
Message: "positional projection cannot be used with exclusion",
},
},
} {
t.Run(name, func(tt *testing.T) {
tt.Parallel()
var t testing.TB = tt
if tc.failsForDocDB != "" {
t = setup.FailsForDocDB(tt, tc.failsForDocDB)
}
require.NotNil(t, tc.filter, "filter should be set")
require.NotNil(t, tc.projection, "projection should be set")
require.NotNil(t, tc.err, "err should be set")
res, err := collection.Find(ctx, tc.filter, options.Find().SetProjection(tc.projection))
assert.Nil(t, res)
AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
})
}
}
func TestQueryProjectionSuccess(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t, shareddata.Scalars)
for name, tc := range map[string]struct { //nolint:vet // used for testing only
filter bson.D // required
projection any // required
res []bson.D // required
}{
"QueryProjectionOfFieldV": {
filter: bson.D{{"_id", "int32"}},
projection: bson.D{{"v", true}, {"_id", false}},
res: []bson.D{
{{"v", int32(42)}},
},
},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
require.NotNil(t, tc.filter, "filter should be set")
require.NotNil(t, tc.projection, "projection should be set")
require.NotNil(t, tc.res, "res should be set")
cursor, err := collection.Find(ctx, tc.filter, options.Find().SetProjection(tc.projection))
require.NoError(t, err)
defer cursor.Close(ctx)
var res []bson.D
err = cursor.All(ctx, &res)
require.NoError(t, err)
assert.Equal(t, tc.res, res)
})
}
}