@@ -429,3 +429,64 @@ func TestDatabase_ListCollections(t *testing.T) {
429
429
})
430
430
}
431
431
}
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