@@ -4609,4 +4609,58 @@ describe('Cursor', function() {
4609
4609
} ) ;
4610
4610
} ) ;
4611
4611
} ) ;
4612
+
4613
+ describe ( 'transforms' , function ( ) {
4614
+ it ( 'should correctly apply map transform to cursor as readable stream' , function ( done ) {
4615
+ const configuration = this . configuration ;
4616
+ const client = configuration . newClient ( ) ;
4617
+ client . connect ( err => {
4618
+ expect ( err ) . to . not . exist ;
4619
+ this . defer ( ( ) => client . close ( ) ) ;
4620
+
4621
+ const docs = 'Aaden Aaron Adrian Aditya Bob Joe' . split ( ' ' ) . map ( x => ( { name : x } ) ) ;
4622
+ const coll = client . db ( configuration . db ) . collection ( 'cursor_stream_mapping' ) ;
4623
+ coll . insertMany ( docs , err => {
4624
+ expect ( err ) . to . not . exist ;
4625
+
4626
+ const bag = [ ] ;
4627
+ const stream = coll
4628
+ . find ( )
4629
+ . project ( { _id : 0 , name : 1 } )
4630
+ . map ( doc => ( { mapped : doc } ) )
4631
+ . on ( 'data' , doc => bag . push ( doc ) ) ;
4632
+
4633
+ stream . on ( 'error' , done ) . on ( 'end' , ( ) => {
4634
+ expect ( bag . map ( x => x . mapped ) ) . to . eql ( docs . map ( x => ( { name : x . name } ) ) ) ;
4635
+ done ( ) ;
4636
+ } ) ;
4637
+ } ) ;
4638
+ } ) ;
4639
+ } ) ;
4640
+
4641
+ it ( 'should correctly apply map transform when converting cursor to array' , function ( done ) {
4642
+ const configuration = this . configuration ;
4643
+ const client = configuration . newClient ( ) ;
4644
+ client . connect ( err => {
4645
+ expect ( err ) . to . not . exist ;
4646
+ this . defer ( ( ) => client . close ( ) ) ;
4647
+
4648
+ const docs = 'Aaden Aaron Adrian Aditya Bob Joe' . split ( ' ' ) . map ( x => ( { name : x } ) ) ;
4649
+ const coll = client . db ( configuration . db ) . collection ( 'cursor_toArray_mapping' ) ;
4650
+ coll . insertMany ( docs , err => {
4651
+ expect ( err ) . to . not . exist ;
4652
+
4653
+ coll
4654
+ . find ( )
4655
+ . project ( { _id : 0 , name : 1 } )
4656
+ . map ( doc => ( { mapped : doc } ) )
4657
+ . toArray ( ( err , mappedDocs ) => {
4658
+ expect ( err ) . to . not . exist ;
4659
+ expect ( mappedDocs . map ( x => x . mapped ) ) . to . eql ( docs . map ( x => ( { name : x . name } ) ) ) ;
4660
+ done ( ) ;
4661
+ } ) ;
4662
+ } ) ;
4663
+ } ) ;
4664
+ } ) ;
4665
+ } ) ;
4612
4666
} ) ;
0 commit comments