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