@@ -10,8 +10,8 @@ const { expect } = require('chai');
10
10
const sinon = require ( 'sinon' ) ;
11
11
const { MongoRuntimeError } = require ( '../../src/error' ) ;
12
12
13
- describe ( 'utils' , function ( ) {
14
- context ( 'eachAsync' , function ( ) {
13
+ describe ( 'driver utils' , function ( ) {
14
+ context ( 'eachAsync function ' , function ( ) {
15
15
it ( 'should callback with an error' , function ( done ) {
16
16
eachAsync (
17
17
[ { error : false } , { error : true } ] ,
@@ -329,7 +329,7 @@ describe('utils', function () {
329
329
} ) ;
330
330
} ) ;
331
331
332
- context ( 'BufferPool' , function ( ) {
332
+ context ( 'BufferPool class ' , function ( ) {
333
333
it ( 'should report the correct length' , function ( ) {
334
334
const buffer = new BufferPool ( ) ;
335
335
buffer . append ( Buffer . from ( [ 0 , 1 ] ) ) ;
@@ -416,7 +416,7 @@ describe('utils', function () {
416
416
} ) ;
417
417
} ) ;
418
418
419
- context ( 'executeLegacyOperation' , function ( ) {
419
+ context ( 'executeLegacyOperation function ' , function ( ) {
420
420
it ( 'should call callback with errors on throw errors, and rethrow error' , function ( ) {
421
421
const expectedError = new Error ( 'THIS IS AN ERROR' ) ;
422
422
let callbackError , caughtError ;
@@ -459,7 +459,7 @@ describe('utils', function () {
459
459
} ) ;
460
460
} ) ;
461
461
462
- describe ( 'shuffler function' , ( ) => {
462
+ describe ( 'shuffle function' , ( ) => {
463
463
it ( 'should support iterables' , function ( ) {
464
464
// Kind of an implicit test, we should not throw/crash here.
465
465
const input = new Set ( [ 'a' , 'b' , 'c' ] ) ;
@@ -474,24 +474,43 @@ describe('utils', function () {
474
474
expect ( output ) . to . have . lengthOf ( input . length ) ;
475
475
} ) ;
476
476
477
- it ( 'should give a random subset if limit is less than the input length' , ( ) => {
478
- const input = [ 'a' , 'b' , 'c' , 'd' , 'e' ] ;
479
- const output = shuffle ( input , 3 ) ;
480
- expect ( output ) . to . have . lengthOf ( 3 ) ;
477
+ it ( `should give a random subset of length input.length - 1` , function ( ) {
478
+ const input = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ] ;
479
+ const output = shuffle ( input , input . length - 1 ) ;
480
+ expect ( output ) . to . not . deep . equal ( input ) ;
481
+ expect ( output ) . to . have . lengthOf ( input . length - 1 ) ;
481
482
} ) ;
482
483
483
- const input = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ] ;
484
- for ( let n = 1 ; n <= input . length ; n ++ ) {
485
- it ( `should give a random subset of length ${ n } ` , function ( ) {
486
- const output = shuffle ( input , n ) ;
487
- if ( n > 2 ) {
488
- // This expectation fails more at n values below 3
489
- // sparing us the flake
490
- expect ( output ) . to . not . deep . equal ( input . slice ( 0 , n ) ) ;
491
- }
492
- expect ( output ) . to . have . lengthOf ( n ) ;
493
- } ) ;
494
- }
484
+ it ( `should give a random subset of length input.length` , function ( ) {
485
+ const input = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ] ;
486
+ const output = shuffle ( input , input . length ) ;
487
+ expect ( output ) . to . not . deep . equal ( input ) ;
488
+ expect ( output ) . to . have . lengthOf ( input . length ) ;
489
+ } ) ;
490
+
491
+ it ( `should always return the same element when input is one item` , function ( ) {
492
+ const input = [ 'a' ] ;
493
+ for ( let i = 0 ; i < 10 ; i ++ ) {
494
+ const output = shuffle ( input ) ;
495
+ expect ( output ) . to . deep . equal ( input ) ;
496
+ }
497
+ for ( let i = 0 ; i < 10 ; i ++ ) {
498
+ const output = shuffle ( input , 1 ) ; // and with limit
499
+ expect ( output ) . to . deep . equal ( input ) ;
500
+ }
501
+ } ) ;
502
+
503
+ it ( `should return a different item on every call of limit 1` , function ( ) {
504
+ const input = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ] ;
505
+ const outputs = new Set ( ) ;
506
+ for ( let i = 0 ; i < 5 ; i ++ ) {
507
+ const output = shuffle ( input , 1 ) ;
508
+ expect ( output ) . to . have . lengthOf ( 1 ) ;
509
+ outputs . add ( output [ 0 ] ) ;
510
+ }
511
+ // Of the 5 shuffles we got at least 2 unique random items, this is to avoid flakiness
512
+ expect ( outputs . size ) . is . greaterThanOrEqual ( 2 ) ;
513
+ } ) ;
495
514
496
515
it ( 'should give a random shuffling of the entire input when no limit provided' , ( ) => {
497
516
const input = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ] ;
@@ -501,8 +520,14 @@ describe('utils', function () {
501
520
expect ( output ) . to . not . deep . equal ( input ) ;
502
521
expect ( output ) . to . have . lengthOf ( input . length ) ;
503
522
} ) ;
523
+ it ( 'should give a random shuffling of the entire input when limit is explicitly set to 0' , ( ) => {
524
+ const input = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ] ;
525
+ const output = shuffle ( input , 0 ) ;
526
+ expect ( output ) . to . not . deep . equal ( input ) ;
527
+ expect ( output ) . to . have . lengthOf ( input . length ) ;
528
+ } ) ;
504
529
505
- it ( 'should handle empty array' , function ( ) {
530
+ it ( 'should handle empty array if limit is unspecified or 0 ' , function ( ) {
506
531
expect ( shuffle ( [ ] ) ) . to . deep . equal ( [ ] ) ;
507
532
expect ( shuffle ( [ ] , 0 ) ) . to . deep . equal ( [ ] ) ;
508
533
} ) ;
@@ -514,6 +539,7 @@ describe('utils', function () {
514
539
515
540
it ( 'should throw if limit is greater than zero and empty array' , function ( ) {
516
541
expect ( ( ) => shuffle ( [ ] , 2 ) ) . to . throw ( MongoRuntimeError ) ;
542
+ expect ( ( ) => shuffle ( [ ] , 1 ) ) . to . throw ( MongoRuntimeError ) ;
517
543
} ) ;
518
544
519
545
it ( 'should throw if limit is larger than input size' , ( ) => {
0 commit comments