@@ -3272,7 +3272,7 @@ describe('Query', function() {
32723272 new User ( { name : 'Jane Doe' } )
32733273 ] ) ;
32743274 await expect ( spy , 'to have calls satisfying' , ( ) => {
3275- spy ( expect . it ( 'to be an array' ) ) ;
3275+ spy ( [ { name : 'John Doe' } , { name : 'Jane Doe' } ] ) ;
32763276 } ) ;
32773277 spy . restore ( ) ;
32783278 } ) ;
@@ -3313,6 +3313,137 @@ describe('Query', function() {
33133313 } ) ;
33143314 } ) ;
33153315 } ) ;
3316+
3317+ describe ( 'with a `batchSize` configured' , ( ) => {
3318+ it ( 'does multiple insert operations with batched arrays of data' , async function ( ) {
3319+ const spy = sinon . spy ( QueryBuilder . prototype , 'insert' ) ;
3320+ const query = new Query ( User ) ;
3321+ await query
3322+ . batchSize ( 1 )
3323+ . insert ( [
3324+ new User ( { name : 'John Doe' } ) ,
3325+ new User ( { name : 'Jane Doe' } )
3326+ ] ) ;
3327+ await expect ( spy , 'to have calls satisfying' , ( ) => {
3328+ spy ( [ { name : 'John Doe' } ] ) ;
3329+ spy ( [ { name : 'Jane Doe' } ] ) ;
3330+ } ) ;
3331+ spy . restore ( ) ;
3332+ } ) ;
3333+
3334+ it ( 'creates the right batches' , async function ( ) {
3335+ const spy = sinon . spy ( QueryBuilder . prototype , 'insert' ) ;
3336+ const users = [
3337+ { name : 'John Doe' } ,
3338+ { name : 'Jane Doe' } ,
3339+ { name : 'John Smith' } ,
3340+ { name : 'Jane Smith' }
3341+ ] ;
3342+
3343+ await new Query ( User ) . batchSize ( 1 ) . insert ( users ) ;
3344+ await expect ( spy , 'to have calls satisfying' , ( ) => {
3345+ spy ( [ { name : 'John Doe' } ] ) ;
3346+ spy ( [ { name : 'Jane Doe' } ] ) ;
3347+ spy ( [ { name : 'John Smith' } ] ) ;
3348+ spy ( [ { name : 'Jane Smith' } ] ) ;
3349+ } ) ;
3350+
3351+ spy . reset ( ) ;
3352+ await new Query ( User ) . batchSize ( 2 ) . insert ( users ) ;
3353+ await expect ( spy , 'to have calls satisfying' , ( ) => {
3354+ spy ( [ { name : 'John Doe' } , { name : 'Jane Doe' } ] ) ;
3355+ spy ( [ { name : 'John Smith' } , { name : 'Jane Smith' } ] ) ;
3356+ } ) ;
3357+
3358+ spy . reset ( ) ;
3359+ await new Query ( User ) . batchSize ( 3 ) . insert ( users ) ;
3360+ await expect ( spy , 'to have calls satisfying' , ( ) => {
3361+ spy ( [
3362+ { name : 'John Doe' } ,
3363+ { name : 'Jane Doe' } ,
3364+ { name : 'John Smith' }
3365+ ] ) ;
3366+ spy ( [ { name : 'Jane Smith' } ] ) ;
3367+ } ) ;
3368+
3369+ const allFourAtOnce = ( ) => {
3370+ spy ( [
3371+ { name : 'John Doe' } ,
3372+ { name : 'Jane Doe' } ,
3373+ { name : 'John Smith' } ,
3374+ { name : 'Jane Smith' }
3375+ ] ) ;
3376+ } ;
3377+
3378+ spy . reset ( ) ;
3379+ await new Query ( User ) . batchSize ( 4 ) . insert ( users ) ;
3380+ await expect ( spy , 'to have calls satisfying' , allFourAtOnce ) ;
3381+
3382+ spy . reset ( ) ;
3383+ await new Query ( User ) . batchSize ( 5 ) . insert ( users ) ;
3384+ await expect ( spy , 'to have calls satisfying' , allFourAtOnce ) ;
3385+
3386+ spy . reset ( ) ;
3387+ await new Query ( User ) . batchSize ( 0 ) . insert ( users ) ;
3388+ await expect ( spy , 'to have calls satisfying' , allFourAtOnce ) ;
3389+
3390+ spy . restore ( ) ;
3391+ } ) ;
3392+
3393+ it ( 'returns a single array of inserted data' , async function ( ) {
3394+ const query = new Query ( User ) . batchSize ( 1 ) ;
3395+ await expect (
3396+ query . insert ( [
3397+ new User ( { id : 1 , name : 'John Doe' } ) ,
3398+ new User ( { id : 2 , name : 'Jane Doe' } ) ,
3399+ new User ( { id : 3 , name : 'John Smith' } )
3400+ ] ) ,
3401+ 'to be fulfilled with sorted rows satisfying' ,
3402+ [
3403+ new User ( { id : 1 , name : 'John Doe' } ) ,
3404+ new User ( { id : 2 , name : 'Jane Doe' } ) ,
3405+ new User ( { id : 3 , name : 'John Smith' } )
3406+ ]
3407+ ) ;
3408+ } ) ;
3409+
3410+ describe ( 'if no rows are inserted' , function ( ) {
3411+ let insertStub ;
3412+
3413+ before ( function ( ) {
3414+ insertStub = sinon . stub ( QueryBuilder . prototype , 'insert' ) ;
3415+ } ) ;
3416+
3417+ beforeEach ( function ( ) {
3418+ insertStub . reset ( ) ;
3419+ insertStub . returns ( Promise . resolve ( [ ] ) ) ;
3420+ } ) ;
3421+
3422+ after ( function ( ) {
3423+ insertStub . restore ( ) ;
3424+ } ) ;
3425+
3426+ it ( 'resolves with am empty array' , async function ( ) {
3427+ const query = new Query ( User ) . batchSize ( 1 ) ;
3428+ await expect (
3429+ query . insert ( [ new User ( { name : 'John Doe' } ) ] ) ,
3430+ 'to be fulfilled with value satisfying' ,
3431+ [ ]
3432+ ) ;
3433+ } ) ;
3434+
3435+ describe ( "with 'require' option configured" , function ( ) {
3436+ it ( 'rejects with a NoRowsInsertedError' , async function ( ) {
3437+ const query = new Query ( User ) . batchSize ( 1 ) . require ( ) ;
3438+ await expect (
3439+ query . insert ( [ new User ( { name : 'John Doe' } ) ] ) ,
3440+ 'to be rejected with error satisfying' ,
3441+ new Query . errors . NoRowsInsertedError ( { query } )
3442+ ) ;
3443+ } ) ;
3444+ } ) ;
3445+ } ) ;
3446+ } ) ;
33163447 } ) ;
33173448 } ) ;
33183449
0 commit comments