Skip to content

Commit e4bd39d

Browse files
committed
Fix command cursors so that they are able to accept commands
GODRIVER-1114 Change-Id: I9ccced4ccc5fac5ed972954d5249e4b3b5d37c8e
1 parent 24c422e commit e4bd39d

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

mongo/database.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ func (db *Database) Aggregate(ctx context.Context, pipeline interface{},
118118

119119
func (db *Database) processRunCommand(ctx context.Context, cmd interface{},
120120
opts ...*options.RunCmdOptions) (*operation.Command, *session.Client, error) {
121-
122121
sess := sessionFromContext(ctx)
123122
if sess == nil && db.client.topology.SessionPool != nil {
124123
var err error
@@ -187,8 +186,14 @@ func (db *Database) RunCommandCursor(ctx context.Context, runCommand interface{}
187186
op, sess, err := db.processRunCommand(ctx, runCommand, opts...)
188187
if err != nil {
189188
closeImplicitSession(sess)
190-
return nil, err
189+
return nil, replaceErrors(err)
190+
}
191+
192+
if err = op.Execute(ctx); err != nil {
193+
closeImplicitSession(sess)
194+
return nil, replaceErrors(err)
191195
}
196+
192197
bc, err := op.ResultCursor(driver.CursorOptions{})
193198
if err != nil {
194199
closeImplicitSession(sess)

mongo/database_internal_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,64 @@ func TestDatabase_ListCollections(t *testing.T) {
429429
})
430430
}
431431
}
432+
433+
func TestDatabase_RunCommandCursor(t *testing.T) {
434+
var elms []interface{}
435+
for i := 0; i < 5; i++ {
436+
elms = append(elms, bson.D{
437+
{"x", i},
438+
})
439+
}
440+
441+
tests := []struct {
442+
name string
443+
ctx context.Context
444+
runCommand interface{}
445+
readPref *readpref.ReadPref
446+
toInsert []interface{}
447+
expectedErr error
448+
minVersion string
449+
}{
450+
{"Success", nil, bson.D{
451+
{"find", "bar"},
452+
}, nil, elms, nil, "3.2"},
453+
{"Success", nil, bson.D{
454+
{"aggregate", "bar"},
455+
{"pipeline", bson.A{}},
456+
{"cursor", bson.D{}},
457+
}, nil, elms, nil, "2.6"},
458+
{"Failure", nil, bson.D{
459+
{"ping", 1},
460+
}, nil, elms, errors.New("cursor should be an embedded document but is of BSON type invalid"), "2.6"},
461+
}
462+
463+
for _, test := range tests {
464+
t.Run(test.name, func(tt *testing.T) {
465+
serverVersion, err := getServerVersion(createTestDatabase(t, nil))
466+
require.NoError(t, err)
467+
if compareVersions(t, serverVersion, test.minVersion) < 0 {
468+
tt.Skip()
469+
}
470+
471+
foo := "foo"
472+
bar := "bar"
473+
coll := createTestCollection(t, &foo, &bar, options.Collection().SetWriteConcern(wcMajority).SetReadPreference(test.readPref))
474+
defer func() {
475+
_ = coll.Drop(ctx)
476+
}()
477+
478+
res, err := coll.InsertMany(test.ctx, test.toInsert)
479+
require.NoError(t, err, "error inserting into database")
480+
481+
cursor, err := coll.Database().RunCommandCursor(test.ctx, test.runCommand)
482+
require.Equal(tt, test.expectedErr, err, "db.RunCommandCursor returned different error than expected")
483+
if cursor != nil {
484+
var count int
485+
for cursor.Next(test.ctx) {
486+
count++
487+
}
488+
require.Equal(t, len(res.InsertedIDs), count, "doc count mismatch; expected %d, got %d", len(res.InsertedIDs), count)
489+
}
490+
})
491+
}
492+
}

0 commit comments

Comments
 (0)