@@ -4423,4 +4423,82 @@ describe('Cursor', function() {
44234423 } ) ;
44244424 } ) ;
44254425 } ) ;
4426+
4427+ function testTransformStream ( config , done ) {
4428+ const client = config . client ;
4429+ const configuration = config . configuration ;
4430+ const collectionName = config . collectionName ;
4431+ const transformFunc = config . transformFunc ;
4432+ const expectedSet = config . expectedSet ;
4433+
4434+ client . connect ( function ( err , client ) {
4435+ const db = client . db ( configuration . db ) ;
4436+ let collection , cursor ;
4437+ const docs = [
4438+ { _id : 0 , a : { b : 1 , c : 0 } } ,
4439+ { _id : 1 , a : { b : 1 , c : 0 } } ,
4440+ { _id : 2 , a : { b : 1 , c : 0 } }
4441+ ] ;
4442+ const resultSet = new Set ( ) ;
4443+ const transformParam = transformFunc != null ? { transform : transformFunc } : null ;
4444+ const close = e => cursor . close ( ( ) => client . close ( ( ) => done ( e ) ) ) ;
4445+
4446+ Promise . resolve ( )
4447+ . then ( ( ) => db . createCollection ( collectionName ) )
4448+ . then ( ( ) => ( collection = db . collection ( collectionName ) ) )
4449+ . then ( ( ) => collection . insertMany ( docs ) )
4450+ . then ( ( ) => collection . find ( ) )
4451+ . then ( _cursor => ( cursor = _cursor ) )
4452+ . then ( ( ) => cursor . transformStream ( transformParam ) )
4453+ . then ( stream => {
4454+ stream . on ( 'data' , function ( doc ) {
4455+ resultSet . add ( doc ) ;
4456+ } ) ;
4457+
4458+ stream . once ( 'end' , function ( ) {
4459+ expect ( resultSet ) . to . deep . equal ( expectedSet ) ;
4460+ close ( ) ;
4461+ } ) ;
4462+
4463+ stream . once ( 'error' , function ( e ) {
4464+ close ( e ) ;
4465+ } ) ;
4466+ } )
4467+ . catch ( e => close ( e ) ) ;
4468+ } ) ;
4469+ }
4470+
4471+ it ( 'transformStream should apply the supplied transformation function to each document in the stream' , function ( done ) {
4472+ const configuration = this . configuration ;
4473+ const client = configuration . newClient ( { w : 1 } , { poolSize : 1 , auto_reconnect : false } ) ;
4474+ const expectedDocs = [ { _id : 0 , b : 1 , c : 0 } , { _id : 1 , b : 1 , c : 0 } , { _id : 2 , b : 1 , c : 0 } ] ;
4475+ const config = {
4476+ client : client ,
4477+ configuration : configuration ,
4478+ collectionName : 'transformStream-test-transform' ,
4479+ transformFunc : doc => ( { _id : doc . _id , b : doc . a . b , c : doc . a . c } ) ,
4480+ expectedSet : new Set ( expectedDocs )
4481+ } ;
4482+
4483+ testTransformStream ( config , done ) ;
4484+ } ) ;
4485+
4486+ it ( 'transformStream should return a stream of unmodified docs if no transform function applied' , function ( done ) {
4487+ const configuration = this . configuration ;
4488+ const client = configuration . newClient ( { w : 1 } , { poolSize : 1 , auto_reconnect : false } ) ;
4489+ const expectedDocs = [
4490+ { _id : 0 , a : { b : 1 , c : 0 } } ,
4491+ { _id : 1 , a : { b : 1 , c : 0 } } ,
4492+ { _id : 2 , a : { b : 1 , c : 0 } }
4493+ ] ;
4494+ const config = {
4495+ client : client ,
4496+ configuration : configuration ,
4497+ collectionName : 'transformStream-test-notransform' ,
4498+ transformFunc : null ,
4499+ expectedSet : new Set ( expectedDocs )
4500+ } ;
4501+
4502+ testTransformStream ( config , done ) ;
4503+ } ) ;
44264504} ) ;
0 commit comments