@@ -478,6 +478,85 @@ test.group('Factory | HasMany | create', (group) => {
478478 assert . equal ( user . posts [ 1 ] . title , 'Lucid 101' )
479479 } )
480480
481+ test ( 'create many relationship with different states' , async ( { fs, assert } ) => {
482+ const app = new AppFactory ( ) . create ( fs . baseUrl , ( ) => { } )
483+ await app . init ( )
484+ const db = getDb ( )
485+ const adapter = ormAdapter ( db )
486+ const BaseModel = getBaseModel ( adapter )
487+
488+ class Post extends BaseModel {
489+ @column ( )
490+ declare userId : number
491+
492+ @column ( )
493+ declare tenantId : number
494+
495+ @column ( )
496+ declare title : string
497+ }
498+ Post . boot ( )
499+
500+ class User extends BaseModel {
501+ @column ( { isPrimary : true } )
502+ declare id : number
503+
504+ @column ( )
505+ declare username : string
506+
507+ @column ( )
508+ points : number = 0
509+
510+ @hasMany ( ( ) => Post )
511+ declare posts : HasMany < typeof Post >
512+ }
513+
514+ const postFactory = factoryManager
515+ . define ( Post , ( ) => {
516+ return {
517+ title : 'Adonis 101' ,
518+ }
519+ } )
520+ . build ( )
521+
522+ const factory = factoryManager
523+ . define ( User , ( ) => {
524+ return { }
525+ } )
526+ . relation ( 'posts' , ( ) => postFactory )
527+ . build ( )
528+
529+ const user = await factory
530+ . with ( 'posts' , 2 , ( related ) => related . merge ( { title : 'Lucid 101' } ) )
531+ . with ( 'posts' , 2 , ( related ) => related . merge ( { title : 'Lucid 101' , tenantId : 1 } ) )
532+ . create ( )
533+
534+ assert . isTrue ( user . $isPersisted )
535+ assert . lengthOf ( user . posts , 4 )
536+
537+ assert . instanceOf ( user . posts [ 0 ] , Post )
538+ assert . isTrue ( user . posts [ 0 ] . $isPersisted )
539+ assert . equal ( user . posts [ 0 ] . userId , user . id )
540+ assert . equal ( user . posts [ 0 ] . title , 'Lucid 101' )
541+
542+ assert . instanceOf ( user . posts [ 1 ] , Post )
543+ assert . isTrue ( user . posts [ 1 ] . $isPersisted )
544+ assert . equal ( user . posts [ 1 ] . userId , user . id )
545+ assert . equal ( user . posts [ 1 ] . title , 'Lucid 101' )
546+
547+ assert . instanceOf ( user . posts [ 2 ] , Post )
548+ assert . isTrue ( user . posts [ 2 ] . $isPersisted )
549+ assert . equal ( user . posts [ 2 ] . userId , user . id )
550+ assert . equal ( user . posts [ 2 ] . tenantId , 1 )
551+ assert . equal ( user . posts [ 2 ] . title , 'Lucid 101' )
552+
553+ assert . instanceOf ( user . posts [ 3 ] , Post )
554+ assert . isTrue ( user . posts [ 3 ] . $isPersisted )
555+ assert . equal ( user . posts [ 3 ] . userId , user . id )
556+ assert . equal ( user . posts [ 3 ] . tenantId , 1 )
557+ assert . equal ( user . posts [ 3 ] . title , 'Lucid 101' )
558+ } )
559+
481560 test ( 'create relationship with custom foreign key' , async ( { fs, assert } ) => {
482561 const app = new AppFactory ( ) . create ( fs . baseUrl , ( ) => { } )
483562 await app . init ( )
0 commit comments