@@ -535,81 +535,81 @@ describe('Collection', function () {
535
535
} ) ;
536
536
} ) ;
537
537
538
- describe ( '(countDocuments) ' , function ( ) {
538
+ describe ( '#estimatedDocumentCount ' , function ( ) {
539
539
let client ;
540
540
let db ;
541
541
let collection ;
542
- beforeEach ( function ( ) {
542
+
543
+ beforeEach ( async function ( ) {
543
544
client = configuration . newClient ( { w : 1 } ) ;
544
545
545
- return client . connect ( ) . then ( client => {
546
- db = client . db ( configuration . db ) ;
547
- collection = db . collection ( 'test_coll' ) ;
548
- } ) ;
549
- } ) ;
550
- afterEach ( function ( ) {
551
- return client . close ( ) ;
546
+ await client . connect ( ) ;
547
+ db = client . db ( configuration . db ) ;
548
+ collection = db . collection ( 'test_coll' ) ;
549
+ await collection . insertOne ( { a : 'c' } ) ;
552
550
} ) ;
553
551
554
- const nonMatchQueryTests = [
555
- {
556
- title : 'should correctly perform estimatedDocumentCount on non-matching query'
557
- } ,
558
- {
559
- title : 'should correctly perform countDocuments on non-matching query'
560
- }
561
- ] ;
552
+ afterEach ( async function ( ) {
553
+ await collection . drop ( ) ;
554
+ await client . close ( ) ;
555
+ } ) ;
562
556
563
- nonMatchQueryTests . forEach ( test => {
564
- it ( test . title , function ( done ) {
565
- const close = e => client . close ( ( ) => done ( e ) ) ;
566
- let thenFunction ;
567
- if (
568
- test . title === 'should correctly perform estimatedDocumentCount on non-matching query'
569
- ) {
570
- thenFunction = ( ) => collection . estimatedDocumentCount ( { a : 'b' } ) ;
571
- } else if ( test . title === 'should correctly perform countDocuments on non-matching query' ) {
572
- thenFunction = ( ) => collection . countDocuments ( { a : 'b' } ) ;
573
- }
574
- Promise . resolve ( )
575
- . then ( thenFunction )
576
- . then ( count => expect ( count ) . to . equal ( 0 ) )
577
- . then ( ( ) => close ( ) )
578
- . catch ( e => close ( e ) ) ;
579
- } ) ;
557
+ it ( 'returns the total documents in the collection' , async function ( ) {
558
+ const result = await collection . estimatedDocumentCount ( ) ;
559
+ expect ( result ) . to . equal ( 1 ) ;
580
560
} ) ;
561
+ } ) ;
581
562
582
- it ( 'countDocuments should return Promise that resolves when no callback passed' , function ( done ) {
583
- const docsPromise = collection . countDocuments ( ) ;
584
- const close = e => client . close ( ( ) => done ( e ) ) ;
563
+ describe ( '#countDocuments' , function ( ) {
564
+ let client ;
565
+ let db ;
566
+ let collection ;
585
567
586
- expect ( docsPromise ) . to . exist . and . to . be . an . instanceof ( Promise ) ;
568
+ beforeEach ( async function ( ) {
569
+ client = configuration . newClient ( { w : 1 } ) ;
570
+ await client . connect ( ) ;
571
+ db = client . db ( configuration . db ) ;
572
+ collection = db . collection ( 'test_coll' ) ;
573
+ await collection . insertOne ( { a : 'c' } ) ;
574
+ } ) ;
587
575
588
- docsPromise
589
- . then ( result => expect ( result ) . to . equal ( 0 ) )
590
- . then ( ( ) => close ( ) )
591
- . catch ( e => close ( e ) ) ;
576
+ afterEach ( async function ( ) {
577
+ await collection . drop ( ) ;
578
+ await client . close ( ) ;
592
579
} ) ;
593
580
594
- it ( 'countDocuments should not return a promise if callback given' , function ( done ) {
595
- const close = e => client . close ( ( ) => done ( e ) ) ;
581
+ context ( 'when passing a non-matching query' , function ( ) {
582
+ it ( 'returns 0' , async function ( ) {
583
+ const result = await collection . countDocuments ( { a : 'b' } ) ;
584
+ expect ( result ) . to . equal ( 0 ) ;
585
+ } ) ;
586
+ } ) ;
596
587
597
- const notPromise = collection . countDocuments ( { a : 1 } , ( ) => {
598
- expect ( notPromise ) . to . be . undefined ;
599
- close ( ) ;
588
+ context ( 'when no callback passed' , function ( ) {
589
+ it ( 'returns a promise' , function ( ) {
590
+ const docsPromise = collection . countDocuments ( ) ;
591
+ expect ( docsPromise ) . to . exist . and . to . be . an . instanceof ( Promise ) ;
592
+ return docsPromise . then ( result => expect ( result ) . to . equal ( 1 ) ) ;
600
593
} ) ;
601
594
} ) ;
602
595
603
- it ( 'countDocuments should correctly call the given callback' , function ( done ) {
604
- const docs = [ { a : 1 } , { a : 2 } ] ;
605
- const close = e => client . close ( ( ) => done ( e ) ) ;
596
+ context ( 'when a callback is passed' , function ( ) {
597
+ it ( 'does not return a promise' , function ( done ) {
598
+ const notPromise = collection . countDocuments ( { a : 1 } , ( ) => {
599
+ expect ( notPromise ) . to . be . undefined ;
600
+ done ( ) ;
601
+ } ) ;
602
+ } ) ;
606
603
607
- collection . insertMany ( docs ) . then ( ( ) =>
608
- collection . countDocuments ( { a : 1 } , ( err , data ) => {
609
- expect ( data ) . to . equal ( 1 ) ;
610
- close ( err ) ;
611
- } )
612
- ) ;
604
+ it ( 'calls the callback' , function ( done ) {
605
+ const docs = [ { a : 1 } , { a : 2 } ] ;
606
+ collection . insertMany ( docs ) . then ( ( ) =>
607
+ collection . countDocuments ( { a : 1 } , ( err , data ) => {
608
+ expect ( data ) . to . equal ( 1 ) ;
609
+ done ( err ) ;
610
+ } )
611
+ ) ;
612
+ } ) ;
613
613
} ) ;
614
614
} ) ;
615
615
0 commit comments