From b6e455e400d28e7c5772ef4ffa02bf44ee3a28cf Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Fri, 13 Dec 2019 13:50:36 +0530 Subject: [PATCH] test: add more assertions to cover edge cases --- test/orm/model-belongs-to.spec.ts | 10 ++++++++-- test/orm/model-has-many.spec.ts | 20 ++++++++++++++++---- test/orm/model-has-one.spec.ts | 10 ++++++++-- test/orm/model-many-to-many.spec.ts | 28 ++++++++++++++++++++++++++-- 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/test/orm/model-belongs-to.spec.ts b/test/orm/model-belongs-to.spec.ts index 1ffed1b8..383c63c2 100644 --- a/test/orm/model-belongs-to.spec.ts +++ b/test/orm/model-belongs-to.spec.ts @@ -1135,7 +1135,7 @@ test.group('Model | BelongsTo | persist', (group) => { }) test('use parent model transaction when defined', async (assert) => { - assert.plan(4) + assert.plan(5) class User extends BaseModel { @column({ primary: true }) @@ -1169,6 +1169,12 @@ test.group('Model | BelongsTo | persist', (group) => { profile.displayName = 'virk' await profile.related<'belongsTo', 'user'>('user').associate(user) + + /** + * Ensure that related save has not committed the transaction + */ + assert.deepEqual(profile.$trx, trx) + await trx.rollback() const totalUsers = await db.query().from('users').count('*', 'total') @@ -1180,7 +1186,7 @@ test.group('Model | BelongsTo | persist', (group) => { assert.isUndefined(profile.$trx) }) - test('create save point when parent is already in transaction', async (assert) => { + test('create save point when parent is in transaction and not persisted', async (assert) => { assert.plan(5) class User extends BaseModel { diff --git a/test/orm/model-has-many.spec.ts b/test/orm/model-has-many.spec.ts index b23c87e5..14e80d9e 100644 --- a/test/orm/model-has-many.spec.ts +++ b/test/orm/model-has-many.spec.ts @@ -1431,7 +1431,7 @@ test.group('Model | HasMany | persist', (group) => { }) test('use parent model transaction when defined', async (assert) => { - assert.plan(4) + assert.plan(5) class Post extends BaseModel { @column({ primary: true }) @@ -1466,6 +1466,12 @@ test.group('Model | HasMany | persist', (group) => { post.title = 'Adonis 101' await user.related('posts').save(post) + + /** + * Ensure that related save has not committed the transaction + */ + assert.deepEqual(user.$trx, trx) + await trx.rollback() const totalUsers = await db.query().from('users').count('*', 'total') @@ -1478,7 +1484,7 @@ test.group('Model | HasMany | persist', (group) => { }) test('use parent model transaction with save many when defined', async (assert) => { - assert.plan(5) + assert.plan(6) class Post extends BaseModel { @column({ primary: true }) @@ -1516,6 +1522,12 @@ test.group('Model | HasMany | persist', (group) => { post1.title = 'Lucid 101' await user.related('posts').saveMany([post, post1]) + + /** + * Ensure that related save has not committed the transaction + */ + assert.deepEqual(user.$trx, trx) + await trx.rollback() const totalUsers = await db.query().from('users').count('*', 'total') @@ -1528,7 +1540,7 @@ test.group('Model | HasMany | persist', (group) => { assert.isUndefined(post1.$trx) }) - test('create save point when parent is already in transaction', async (assert) => { + test('create save point when parent is in transaction and not persisted', async (assert) => { assert.plan(5) class Post extends BaseModel { @@ -1577,7 +1589,7 @@ test.group('Model | HasMany | persist', (group) => { assert.isUndefined(post.$trx) }) - test('create save point with saveMany when parent is already in transaction', async (assert) => { + test('create save point with saveMany when parent is in transaction and not persisted', async (assert) => { assert.plan(5) class Post extends BaseModel { diff --git a/test/orm/model-has-one.spec.ts b/test/orm/model-has-one.spec.ts index 036d3960..e5fb5777 100644 --- a/test/orm/model-has-one.spec.ts +++ b/test/orm/model-has-one.spec.ts @@ -1124,7 +1124,7 @@ test.group('Model | HasOne | persist', (group) => { }) test('use parent model transaction when defined', async (assert) => { - assert.plan(4) + assert.plan(5) class Profile extends BaseModel { @column({ primary: true }) @@ -1159,6 +1159,12 @@ test.group('Model | HasOne | persist', (group) => { profile.displayName = 'virk' await user.related<'hasOne', 'profile'>('profile').save(profile) + + /** + * Ensure that related save has not committed the transaction + */ + assert.deepEqual(user.$trx, trx) + await trx.rollback() const totalUsers = await db.query().from('users').count('*', 'total') @@ -1170,7 +1176,7 @@ test.group('Model | HasOne | persist', (group) => { assert.isUndefined(profile.$trx) }) - test('create save point when parent is already in transaction', async (assert) => { + test('create save point when parent is already in transaction and not persisted', async (assert) => { assert.plan(5) class Profile extends BaseModel { diff --git a/test/orm/model-many-to-many.spec.ts b/test/orm/model-many-to-many.spec.ts index 05d1ede0..836aa8a5 100644 --- a/test/orm/model-many-to-many.spec.ts +++ b/test/orm/model-many-to-many.spec.ts @@ -2483,6 +2483,12 @@ test.group('Model | ManyToMany | persist', (group) => { skill.name = 'Programming' await user.related('skills').save(skill) + + /** + * Ensure that related save has not committed the transaction + */ + assert.deepEqual(user.$trx, trx) + await trx.rollback() const totalUsers = await db.query().from('users').count('*', 'total') @@ -2524,6 +2530,12 @@ test.group('Model | ManyToMany | persist', (group) => { skill.name = 'Programming' await user.related('skills').save(skill, false) + + /** + * Ensure that related save has not committed the transaction + */ + assert.deepEqual(user.$trx, trx) + await trx.rollback() const totalUsers = await db.query().from('users').count('*', 'total') @@ -2568,6 +2580,12 @@ test.group('Model | ManyToMany | persist', (group) => { skill1.name = 'Dancy' await user.related('skills').saveMany([skill, skill1]) + + /** + * Ensure that related save has not committed the transaction + */ + assert.deepEqual(user.$trx, trx) + await trx.rollback() const totalUsers = await db.query().from('users').count('*', 'total') @@ -2612,6 +2630,12 @@ test.group('Model | ManyToMany | persist', (group) => { skill1.name = 'Dancing' await user.related('skills').saveMany([skill, skill1], false) + + /** + * Ensure that related save has not committed the transaction + */ + assert.deepEqual(user.$trx, trx) + await trx.rollback() const totalUsers = await db.query().from('users').count('*', 'total') @@ -2623,7 +2647,7 @@ test.group('Model | ManyToMany | persist', (group) => { assert.lengthOf(skillUsers, 0) }) - test('create save point when parent is already in transaction', async (assert) => { + test('create save point when parent is in transaction and not persisted', async (assert) => { class Skill extends BaseModel { @column({ primary: true }) public id: number @@ -2668,7 +2692,7 @@ test.group('Model | ManyToMany | persist', (group) => { assert.lengthOf(skillUsers, 0) }) - test('create save point with save many when parent is already in transaction', async (assert) => { + test('create save point with save many when parent is in transaction and not persisted', async (assert) => { class Skill extends BaseModel { @column({ primary: true }) public id: number