|
| 1 | +import { describe, it, before, beforeEach, after } from 'node:test' |
| 2 | +import { strict as assert } from 'assert' |
| 3 | +import { Client, Pool } from 'pg' |
| 4 | +import { transaction } from './index.js' |
| 5 | + |
| 6 | +const withClient = async (cb: (client: Client) => Promise<void>): Promise<void> => { |
| 7 | + const client = new Client() |
| 8 | + await client.connect() |
| 9 | + try { |
| 10 | + await cb(client) |
| 11 | + } finally { |
| 12 | + await client.end() |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +describe('Transaction', () => { |
| 17 | + before(async () => { |
| 18 | + // Ensure the test table is created before running tests |
| 19 | + await withClient(async (client) => { |
| 20 | + await client.query('CREATE TABLE IF NOT EXISTS test_table (id SERIAL PRIMARY KEY, name TEXT)') |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + await withClient(async (client) => { |
| 26 | + await client.query('TRUNCATE test_table') |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + after(async () => { |
| 31 | + // Clean up the test table after running tests |
| 32 | + await withClient(async (client) => { |
| 33 | + await client.query('DROP TABLE IF EXISTS test_table') |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should create a client with an empty temp table', async () => { |
| 38 | + await withClient(async (client) => { |
| 39 | + const { rowCount } = await client.query('SELECT * FROM test_table') |
| 40 | + assert.equal(rowCount, 0, 'Temp table should be empty on creation') |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should auto-commit at end of callback', async () => { |
| 45 | + await withClient(async (client) => { |
| 46 | + await transaction(client, async (client) => { |
| 47 | + await client.query('INSERT INTO test_table (name) VALUES ($1)', ['AutoCommit']) |
| 48 | + // row should be visible within transaction |
| 49 | + const { rows } = await client.query('SELECT * FROM test_table') |
| 50 | + assert.equal(rows.length, 1, 'Row should be inserted within transaction') |
| 51 | + |
| 52 | + // while inside this transaction, the changes are not visible outside |
| 53 | + await withClient(async (innerClient) => { |
| 54 | + const { rowCount } = await innerClient.query('SELECT * FROM test_table') |
| 55 | + assert.equal(rowCount, 0, 'Temp table should still be empty inside transaction') |
| 56 | + }) |
| 57 | + }) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should rollback on error', async () => { |
| 62 | + await withClient(async (client) => { |
| 63 | + try { |
| 64 | + await transaction(client, async (client) => { |
| 65 | + await client.query('INSERT INTO test_table (name) VALUES ($1)', ['RollbackTest']) |
| 66 | + throw new Error('Intentional Error to trigger rollback') |
| 67 | + }) |
| 68 | + } catch (error) { |
| 69 | + // Expected error, do nothing |
| 70 | + } |
| 71 | + |
| 72 | + // After rollback, the table should still be empty |
| 73 | + const { rowCount } = await client.query('SELECT * FROM test_table') |
| 74 | + assert.equal(rowCount, 0, 'Temp table should be empty after rollback') |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('works with Pool', async () => { |
| 79 | + const pool = new Pool() |
| 80 | + try { |
| 81 | + await transaction(pool, async (client) => { |
| 82 | + await client.query('INSERT INTO test_table (name) VALUES ($1)', ['PoolTransaction']) |
| 83 | + const { rows } = await client.query('SELECT * FROM test_table') |
| 84 | + assert.equal(rows.length, 1, 'Row should be inserted in pool transaction') |
| 85 | + }) |
| 86 | + |
| 87 | + assert.equal(pool.idleCount, 1, 'Pool should have idle clients after transaction') |
| 88 | + |
| 89 | + // Verify the row is visible outside the transaction |
| 90 | + const { rows } = await pool.query('SELECT * FROM test_table') |
| 91 | + assert.equal(rows.length, 1, 'Row should be visible after pool transaction') |
| 92 | + } finally { |
| 93 | + await pool.end() |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + it('rollsback errors with pool', async () => { |
| 98 | + const pool = new Pool() |
| 99 | + try { |
| 100 | + try { |
| 101 | + await transaction(pool, async (client) => { |
| 102 | + await client.query('INSERT INTO test_table (name) VALUES ($1)', ['PoolRollbackTest']) |
| 103 | + throw new Error('Intentional Error to trigger rollback') |
| 104 | + }) |
| 105 | + } catch (error) { |
| 106 | + // Expected error, do nothing |
| 107 | + } |
| 108 | + |
| 109 | + // After rollback, the table should still be empty |
| 110 | + const { rowCount } = await pool.query('SELECT * FROM test_table') |
| 111 | + assert.equal(rowCount, 0, 'Temp table should be empty after pool rollback') |
| 112 | + } finally { |
| 113 | + await pool.end() |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + it('can be bound to first argument', async () => { |
| 118 | + const pool = new Pool() |
| 119 | + try { |
| 120 | + const txn = transaction.bind(null, pool) |
| 121 | + |
| 122 | + await txn(async (client) => { |
| 123 | + await client.query('INSERT INTO test_table (name) VALUES ($1)', ['BoundTransaction']) |
| 124 | + const { rows } = await client.query('SELECT * FROM test_table') |
| 125 | + assert.equal(rows.length, 1, 'Row should be inserted in bound transaction') |
| 126 | + }) |
| 127 | + |
| 128 | + // Verify the row is visible outside the transaction |
| 129 | + const { rows } = await pool.query('SELECT * FROM test_table') |
| 130 | + assert.equal(rows.length, 1, 'Row should be visible after bound transaction') |
| 131 | + } finally { |
| 132 | + await pool.end() |
| 133 | + } |
| 134 | + }) |
| 135 | +}) |
0 commit comments