@@ -94,6 +94,78 @@ test('instance - saveAll', (t) => {
9494 } )
9595} )
9696
97+ test ( 'instance - saveAll with ids only' , ( t ) => {
98+ t . plan ( 6 )
99+
100+ let Foo
101+ let Baz
102+ let Boz
103+ let foo
104+ let baz
105+ let boz
106+
107+ dropDb ( )
108+ . then ( ( ) => {
109+ Foo = requelize . model ( 'foo' , {
110+ name : Joi . string ( )
111+ } )
112+
113+ Baz = requelize . model ( 'baz' , {
114+ name : Joi . string ( )
115+ } )
116+
117+ Boz = requelize . model ( 'boz' , {
118+ name : Joi . string ( )
119+ } )
120+
121+ Foo . hasMany ( 'baz' , 'bazes' , 'foo_id' )
122+ Baz . belongsTo ( 'foo' , 'foo' , 'foo_id' )
123+
124+ Foo . belongsToMany ( 'boz' , 'bozes' )
125+ Boz . belongsToMany ( 'foo' , 'foos' )
126+
127+ return requelize . sync ( )
128+ } )
129+ . then ( ( ) => {
130+ baz = new Baz ( { name : 'baz' } )
131+ boz = new Boz ( { name : 'boz' } )
132+
133+ return Promise . all ( [ baz . save ( ) , boz . save ( ) ] )
134+ } )
135+ . then ( ( ) => {
136+ foo = new Foo ( { name : 'foo' } )
137+
138+ foo . bazes = [ baz . id ]
139+ foo . bozes = [ boz . id ]
140+
141+ return foo . saveAll ( {
142+ bazes : true ,
143+ bozes : true
144+ } )
145+ } )
146+ . then ( ( ) => {
147+ t . equal ( 'string' , typeof foo . id , 'foo is saved' )
148+ t . equal ( 'string' , typeof baz . id , 'baz is saved' )
149+ t . equal ( 'string' , typeof boz . id , 'boz is saved' )
150+
151+ // Be careful here : foo.bazes[0] is not the same object as baz because it was recreated from the id
152+ // That means baz.foo_id will be undefined whereas foo.bazes[0].foo_id is set
153+ t . equal ( foo . id , foo . bazes [ 0 ] . foo_id , 'hasMany relationship' )
154+
155+ return Foo . embed ( { bozes : true } )
156+ } )
157+ . then ( ( res ) => {
158+ t . equal ( true , Array . isArray ( res [ 0 ] . bozes ) , 'belongsToMany relationship' )
159+ t . equal ( 1 , res [ 0 ] . bozes . length , 'belongsToMany relationship' )
160+ } )
161+ . catch ( ( err ) => {
162+ t . fail ( err )
163+ } )
164+ . then ( ( ) => {
165+ t . end ( )
166+ } )
167+ } )
168+
97169test ( 'instance - resave' , ( t ) => {
98170 t . plan ( 2 )
99171
0 commit comments