@@ -187,9 +187,142 @@ export function hasManyInclusionResolverAcceptance(
187
187
expect ( toJSON ( result ) ) . to . deepEqual ( toJSON ( expected ) ) ;
188
188
} ) ;
189
189
190
- it ( 'throws when navigational properties are present when updating model instance' , async ( ) => {
191
- const created = await customerRepo . create ( { name : 'customer' } ) ;
192
- const customerId = created . id ;
190
+ skipIf (
191
+ features . hasRevisionToken ,
192
+ it ,
193
+ 'returns inclusions after running save() operation' ,
194
+ async ( ) => {
195
+ // this shows save() works well with func ensurePersistable and ObjectId
196
+ // the test skips for Cloudant as it needs the _rev property for replacement.
197
+ // see replace-by-id.suite.ts
198
+ const thor = await customerRepo . create ( { name : 'Thor' } ) ;
199
+ const odin = await customerRepo . create ( { name : 'Odin' } ) ;
200
+
201
+ const thorOrder = await orderRepo . create ( {
202
+ customerId : thor . id ,
203
+ description : 'Pizza' ,
204
+ } ) ;
205
+
206
+ const pizza = await orderRepo . findById ( thorOrder . id ) ;
207
+ pizza . customerId = odin . id ;
208
+
209
+ await orderRepo . save ( pizza ) ;
210
+ const odinPizza = await orderRepo . findById ( thorOrder . id ) ;
211
+
212
+ const result = await customerRepo . findById ( odin . id , {
213
+ include : [ { relation : 'orders' } ] ,
214
+ } ) ;
215
+ const expected = {
216
+ ...odin ,
217
+ parentId : features . emptyValue ,
218
+ orders : [
219
+ {
220
+ ...odinPizza ,
221
+ isShipped : features . emptyValue ,
222
+ // eslint-disable-next-line @typescript-eslint/camelcase
223
+ shipment_id : features . emptyValue ,
224
+ } ,
225
+ ] ,
226
+ } ;
227
+ expect ( toJSON ( result ) ) . to . containEql ( toJSON ( expected ) ) ;
228
+ } ,
229
+ ) ;
230
+
231
+ skipIf (
232
+ features . hasRevisionToken ,
233
+ it ,
234
+ 'returns inclusions after running replaceById() operation' ,
235
+ async ( ) => {
236
+ // this shows replaceById() works well with func ensurePersistable and ObjectId
237
+ // the test skips for Cloudant as it needs the _rev property for replacement.
238
+ // see replace-by-id.suite.ts
239
+ const thor = await customerRepo . create ( { name : 'Thor' } ) ;
240
+ const odin = await customerRepo . create ( { name : 'Odin' } ) ;
241
+
242
+ const thorOrder = await orderRepo . create ( {
243
+ customerId : thor . id ,
244
+ description : 'Pizza' ,
245
+ } ) ;
246
+
247
+ const pizza = await orderRepo . findById ( thorOrder . id . toString ( ) ) ;
248
+ pizza . customerId = odin . id ;
249
+ // FIXME: [mongo] if pizza obj is converted to JSON obj, it would get an error
250
+ // because it tries to convert ObjectId to string type.
251
+ // should test with JSON obj once it's fixed.
252
+
253
+ await orderRepo . replaceById ( pizza . id , pizza ) ;
254
+ const odinPizza = await orderRepo . findById ( thorOrder . id ) ;
255
+
256
+ const result = await customerRepo . find ( {
257
+ include : [ { relation : 'orders' } ] ,
258
+ } ) ;
259
+ const expected = [
260
+ {
261
+ ...thor ,
262
+ parentId : features . emptyValue ,
263
+ } ,
264
+ {
265
+ ...odin ,
266
+ parentId : features . emptyValue ,
267
+ orders : [
268
+ {
269
+ ...odinPizza ,
270
+ isShipped : features . emptyValue ,
271
+ // eslint-disable-next-line @typescript-eslint/camelcase
272
+ shipment_id : features . emptyValue ,
273
+ } ,
274
+ ] ,
275
+ } ,
276
+ ] ;
277
+ expect ( toJSON ( result ) ) . to . deepEqual ( toJSON ( expected ) ) ;
278
+ } ,
279
+ ) ;
280
+
281
+ it ( 'returns inclusions after running updateById() operation' , async ( ) => {
282
+ const thor = await customerRepo . create ( { name : 'Thor' } ) ;
283
+ const odin = await customerRepo . create ( { name : 'Odin' } ) ;
284
+
285
+ const thorOrder = await orderRepo . create ( {
286
+ customerId : thor . id ,
287
+ description : 'Pizza' ,
288
+ } ) ;
289
+
290
+ const pizza = await orderRepo . findById ( thorOrder . id . toString ( ) ) ;
291
+ pizza . customerId = odin . id ;
292
+ const reheatedPizza = toJSON ( pizza ) ;
293
+
294
+ await orderRepo . updateById ( pizza . id , reheatedPizza ) ;
295
+ const odinPizza = await orderRepo . findById ( thorOrder . id ) ;
296
+
297
+ const result = await customerRepo . find ( {
298
+ include : [ { relation : 'orders' } ] ,
299
+ } ) ;
300
+ const expected = [
301
+ {
302
+ ...thor ,
303
+ parentId : features . emptyValue ,
304
+ } ,
305
+ {
306
+ ...odin ,
307
+ parentId : features . emptyValue ,
308
+ orders : [
309
+ {
310
+ ...odinPizza ,
311
+ isShipped : features . emptyValue ,
312
+ // eslint-disable-next-line @typescript-eslint/camelcase
313
+ shipment_id : features . emptyValue ,
314
+ } ,
315
+ ] ,
316
+ } ,
317
+ ] ;
318
+ expect ( toJSON ( result ) ) . to . deepEqual ( toJSON ( expected ) ) ;
319
+ } ) ;
320
+
321
+ it ( 'throws when navigational properties are present when updating model instance with save()' , async ( ) => {
322
+ // save() calls replaceById so the result will be the same for replaceById
323
+ const customer = await customerRepo . create ( { name : 'customer' } ) ;
324
+ const anotherCus = await customerRepo . create ( { name : 'another customer' } ) ;
325
+ const customerId = customer . id ;
193
326
194
327
await orderRepo . create ( {
195
328
description : 'pizza' ,
@@ -201,11 +334,19 @@ export function hasManyInclusionResolverAcceptance(
201
334
} ) ;
202
335
expect ( found . orders ) . to . have . lengthOf ( 1 ) ;
203
336
337
+ const wrongOrder = await orderRepo . create ( {
338
+ description : 'wrong order' ,
339
+ customerId : anotherCus . id ,
340
+ } ) ;
341
+
204
342
found . name = 'updated name' ;
343
+ found . orders . push ( wrongOrder ) ;
344
+
205
345
await expect ( customerRepo . save ( found ) ) . to . be . rejectedWith (
206
- 'The `Customer` instance is not valid. Details: ` orders` is not defined in the model (value: undefined).' ,
346
+ / N a v i g a t i o n a l p r o p e r t i e s a r e n o t a l l o w e d . * " o r d e r s " / ,
207
347
) ;
208
348
} ) ;
349
+
209
350
// scope for inclusion is not supported yet
210
351
it ( 'throws error if the inclusion query contains a non-empty scope' , async ( ) => {
211
352
const customer = await customerRepo . create ( { name : 'customer' } ) ;
0 commit comments