|
| 1 | +import { strict as assert } from 'assert'; |
| 2 | +import { Client } from 'pg'; |
| 3 | +import { transaction } from '.'; |
| 4 | + |
| 5 | +class DisposableClient extends Client { |
| 6 | + // overwrite the query method and log the arguments and then dispatch to the original method |
| 7 | + override query(queryText: string, values?: any[]): Promise<any>; |
| 8 | + override query(queryConfig: any): Promise<any>; |
| 9 | + override query(queryStream: any): any; |
| 10 | + override query(...args: any[]): any { |
| 11 | + // console.log('Executing query:', ...args); |
| 12 | + // @ts-ignore |
| 13 | + return super.query(...args); |
| 14 | + } |
| 15 | + |
| 16 | + async [Symbol.asyncDispose]() { |
| 17 | + await this.end(); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +async function getClient(): Promise<DisposableClient> { |
| 22 | + const client = new DisposableClient() |
| 23 | + await client.connect(); |
| 24 | + await client.query('CREATE TEMP TABLE test_table (id SERIAL PRIMARY KEY, name TEXT)'); |
| 25 | + return client |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +describe('transaction', () => { |
| 30 | + it('should create a client with an empty temp table', async () => { |
| 31 | + await using client = await getClient(); |
| 32 | + const { rowCount } = await client.query('SELECT * FROM test_table'); |
| 33 | + assert.equal(rowCount, 0, 'Temp table should be empty on creation'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('automatically commits on success', async () => { |
| 37 | + await using client = await getClient(); |
| 38 | + |
| 39 | + const result = await transaction(client, async () => { |
| 40 | + await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test']); |
| 41 | + const { rows } = await client.query('SELECT * FROM test_table'); |
| 42 | + return rows[0].name; // Should return 'test' |
| 43 | + }); |
| 44 | + |
| 45 | + assert.equal(result, 'test'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('automatically rolls back on error', async () => { |
| 49 | + await using client = await getClient(); |
| 50 | + |
| 51 | + // Assert that the transaction function rejects with the expected error |
| 52 | + await assert.rejects( |
| 53 | + async () => { |
| 54 | + await transaction(client, async () => { |
| 55 | + await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test']); |
| 56 | + const { rows } = await client.query('SELECT * FROM test_table'); |
| 57 | + throw new Error('Simulated error'); // This will trigger a rollback |
| 58 | + }); |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'Error', |
| 62 | + message: 'Simulated error' |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + // Verify that the transaction rolled back |
| 67 | + const { rowCount } = await client.query('SELECT * FROM test_table'); |
| 68 | + assert.equal(rowCount, 0, 'Table should be empty after rollback'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('can return nothing from the transaction with correct type', async () => { |
| 72 | + await using client = await getClient(); |
| 73 | + |
| 74 | + const _nothing: void = await transaction(client, async () => { |
| 75 | + await client.query('INSERT INTO test_table (name) VALUES ($1)', ['test']); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments