Skip to content

Commit 4a66187

Browse files
author
Divjot Arora
committed
Add the aggregateOption method to OptHint
GODRIVER-47 Change-Id: I29658086cc91ec3d6f93f7b7d41b7720b231cc57
1 parent aa7785b commit 4a66187

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

core/option/options.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,10 @@ func (opt OptHint) Option(d *bson.Document) error {
425425
return nil
426426
}
427427

428-
func (OptHint) countOption() {}
429-
func (OptHint) findOption() {}
430-
func (OptHint) findOneOption() {}
428+
func (OptHint) countOption() {}
429+
func (OptHint) findOption() {}
430+
func (OptHint) findOneOption() {}
431+
func (OptHint) aggregateOption() {}
431432

432433
// OptLimit is for internal use.
433434
type OptLimit int64

mongo/collection_internal_test.go

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,16 +1025,21 @@ func TestCollection_Aggregate(t *testing.T) {
10251025
}
10261026
}
10271027

1028-
func TestCollection_Aggregate_withOptions(t *testing.T) {
1029-
if testing.Short() {
1030-
t.Skip("skipping integration test in short mode")
1031-
}
1032-
1033-
t.Parallel()
1034-
1028+
func testAggregateWithOptions(t *testing.T, createIndex bool, option option.AggregateOptioner) error {
10351029
coll := createTestCollection(t, nil, nil)
10361030
initCollection(t, coll)
10371031

1032+
if createIndex {
1033+
indexView := coll.Indexes()
1034+
_, err := indexView.CreateOne(context.Background(), IndexModel{
1035+
Keys: bson.NewDocument(bson.EC.Int32("x", 1)),
1036+
})
1037+
1038+
if err != nil {
1039+
return err
1040+
}
1041+
}
1042+
10381043
pipeline := bson.NewArray(
10391044
bson.VC.DocumentFromElements(
10401045
bson.EC.SubDocumentFromElements(
@@ -1059,24 +1064,65 @@ func TestCollection_Aggregate_withOptions(t *testing.T) {
10591064
),
10601065
))
10611066

1062-
cursor, err := coll.Aggregate(context.Background(), pipeline, Opt.AllowDiskUse(true))
1063-
require.Nil(t, err)
1067+
cursor, err := coll.Aggregate(context.Background(), pipeline, option)
1068+
if err != nil {
1069+
return err
1070+
}
10641071

10651072
for i := 2; i < 5; i++ {
10661073
var doc = bson.NewDocument()
10671074
cursor.Next(context.Background())
10681075
err = cursor.Decode(doc)
1069-
require.NoError(t, err)
1076+
if err != nil {
1077+
return err
1078+
}
1079+
1080+
if doc.Len() != 1 {
1081+
return fmt.Errorf("got doc len %d, expected 1", doc.Len())
1082+
}
10701083

1071-
require.Equal(t, doc.Len(), 1)
10721084
num, err := doc.LookupErr("x")
1073-
require.NoError(t, err)
1085+
if err != nil {
1086+
return err
1087+
}
1088+
10741089
if num.Type() != bson.TypeInt32 {
1075-
t.Errorf("Incorrect type for x. Got %s, but wanted Int32", num.Type())
1076-
t.FailNow()
1090+
return fmt.Errorf("incorrect type for x. got %s, wanted Int32", num.Type())
10771091
}
1078-
require.Equal(t, int(num.Int32()), i)
1092+
1093+
if int(num.Int32()) != i {
1094+
return fmt.Errorf("unexpected value returned. got %d, expected %d", int(num.Int32()), i)
1095+
}
1096+
}
1097+
1098+
return nil
1099+
}
1100+
1101+
func TestCollection_Aggregate_IndexHint(t *testing.T) {
1102+
skipIfBelow36(t)
1103+
1104+
if testing.Short() {
1105+
t.Skip("skipping integration test in short mode")
1106+
}
1107+
1108+
t.Parallel()
1109+
1110+
hint, err := Opt.Hint(bson.NewDocument(bson.EC.Int32("x", 1)))
1111+
require.NoError(t, err)
1112+
1113+
err = testAggregateWithOptions(t, true, hint)
1114+
require.NoError(t, err)
1115+
}
1116+
1117+
func TestCollection_Aggregate_withOptions(t *testing.T) {
1118+
if testing.Short() {
1119+
t.Skip("skipping integration test in short mode")
10791120
}
1121+
1122+
t.Parallel()
1123+
1124+
err := testAggregateWithOptions(t, false, Opt.AllowDiskUse(true))
1125+
require.NoError(t, err)
10801126
}
10811127

10821128
func TestCollection_Count(t *testing.T) {

0 commit comments

Comments
 (0)