Skip to content

Commit

Permalink
extract transaction tests to separate spec
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed May 18, 2022
1 parent 4922b26 commit 7e68a68
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 49 deletions.
49 changes: 0 additions & 49 deletions tests/insert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,53 +191,4 @@ describe('mock Insert statement', () => {
expect(tracker.history.insert).toHaveLength(1);
expect(data).toEqual(3);
});

it('should support transactions', async () => {
tracker.on.insert('table_name').responseOnce(1);
tracker.on.delete('table_name').responseOnce(1);

await db.transaction(async (trx) => {
await db('table_name').insert({ name: faker.name.firstName() }).transacting(trx);
await db('table_name').delete().where({ name: faker.name.firstName() }).transacting(trx);
});

expect(tracker.history.insert).toHaveLength(1);
expect(tracker.history.delete).toHaveLength(1);
});

it('should support transactions with rollback', async () => {
tracker.on.insert('table_name').responseOnce(1);
tracker.on.delete('table_name').responseOnce(1);

await db
.transaction(async (trx) => {
await db('table_name').insert({ name: faker.name.firstName() }).transacting(trx);
await db('table_name').delete().where({ name: faker.name.firstName() }).transacting(trx);
throw new Error('TEST');
})
.catch((e) => {
expect(e.message).toBe('TEST');
});

expect(tracker.history.insert).toHaveLength(1);
expect(tracker.history.delete).toHaveLength(1);
});

it('should support nested transactions', async () => {
tracker.on.insert('table_name').responseOnce(1);
tracker.on.delete('table_name').responseOnce(1);

await db.transaction(async (trx) => {
await db('table_name').insert({ name: faker.name.firstName() }).transacting(trx);
await trx.transaction(async (innerTrx) => {
await db('table_name')
.delete()
.where({ name: faker.name.firstName() })
.transacting(innerTrx);
});
});

expect(tracker.history.insert).toHaveLength(1);
expect(tracker.history.delete).toHaveLength(1);
});
});
81 changes: 81 additions & 0 deletions tests/transaction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { faker } from '@faker-js/faker';
import knex, { Knex } from 'knex';
import { getTracker, MockClient, Tracker } from '../src';

describe('transaction', () => {
let db: Knex;
let tracker: Tracker;

beforeAll(() => {
db = knex({
client: MockClient,
});
tracker = getTracker();
});

afterEach(() => {
tracker.reset();
});

it('should support transactions', async () => {
tracker.on.insert('table_name').responseOnce(1);
tracker.on.delete('table_name').responseOnce(1);

await db.transaction(async (trx) => {
await db('table_name').insert({ name: faker.name.firstName() }).transacting(trx);
await db('table_name').delete().where({ name: faker.name.firstName() }).transacting(trx);
});

expect(tracker.history.insert).toHaveLength(1);
expect(tracker.history.delete).toHaveLength(1);
});

it('should support transactions with rollback', async () => {
tracker.on.insert('table_name').responseOnce(1);
tracker.on.delete('table_name').responseOnce(1);

await db
.transaction(async (trx) => {
await db('table_name').insert({ name: faker.name.firstName() }).transacting(trx);
await db('table_name').delete().where({ name: faker.name.firstName() }).transacting(trx);
throw new Error('TEST');
})
.catch((e) => {
expect(e.message).toBe('TEST');
});

expect(tracker.history.insert).toHaveLength(1);
expect(tracker.history.delete).toHaveLength(1);
});

it('should support nested transactions', async () => {
tracker.on.insert('table_name').responseOnce(1);
tracker.on.delete('table_name').responseOnce(1);

await db.transaction(async (trx) => {
await db('table_name').insert({ name: faker.name.firstName() }).transacting(trx);
await trx.transaction(async (innerTrx) => {
await db('table_name')
.delete()
.where({ name: faker.name.firstName() })
.transacting(innerTrx);
});
});

expect(tracker.history.insert).toHaveLength(1);
expect(tracker.history.delete).toHaveLength(1);
});

it('should support transactions with commit', async () => {
tracker.on.insert('table_name').responseOnce(1);
tracker.on.delete('table_name').responseOnce(1);

const trx = await db.transaction();
await trx('table_name').insert({ name: faker.name.firstName() });
await trx('table_name').delete().where({ name: faker.name.firstName() });
await trx.commit();

expect(tracker.history.insert).toHaveLength(1);
expect(tracker.history.delete).toHaveLength(1);
});
});

0 comments on commit 7e68a68

Please sign in to comment.