Skip to content

Commit

Permalink
Merge pull request #9 from MelleB/fix/improve-coverage
Browse files Browse the repository at this point in the history
Increase test coverage
  • Loading branch information
MelleB authored Aug 22, 2024
2 parents 7198650 + aecfc0b commit 8760d7b
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { describe, test, expect } from 'vitest';
import { describe, test, expect, assert } from 'vitest';
import { getClient, createTable, checkRows, insertRow } from './test/util';

describe('happy flows', async () => {
test('single level non explicit commit', async () => {
const tableName = 'pgtx_happy_table1';
const client = await getClient();
await client.transaction(async (tx) => {
await createTable(tx, 'table1');
await insertRow(tx, 'table1', 1);
await createTable(tx, tableName);
await insertRow(tx, tableName, 1);
});

await checkRows(client, 'table1', 1);
await checkRows(client, tableName, 1);
});

test('nested multiple non explicit commits', async () => {
const client = await getClient();
const tableName = 'table2';
const tableName = 'pgtx_happy_table2';

await client.transaction(async (tx) => {
await createTable(tx, tableName);
Expand All @@ -36,7 +37,7 @@ describe('happy flows', async () => {
});

test('simple rollback', async () => {
const tableName = 'table3';
const tableName = 'pgtx_happy_table3';
const client = await getClient();
await createTable(client, tableName);

Expand All @@ -49,7 +50,7 @@ describe('happy flows', async () => {
});

test('nested rollback', async () => {
const tableName = 'table4';
const tableName = 'pgtx_happy_table4';
const client = await getClient();

await client.transaction(async (tx) => {
Expand All @@ -65,7 +66,7 @@ describe('happy flows', async () => {
});

test('nested rollback with continuation', async () => {
const tableName = 'table5';
const tableName = 'pgtx_happy_table5';
const client = await getClient();

await client.transaction(async (tx) => {
Expand All @@ -83,11 +84,39 @@ describe('happy flows', async () => {

await checkRows(client, tableName, 1);
});

test('committing an unstarted transactions works', async () => {
const client = await getClient();

let noticeCount = 0;
client.on('notice', (notice) => {
noticeCount += 1;
assert.equal(notice.message, 'there is no transaction in progress');
});

await client.commit();

assert.equal(noticeCount, 1);
});

test('rolling back an unstarted transactions works', async () => {
const client = await getClient();

let noticeCount = 0;
client.on('notice', (notice) => {
noticeCount += 1;
assert.equal(notice.message, 'there is no transaction in progress');
});

await client.rollback();

assert.equal(noticeCount, 1);
});
});

describe('exception handling', async () => {
test('exception executes rollback', async () => {
const tableName = 'table6';
const tableName = 'pgtx_exception_table1';
const client = await getClient();

await createTable(client, tableName);
Expand All @@ -104,7 +133,7 @@ describe('exception handling', async () => {
});

test('nested exception without try/catch rolls back everything', async () => {
const tableName = 'table7';
const tableName = 'pgtx_exception_table2';
const client = await getClient();

try {
Expand All @@ -124,7 +153,7 @@ describe('exception handling', async () => {
});

test('nested exception executes rollback to savepoint', async () => {
const tableName = 'table8';
const tableName = 'pgtx_exception_table3';
const client = await getClient();

await createTable(client, tableName);
Expand All @@ -146,7 +175,7 @@ describe('exception handling', async () => {

describe('explicit mode', async () => {
test('unnested transaction - commit', async () => {
const tableName = 'table9';
const tableName = 'pgtx_explicit_table1';
const client = await getClient();

await createTable(client, tableName);
Expand All @@ -158,7 +187,7 @@ describe('explicit mode', async () => {
});

test('unnested transaction - rollback', async () => {
const tableName = 'table10';
const tableName = 'pgtx_explicit_table2';
const client = await getClient();

await createTable(client, tableName);
Expand All @@ -170,7 +199,7 @@ describe('explicit mode', async () => {
});

test('nested transaction - commit', async () => {
const tableName = 'table11';
const tableName = 'pgtx_explicit_table3';
const client = await getClient();

await createTable(client, tableName);
Expand All @@ -184,7 +213,7 @@ describe('explicit mode', async () => {
});

test('nested transaction - rollback', async () => {
const tableName = 'table12';
const tableName = 'pgtx_explicit_table4';
const client = await getClient();

await createTable(client, tableName);
Expand Down

0 comments on commit 8760d7b

Please sign in to comment.