@@ -5,7 +5,7 @@ const stream = require('stream');
55const { MongoClient} = require ( 'mongodb' ) ;
66const Collection = require ( '../lib' ) . Collection ;
77const MongoQuery = require ( '../lib' ) . MongoQuery ;
8- const { EJSON } = require ( 'bson' ) ;
8+ const { EJSON , ObjectId } = require ( 'bson' ) ;
99
1010const config = {
1111 databaseName : 'mongo-magic-tests' ,
@@ -28,7 +28,9 @@ describe('Collection', function () {
2828 const collections = await _db . collections ( ) ;
2929
3030 for ( const collection of collections ) {
31- await collection . deleteMany ( { } ) ;
31+ if ( ! collection . collectionName . startsWith ( 'system.' ) ) {
32+ await collection . deleteMany ( { } ) ;
33+ }
3234 }
3335 } ) ;
3436
@@ -143,6 +145,27 @@ describe('Collection', function () {
143145 const mQuery = new MongoQuery ( { limit : 1000 } ) ;
144146 collection . queryAsStream ( mQuery , { transform : ( x ) => EJSON . serialize ( x , { } ) } ) . pipe ( ws ) ;
145147 } ) ;
148+
149+ it ( 'should apply a transform on documents if one is provided' , function ( done ) {
150+ const collection = new Collection ( _db . collection ( 'testquery' ) ) ;
151+ const ws = new stream . Writable ( { objectMode : true } ) ;
152+ let writeCnt = 0 ;
153+
154+ ws . _write = function ( chunk , encoding , done ) {
155+ writeCnt ++ ;
156+ assert . strictEqual ( ObjectId . isValid ( chunk . _id . $oid ) , true , 'Document was not transformed' )
157+ return done ( ) ;
158+ } ;
159+
160+ ws . on ( 'finish' , function ( ) {
161+ assert . strictEqual ( writeCnt , 2 , 'Invalid writes' ) ;
162+ return done ( ) ;
163+ } ) ;
164+
165+ const mQuery = new MongoQuery ( { limit : 1000 } ) ;
166+ collection . queryAsStream ( mQuery , { } , { transform : ( x ) => EJSON . serialize ( x , { } ) } ) . pipe ( ws ) ;
167+ } )
168+
146169 } ) ;
147170
148171 describe ( 'Stats' , function ( ) {
0 commit comments