From 380d684682f3875c8251a8bd08befcafdc6021d2 Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Sun, 21 Jun 2020 01:00:36 +0530 Subject: [PATCH] fix: persist belongs to before persisting the parent model --- src/Factory/FactoryBuilder.ts | 43 ++++++++++++++++++++++++++----- test/factory/belongs-to-spec.ts | 7 +++++ test/factory/has-many.spec.ts | 7 +++++ test/factory/has-one.spec.ts | 7 +++++ test/factory/many-to-many.spec.ts | 9 +++++++ 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/Factory/FactoryBuilder.ts b/src/Factory/FactoryBuilder.ts index f82c7a97..9c52d1dd 100644 --- a/src/Factory/FactoryBuilder.ts +++ b/src/Factory/FactoryBuilder.ts @@ -33,6 +33,16 @@ export class FactoryBuilder implements FactoryBuilderContract void, }[] = [] + /** + * Belongs to relationships are treated different, since they are + * persisted before the parent model + */ + private withBelongsToRelations: { + name: string, + count?: number, + callback?: (factory: any) => void, + }[] = [] + /** * The current index. Updated by `makeMany` and `createMany` */ @@ -151,8 +161,14 @@ export class FactoryBuilder implements FactoryBuilderContract void): this { - this.withRelations.push({ name: relation, count, callback }) + public with (name: string, count?: number, callback?: (factory: never) => void): this { + const relation = this.model.getRelation(name) + + if (relation.relation.type === 'belongsTo') { + this.withBelongsToRelations.push({ name, count, callback }) + return this + } + + this.withRelations.push({ name, count, callback }) return this } @@ -250,16 +273,24 @@ export class FactoryBuilder implements FactoryBuilderContract { assert.instanceOf(profile.user, User) assert.isTrue(profile.user.$isPersisted) assert.equal(profile.user.id, profile.userId) + + const users = await db.from('users').select('*') + const profiles = await db.from('profiles').select('*') + + assert.lengthOf(profiles, 1) + assert.lengthOf(users, 1) + assert.equal(profiles[0].user_id, users[0].id) }) test('pass custom attributes to the relationship', async (assert) => { diff --git a/test/factory/has-many.spec.ts b/test/factory/has-many.spec.ts index aaaf5ba5..010b454b 100644 --- a/test/factory/has-many.spec.ts +++ b/test/factory/has-many.spec.ts @@ -255,6 +255,13 @@ test.group('Factory | HasMany | create', (group) => { assert.instanceOf(user.posts[0], Post) assert.isTrue(user.posts[0].$isPersisted) assert.equal(user.posts[0].userId, user.id) + + const users = await db.from('users').select('*') + const posts = await db.from('posts').select('*') + + assert.lengthOf(posts, 1) + assert.lengthOf(users, 1) + assert.equal(posts[0].user_id, users[0].id) }) test('pass custom attributes to relationship', async (assert) => { diff --git a/test/factory/has-one.spec.ts b/test/factory/has-one.spec.ts index d5d992ed..92707bac 100644 --- a/test/factory/has-one.spec.ts +++ b/test/factory/has-one.spec.ts @@ -206,6 +206,13 @@ test.group('Factory | HasOne | create', (group) => { assert.instanceOf(user.profile, Profile) assert.isTrue(user.profile.$isPersisted) assert.equal(user.profile.userId, user.id) + + const users = await db.from('users').select('*') + const profiles = await db.from('profiles').select('*') + + assert.lengthOf(profiles, 1) + assert.lengthOf(users, 1) + assert.equal(profiles[0].user_id, users[0].id) }) test('pass custom attributes to relationship', async (assert) => { diff --git a/test/factory/many-to-many.spec.ts b/test/factory/many-to-many.spec.ts index 38e1fc6d..cc1ef315 100644 --- a/test/factory/many-to-many.spec.ts +++ b/test/factory/many-to-many.spec.ts @@ -272,6 +272,15 @@ test.group('Factory | ManyToMany | create', (group) => { user_id: user.id, skill_id: user.skills[0].id, }) + + const users = await db.from('users').select('*') + const skills = await db.from('skills').select('*') + const skillUsers = await db.from('skill_user').select('*') + + assert.lengthOf(skills, 1) + assert.lengthOf(users, 1) + assert.equal(skillUsers[0].user_id, users[0].id) + assert.equal(skillUsers[0].skill_id, skills[0].id) }) test('pass custom attributes', async (assert) => {