@@ -12,7 +12,6 @@ const { ReadPreference } = require('../../src/read_preference');
12
12
const { ServerType } = require ( '../../src/sdam/common' ) ;
13
13
const { formatSort } = require ( '../../src/sort' ) ;
14
14
const { FindCursor } = require ( '../../src/cursor/find_cursor' ) ;
15
- const kSession = Symbol ( 'session' ) ;
16
15
17
16
describe ( 'Cursor' , function ( ) {
18
17
before ( function ( ) {
@@ -109,6 +108,7 @@ describe('Cursor', function () {
109
108
}
110
109
} ) ;
111
110
111
+
112
112
it ( 'cursor should trigger getMore' , {
113
113
// Add a tag that our runner can trigger on
114
114
// in this case we are setting that node needs to be higher than 0.10.X to run
@@ -3785,7 +3785,7 @@ describe('Cursor', function () {
3785
3785
expect ( doc ) . to . exist ;
3786
3786
const clonedCursor = cursor . clone ( ) ;
3787
3787
expect ( clonedCursor . cursorOptions . session ) . to . not . exist ;
3788
- expect ( clonedCursor [ kSession ] ) . to . not . exist ;
3788
+ expect ( clonedCursor . session ) . to . not . exist ;
3789
3789
} )
3790
3790
. finally ( ( ) => {
3791
3791
return cursor . close ( ) ;
@@ -3805,7 +3805,7 @@ describe('Cursor', function () {
3805
3805
expect ( doc ) . to . exist ;
3806
3806
const clonedCursor = cursor . clone ( ) ;
3807
3807
expect ( clonedCursor . cursorOptions . session ) . to . not . exist ;
3808
- expect ( clonedCursor [ kSession ] ) . to . not . exist ;
3808
+ expect ( clonedCursor . session ) . to . not . exist ;
3809
3809
} )
3810
3810
. finally ( ( ) => {
3811
3811
return cursor . close ( ) ;
@@ -3914,66 +3914,6 @@ describe('Cursor', function () {
3914
3914
const expectedDocs = [
3915
3915
{ _id : 0 , b : 1 , c : 0 } ,
3916
3916
{ _id : 1 , b : 1 , c : 0 } ,
3917
- { _id : 2 , b : 1 , c : 0 }
3918
- ] ;
3919
- const config = {
3920
- client : client ,
3921
- configuration : configuration ,
3922
- collectionName : 'stream-test-transform' ,
3923
- transformFunc : doc => ( { _id : doc . _id , b : doc . a . b , c : doc . a . c } ) ,
3924
- expectedSet : new Set ( expectedDocs )
3925
- } ;
3926
-
3927
- testTransformStream ( config , done ) ;
3928
- } ) ;
3929
-
3930
- it ( 'stream should return a stream of unmodified docs if no transform function applied' , function ( done ) {
3931
- const configuration = this . configuration ;
3932
- const client = configuration . newClient ( { w : 1 } , { maxPoolSize : 1 } ) ;
3933
- const expectedDocs = [
3934
- { _id : 0 , a : { b : 1 , c : 0 } } ,
3935
- { _id : 1 , a : { b : 1 , c : 0 } } ,
3936
- { _id : 2 , a : { b : 1 , c : 0 } }
3937
- ] ;
3938
- const config = {
3939
- client : client ,
3940
- configuration : configuration ,
3941
- collectionName : 'transformStream-test-notransform' ,
3942
- transformFunc : null ,
3943
- expectedSet : new Set ( expectedDocs )
3944
- } ;
3945
-
3946
- testTransformStream ( config , done ) ;
3947
- } ) ;
3948
-
3949
- it . skip ( 'should apply parent read preference to count command' , function ( done ) {
3950
- // NOTE: this test is skipped because mongo orchestration does not test sharded clusters
3951
- // with secondaries. This behavior should be unit tested
3952
-
3953
- const configuration = this . configuration ;
3954
- const client = configuration . newClient (
3955
- { w : 1 , readPreference : ReadPreference . SECONDARY } ,
3956
- { maxPoolSize : 1 , connectWithNoPrimary : true }
3957
- ) ;
3958
-
3959
- client . connect ( ( err , client ) => {
3960
- expect ( err ) . to . not . exist ;
3961
- this . defer ( ( ) => client . close ( ) ) ;
3962
-
3963
- const db = client . db ( configuration . db ) ;
3964
- let collection , cursor , spy ;
3965
- const close = e => cursor . close ( ( ) => client . close ( ( ) => done ( e ) ) ) ;
3966
-
3967
- Promise . resolve ( )
3968
- . then ( ( ) => new Promise ( resolve => setTimeout ( ( ) => resolve ( ) , 500 ) ) )
3969
- . then ( ( ) => db . createCollection ( 'test_count_readPreference' ) )
3970
- . then ( ( ) => ( collection = db . collection ( 'test_count_readPreference' ) ) )
3971
- . then ( ( ) => collection . find ( ) )
3972
- . then ( _cursor => ( cursor = _cursor ) )
3973
- . then ( ( ) => ( spy = sinon . spy ( cursor . topology , 'command' ) ) )
3974
- . then ( ( ) => cursor . count ( ) )
3975
- . then ( ( ) =>
3976
- expect ( spy . firstCall . args [ 2 ] )
3977
3917
. to . have . nested . property ( 'readPreference.mode' )
3978
3918
. that . equals ( 'secondary' )
3979
3919
)
@@ -4025,6 +3965,53 @@ describe('Cursor', function () {
4025
3965
} ) ;
4026
3966
} ) ;
4027
3967
3968
+ describe ( '#stream' , function ( ) {
3969
+ context ( 'when the stream is closed' , function ( ) {
3970
+ it ( 'emits the close event once only' , function ( ) {
3971
+ const configuration = this . configuration ;
3972
+ const client = configuration . newClient ( { w : 1 } , { maxPoolSize : 1 } ) ;
3973
+
3974
+ return client . connect ( err => {
3975
+ expect ( err ) . to . not . exist ;
3976
+ this . defer ( ( ) => client . close ( ) ) ;
3977
+
3978
+ const collection = client . db ( ) . collection ( 'documents' ) ;
3979
+ collection . drop ( ( ) => {
3980
+ const docs = [ { a : 1 } , { a : 2 } , { a : 3 } ] ;
3981
+ collection . insertMany ( docs , err => {
3982
+ expect ( err ) . to . not . exist ;
3983
+
3984
+ const cursor = collection . find ( { } , { sort : { a : 1 } } ) ;
3985
+ cursor . hasNext ( ( err , hasNext ) => {
3986
+ expect ( err ) . to . not . exist ;
3987
+ expect ( hasNext ) . to . be . true ;
3988
+
3989
+ const collected = [ ] ;
3990
+ const stream = new Writable ( {
3991
+ objectMode : true ,
3992
+ write : ( chunk , encoding , next ) => {
3993
+ collected . push ( chunk ) ;
3994
+ next ( undefined , chunk ) ;
3995
+ }
3996
+ } ) ;
3997
+
3998
+ const cursorStream = cursor . stream ( ) ;
3999
+
4000
+ cursorStream . on ( 'end' , ( ) => {
4001
+ expect ( collected ) . to . have . length ( 3 ) ;
4002
+ expect ( collected ) . to . eql ( docs ) ;
4003
+ done ( ) ;
4004
+ } ) ;
4005
+
4006
+ cursorStream . pipe ( stream ) ;
4007
+ } ) ;
4008
+ } ) ;
4009
+ } ) ;
4010
+ } ) ;
4011
+ } ) ;
4012
+ } ) ;
4013
+ } ) ;
4014
+
4028
4015
describe ( 'transforms' , function ( ) {
4029
4016
it ( 'should correctly apply map transform to cursor as readable stream' , function ( done ) {
4030
4017
const configuration = this . configuration ;
@@ -4083,24 +4070,70 @@ describe('Cursor', function () {
4083
4070
context ( 'sort' , function ( ) {
4084
4071
const findSort = ( input , output ) =>
4085
4072
withMonitoredClient ( 'find' , function ( client , events , done ) {
4086
- const db = client . db ( 'test' ) ;
4087
- db . collection ( 'test_sort_dos' , ( err , collection ) => {
4073
+ } ) ;
4074
+
4075
+ describe ( 'transforms' , function ( ) {
4076
+ it ( 'should correctly apply map transform to cursor as readable stream' , function ( done ) {
4077
+ const configuration = this . configuration ;
4078
+ const client = configuration . newClient ( ) ;
4079
+ client . connect ( err => {
4080
+ expect ( err ) . to . not . exist ;
4081
+ this . defer ( ( ) => client . close ( ) ) ;
4082
+
4083
+ const docs = 'Aaden Aaron Adrian Aditya Bob Joe' . split ( ' ' ) . map ( x => ( { name : x } ) ) ;
4084
+ const coll = client . db ( configuration . db ) . collection ( 'cursor_stream_mapping' ) ;
4085
+ coll . insertMany ( docs , err => {
4088
4086
expect ( err ) . to . not . exist ;
4089
- const cursor = collection . find ( { } , { sort : input } ) ;
4090
- cursor . next ( err => {
4091
- expect ( err ) . to . not . exist ;
4092
- expect ( events [ 0 ] . command . sort ) . to . deep . equal ( output ) ;
4093
- cursor . close ( done ) ;
4087
+
4088
+ const bag = [ ] ;
4089
+ const stream = coll
4090
+ . find ( )
4091
+ . project ( { _id : 0 , name : 1 } )
4092
+ . map ( doc => ( { mapped : doc } ) )
4093
+ . stream ( )
4094
+ . on ( 'data' , doc => bag . push ( doc ) ) ;
4095
+
4096
+ stream . on ( 'error' , done ) . on ( 'end' , ( ) => {
4097
+ expect ( bag . map ( x => x . mapped ) ) . to . eql ( docs . map ( x => ( { name : x . name } ) ) ) ;
4098
+ done ( ) ;
4094
4099
} ) ;
4095
4100
} ) ;
4096
4101
} ) ;
4102
+ } ) ;
4097
4103
4098
- const cursorSort = ( input , output ) =>
4104
+ it ( 'should correctly apply map transform when converting cursor to array' , function ( done ) {
4105
+ const configuration = this . configuration ;
4106
+ const client = configuration . newClient ( ) ;
4107
+ client . connect ( err => {
4108
+ expect ( err ) . to . not . exist ;
4109
+ this . defer ( ( ) => client . close ( ) ) ;
4110
+
4111
+ const docs = 'Aaden Aaron Adrian Aditya Bob Joe' . split ( ' ' ) . map ( x => ( { name : x } ) ) ;
4112
+ const coll = client . db ( configuration . db ) . collection ( 'cursor_toArray_mapping' ) ;
4113
+ coll . insertMany ( docs , err => {
4114
+ expect ( err ) . to . not . exist ;
4115
+
4116
+ coll
4117
+ . find ( )
4118
+ . project ( { _id : 0 , name : 1 } )
4119
+ . map ( doc => ( { mapped : doc } ) )
4120
+ . toArray ( ( err , mappedDocs ) => {
4121
+ expect ( err ) . to . not . exist ;
4122
+ expect ( mappedDocs . map ( x => x . mapped ) ) . to . eql ( docs . map ( x => ( { name : x . name } ) ) ) ;
4123
+ done ( ) ;
4124
+ } ) ;
4125
+ } ) ;
4126
+ } ) ;
4127
+ } ) ;
4128
+ } ) ;
4129
+
4130
+ context ( 'sort' , function ( ) {
4131
+ const findSort = ( input , output ) =>
4099
4132
withMonitoredClient ( 'find' , function ( client , events , done ) {
4100
4133
const db = client . db ( 'test' ) ;
4101
4134
db . collection ( 'test_sort_dos' , ( err , collection ) => {
4102
4135
expect ( err ) . to . not . exist ;
4103
- const cursor = collection . find ( { } ) . sort ( input ) ;
4136
+ const cursor = collection . find ( { } , { sort : input } ) ;
4104
4137
cursor . next ( err => {
4105
4138
expect ( err ) . to . not . exist ;
4106
4139
expect ( events [ 0 ] . command . sort ) . to . deep . equal ( output ) ;
@@ -4109,61 +4142,15 @@ describe('Cursor', function () {
4109
4142
} ) ;
4110
4143
} ) ;
4111
4144
4112
- it ( 'should use find options object' , findSort ( { alpha : 1 } , { alpha : 1 } ) ) ;
4113
- it ( 'should use find options string' , findSort ( 'alpha' , { alpha : 1 } ) ) ;
4114
- it ( 'should use find options shallow array' , findSort ( [ 'alpha' , 1 ] , { alpha : 1 } ) ) ;
4115
- it ( 'should use find options deep array' , findSort ( [ [ 'alpha' , 1 ] ] , { alpha : 1 } ) ) ;
4116
-
4117
- it ( 'should use cursor.sort object' , cursorSort ( { alpha : 1 } , { alpha : 1 } ) ) ;
4118
- it ( 'should use cursor.sort string' , cursorSort ( 'alpha' , { alpha : 1 } ) ) ;
4119
- it ( 'should use cursor.sort shallow array' , cursorSort ( [ 'alpha' , 1 ] , { alpha : 1 } ) ) ;
4120
- it ( 'should use cursor.sort deep array' , cursorSort ( [ [ 'alpha' , 1 ] ] , { alpha : 1 } ) ) ;
4121
-
4122
- it ( 'formatSort - one key' , ( ) => {
4123
- expect ( formatSort ( 'alpha' ) ) . to . deep . equal ( { alpha : 1 } ) ;
4124
- expect ( formatSort ( [ 'alpha' ] ) ) . to . deep . equal ( { alpha : 1 } ) ;
4125
- expect ( formatSort ( 'alpha' , 1 ) ) . to . deep . equal ( { alpha : 1 } ) ;
4126
- expect ( formatSort ( 'alpha' , 'asc' ) ) . to . deep . equal ( { alpha : 1 } ) ;
4127
- expect ( formatSort ( [ [ 'alpha' , 'asc' ] ] ) ) . to . deep . equal ( { alpha : 1 } ) ;
4128
- expect ( formatSort ( 'alpha' , 'ascending' ) ) . to . deep . equal ( { alpha : 1 } ) ;
4129
- expect ( formatSort ( { alpha : 1 } ) ) . to . deep . equal ( { alpha : 1 } ) ;
4130
- expect ( formatSort ( 'beta' ) ) . to . deep . equal ( { beta : 1 } ) ;
4131
- expect ( formatSort ( [ 'beta' ] ) ) . to . deep . equal ( { beta : 1 } ) ;
4132
- expect ( formatSort ( 'beta' , - 1 ) ) . to . deep . equal ( { beta : - 1 } ) ;
4133
- expect ( formatSort ( 'beta' , 'desc' ) ) . to . deep . equal ( { beta : - 1 } ) ;
4134
- expect ( formatSort ( 'beta' , 'descending' ) ) . to . deep . equal ( { beta : - 1 } ) ;
4135
- expect ( formatSort ( { beta : - 1 } ) ) . to . deep . equal ( { beta : - 1 } ) ;
4136
- expect ( formatSort ( { alpha : { $meta : 'hi' } } ) ) . to . deep . equal ( {
4137
- alpha : { $meta : 'hi' }
4138
- } ) ;
4139
- } ) ;
4140
-
4141
- it ( 'formatSort - multi key' , ( ) => {
4142
- expect ( formatSort ( [ 'alpha' , 'beta' ] ) ) . to . deep . equal ( { alpha : 1 , beta : 1 } ) ;
4143
- expect ( formatSort ( { alpha : 1 , beta : 1 } ) ) . to . deep . equal ( { alpha : 1 , beta : 1 } ) ;
4144
- expect (
4145
- formatSort ( [
4146
- [ 'alpha' , 'asc' ] ,
4147
- [ 'beta' , 'ascending' ]
4148
- ] )
4149
- ) . to . deep . equal ( { alpha : 1 , beta : 1 } ) ;
4150
- expect ( formatSort ( { alpha : { $meta : 'hi' } , beta : 'ascending' } ) ) . to . deep . equal ( {
4151
- alpha : { $meta : 'hi' } ,
4152
- beta : 1
4153
- } ) ;
4154
- } ) ;
4155
-
4156
- it ( 'should use allowDiskUse option on sort' , {
4157
- metadata : { requires : { mongodb : '>=4.4' } } ,
4158
- test : withMonitoredClient ( 'find' , function ( client , events , done ) {
4145
+ const cursorSort = ( input , output ) =>
4146
+ withMonitoredClient ( 'find' , function ( client , events , done ) {
4159
4147
const db = client . db ( 'test' ) ;
4160
- db . collection ( 'test_sort_allow_disk_use ' , ( err , collection ) => {
4148
+ db . collection ( 'test_sort_dos ' , ( err , collection ) => {
4161
4149
expect ( err ) . to . not . exist ;
4162
- const cursor = collection . find ( { } ) . sort ( [ 'alpha' , 1 ] ) . allowDiskUse ( ) ;
4150
+ const cursor = collection . find ( { } ) . sort ( input ) ;
4163
4151
cursor . next ( err => {
4164
4152
expect ( err ) . to . not . exist ;
4165
- const { command } = events . shift ( ) ;
4166
- expect ( command . sort ) . to . deep . equal ( { alpha : 1 } ) ;
4153
+ expect ( events [ 0 ] . command . sort ) . to . deep . equal ( output ) ;
4167
4154
expect ( command . allowDiskUse ) . to . be . true ;
4168
4155
cursor . close ( done ) ;
4169
4156
} ) ;
0 commit comments