Skip to content

Commit

Permalink
Support querying documents (#573)
Browse files Browse the repository at this point in the history
Closes #72.
  • Loading branch information
Dmitry authored May 10, 2022
1 parent 7cfd4aa commit b7cc283
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
4 changes: 4 additions & 0 deletions integration/query_comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func TestQueryComparisonImplicit(t *testing.T) {
filter: bson.D{{"value", math.SmallestNonzeroFloat64}},
expectedIDs: []any{"double-smallest"},
},
"DoubleNaN": {
filter: bson.D{{"value", math.NaN()}},
expectedIDs: []any{"double-nan"},
},

"String": {
filter: bson.D{{"value", "foo"}},
Expand Down
46 changes: 46 additions & 0 deletions integration/query_logical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ func TestQueryLogicalAnd(t *testing.T) {
},
expectedIDs: []any{},
},
"AndAnd": {
filter: bson.A{
bson.D{{"$and", bson.A{
bson.D{{"value", bson.D{{"$gt", int32(0)}}}},
bson.D{{"value", bson.D{{"$lte", int32(42)}}}},
}}},
bson.D{{"value", bson.D{{"$type", "int"}}}},
},
expectedIDs: []any{"int32"},
},
"AndAndAnd": {
filter: bson.A{
bson.D{{"$and", bson.A{
bson.D{{"value", bson.D{{"$gt", int32(0)}}}},
bson.D{{"$and", bson.A{
bson.D{{"value", bson.D{{"$lt", int32(43)}}}},
bson.D{{"value", bson.D{{"$eq", int32(42)}}}},
}}},
}}},
bson.D{{"value", bson.D{{"$type", "int"}}}},
},
expectedIDs: []any{"int32"},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -114,6 +137,29 @@ func TestQueryLogicalOr(t *testing.T) {
"int32-min", "int32-zero", "int64-min", "int64-zero",
},
},
"OrAnd": {
filter: bson.A{
bson.D{{"$and", bson.A{
bson.D{},
bson.D{},
}}},
bson.D{},
},
expectedIDs: []any{
"binary", "binary-empty",
"bool-false", "bool-true",
"datetime", "datetime-epoch", "datetime-year-max", "datetime-year-min",
"double", "double-max", "double-nan", "double-negative-infinity", "double-negative-zero",
"double-positive-infinity", "double-smallest", "double-whole", "double-zero",
"int32", "int32-max", "int32-min", "int32-zero",
"int64", "int64-max", "int64-min", "int64-zero",
"null",
"objectid", "objectid-empty",
"regex", "regex-empty",
"string", "string-double", "string-empty", "string-whole",
"timestamp", "timestamp-i",
},
},
"BadInput": {
filter: nil,
err: &mongo.CommandError{
Expand Down
109 changes: 109 additions & 0 deletions integration/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,112 @@ func TestQueryBadSortType(t *testing.T) {
})
}
}

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

_, err := collection.InsertMany(ctx, []any{
bson.D{
{"_id", "document-two-fields"},
{"foo", "bar"},
{"baz", int32(42)},
},
bson.D{
{"_id", "document-value-two-fields"},
{"value", bson.D{{"foo", "bar"}, {"baz", int32(42)}}},
},
})
require.NoError(t, err)

for name, tc := range map[string]struct {
filter bson.D
expectedIDs []any
}{
"Document": {
filter: bson.D{{"foo", "bar"}, {"baz", int32(42)}},
expectedIDs: []any{"document-two-fields"},
},
"DocumentChangedFieldsOrder": {
filter: bson.D{{"baz", int32(42)}, {"foo", "bar"}},
expectedIDs: []any{"document-two-fields"},
},
"DocumentValueFields": {
filter: bson.D{{"value", bson.D{{"foo", "bar"}, {"baz", int32(42)}}}},
expectedIDs: []any{"document-value-two-fields"},
},

"Array": {
filter: bson.D{{"value", bson.A{int32(42), "foo", nil}}},
expectedIDs: []any{"array-three"},
},
"ArrayChangedOrder": {
filter: bson.D{{"value", bson.A{int32(42), nil, "foo"}}},
expectedIDs: []any{},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

cursor, err := collection.Find(ctx, tc.filter, options.Find().SetSort(bson.D{{"_id", 1}}))
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 TestDotNotation(t *testing.T) {
t.Parallel()
providers := []shareddata.Provider{shareddata.Scalars, shareddata.Composites}
ctx, collection := setup(t, providers...)

_, err := collection.InsertMany(ctx, []any{
bson.D{
{"_id", "document-deeply-nested"},
{
"foo",
bson.D{{
"bar",
bson.D{{
"baz",
bson.D{{"qux", bson.D{{"quz", int32(42)}}}},
}},
}},
},
},
})
require.NoError(t, err)

for name, tc := range map[string]struct {
filter bson.D
expectedIDs []any
}{
"DocumentDeepNested": {
filter: bson.D{{"foo.bar.baz.qux.quz", int32(42)}},
expectedIDs: []any{"document-deeply-nested"},
},
"Document": {
filter: bson.D{{"foo.bar.baz", bson.D{{"qux.quz", int32(42)}}}},
expectedIDs: []any{},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

cursor, err := collection.Find(ctx, tc.filter, options.Find().SetSort(bson.D{{"_id", 1}}))
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))
})
}
}

0 comments on commit b7cc283

Please sign in to comment.