-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtransaction.test.ts
More file actions
158 lines (138 loc) · 4.51 KB
/
Copy pathtransaction.test.ts
File metadata and controls
158 lines (138 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { describe, it, expect, vi } from 'vitest';
import type { DbClient } from './client.js';
import {
withTransaction,
createDbTransactionRunner,
passthroughTransactionRunner,
} from './transaction.js';
function createRecordingClient(options?: {
/** Fail when this SQL fragment is seen (case-insensitive). */
failOn?: string | RegExp;
/** Fail on the Nth non-control query (1-based). Control = BEGIN/COMMIT/ROLLBACK. */
failOnDataQuery?: number;
}): DbClient & { statements: string[] } {
const statements: string[] = [];
let dataQueryCount = 0;
const client: DbClient & { statements: string[] } = {
statements,
async query(text: string) {
statements.push(text);
const normalized = text.trim().toUpperCase();
const isControl =
normalized === 'BEGIN' ||
normalized === 'COMMIT' ||
normalized === 'ROLLBACK';
if (!isControl) {
dataQueryCount += 1;
if (
options?.failOnDataQuery !== undefined &&
dataQueryCount === options.failOnDataQuery
) {
throw new Error(`injected failure on data query #${dataQueryCount}`);
}
}
if (options?.failOn) {
const pattern =
typeof options.failOn === 'string'
? new RegExp(options.failOn, 'i')
: options.failOn;
if (pattern.test(text)) {
throw new Error(`injected failure on: ${text}`);
}
}
return { rows: [] };
},
async end() {
/* no-op */
},
};
return client;
}
describe('withTransaction', () => {
it('runs work without control statements when client is undefined', async () => {
const result = await withTransaction(undefined, async () => 42);
expect(result).toBe(42);
});
it('commits after successful work', async () => {
const client = createRecordingClient();
const result = await withTransaction(client, async () => {
await client.query('INSERT INTO t VALUES (1)');
return 'ok';
});
expect(result).toBe('ok');
expect(client.statements).toEqual([
'BEGIN',
'INSERT INTO t VALUES (1)',
'COMMIT',
]);
});
it('rolls back and rethrows when work fails', async () => {
const client = createRecordingClient();
await expect(
withTransaction(client, async () => {
await client.query('INSERT INTO t VALUES (1)');
throw new Error('boom');
}),
).rejects.toThrow('boom');
expect(client.statements).toEqual([
'BEGIN',
'INSERT INTO t VALUES (1)',
'ROLLBACK',
]);
expect(client.statements).not.toContain('COMMIT');
});
it('rolls back when a mid-flow data query fails (failure injection)', async () => {
const client = createRecordingClient({ failOnDataQuery: 2 });
await expect(
withTransaction(client, async () => {
await client.query('UPDATE credit_lines SET utilized = $1');
await client.query('INSERT INTO transactions ...');
return 'unreachable';
}),
).rejects.toThrow('injected failure on data query #2');
expect(client.statements[0]).toBe('BEGIN');
expect(client.statements.at(-1)).toBe('ROLLBACK');
expect(client.statements).not.toContain('COMMIT');
});
it('prefers the original error if ROLLBACK itself fails', async () => {
const statements: string[] = [];
const client: DbClient = {
async query(text: string) {
statements.push(text);
const n = text.trim().toUpperCase();
if (n === 'ROLLBACK') {
throw new Error('rollback failed');
}
return { rows: [] };
},
async end() {
/* no-op */
},
};
await expect(
withTransaction(client, async () => {
throw new Error('work failed');
}),
).rejects.toThrow('work failed');
expect(statements).toEqual(['BEGIN', 'ROLLBACK']);
});
});
describe('createDbTransactionRunner', () => {
it('delegates to withTransaction on the given client', async () => {
const client = createRecordingClient();
const run = createDbTransactionRunner(client);
const value = await run(async () => {
await client.query('SELECT 1');
return 7;
});
expect(value).toBe(7);
expect(client.statements).toEqual(['BEGIN', 'SELECT 1', 'COMMIT']);
});
});
describe('passthroughTransactionRunner', () => {
it('executes work without a database client', async () => {
const spy = vi.fn(async () => 'done');
await expect(passthroughTransactionRunner(spy)).resolves.toBe('done');
expect(spy).toHaveBeenCalledOnce();
});
});