@@ -2,12 +2,23 @@ const _ = require('lodash');
22const { Model } = require ( '../../' ) ;
33const expect = require ( 'expect.js' ) ;
44const Promise = require ( 'bluebird' ) ;
5+ const mockKnexFactory = require ( '../../testUtils/mockKnex' ) ;
56
67module . exports = session => {
78 describe ( 'Composite keys' , ( ) => {
9+ let mockKnex ;
10+ let queries ;
11+
812 let A ;
913 let B ;
1014
15+ before ( ( ) => {
16+ mockKnex = mockKnexFactory ( session . knex , function ( mock , oldImpl , args ) {
17+ queries . push ( this . toSQL ( ) ) ;
18+ return oldImpl . apply ( this , args ) ;
19+ } ) ;
20+ } ) ;
21+
1122 before ( ( ) => {
1223 return session . knex . schema
1324 . dropTableIfExists ( 'A_B' )
@@ -113,8 +124,12 @@ module.exports = session => {
113124 }
114125 }
115126
116- A = ModelA . bindKnex ( session . knex ) ;
117- B = ModelB . bindKnex ( session . knex ) ;
127+ A = ModelA . bindKnex ( mockKnex ) ;
128+ B = ModelB . bindKnex ( mockKnex ) ;
129+ } ) ;
130+
131+ beforeEach ( ( ) => {
132+ queries = [ ] ;
118133 } ) ;
119134
120135 describe ( 'insert' , ( ) => {
@@ -326,6 +341,30 @@ module.exports = session => {
326341 ] ) ;
327342 } ) ;
328343
344+ it ( 'should work when updating the root' , async ( ) => {
345+ queries = [ ] ;
346+
347+ const result = await A . query ( ) . upsertGraph ( {
348+ id1 : 1 ,
349+ id2 : '1' ,
350+ aval : 'updated'
351+ } ) ;
352+
353+ expect ( result . aval ) . to . equal ( 'updated' ) ;
354+ expect ( queries . length ) . to . equal ( 2 ) ;
355+ expect ( queries [ 0 ] . bindings ) . to . eql ( [ 1 , '1' ] ) ;
356+
357+ if ( session . isPostgres ( ) ) {
358+ expect ( queries [ 0 ] . sql ) . to . equal (
359+ 'select "A"."id1", "A"."id2", "A"."aval" from "A" where ("A"."id1", "A"."id2") in ((?, ?))'
360+ ) ;
361+ }
362+
363+ const fromDb = await A . query ( ) . findById ( [ 1 , '1' ] ) ;
364+
365+ expect ( fromDb . aval ) . to . equal ( 'updated' ) ;
366+ } ) ;
367+
329368 it ( 'should work when `insertMissing` option is true' , ( ) => {
330369 return A . query ( )
331370 . upsertGraph (
0 commit comments