-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtxstatus-tests.js
More file actions
82 lines (75 loc) · 2.56 KB
/
txstatus-tests.js
File metadata and controls
82 lines (75 loc) · 2.56 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
'use strict'
const helper = require('./test-helper')
const suite = new helper.Suite()
const pg = helper.pg
const assert = require('assert')
suite.test('txStatus tracking', function (done) {
const client = new pg.Client()
client.connect(
assert.success(function () {
// Run a simple query to initialize txStatus
client.query(
'SELECT 1',
assert.success(function () {
// Test 1: Initial state after query (should be idle)
assert.equal(client.getTransactionStatus(), 'I', 'should start in idle state')
// Test 2: BEGIN transaction
client.query(
'BEGIN',
assert.success(function () {
assert.equal(client.getTransactionStatus(), 'T', 'should be in transaction state')
// Test 3: COMMIT
client.query(
'COMMIT',
assert.success(function () {
assert.equal(client.getTransactionStatus(), 'I', 'should return to idle after commit')
client.end(done)
})
)
})
)
})
)
})
)
})
suite.test('txStatus error state', function (done) {
const client = new pg.Client()
client.connect(
assert.success(function () {
// Run a simple query to initialize txStatus
client.query(
'SELECT 1',
assert.success(function () {
client.query(
'BEGIN',
assert.success(function () {
// Execute invalid SQL to trigger error state
client.query('INVALID SQL SYNTAX', function (err) {
assert(err, 'should receive error from invalid query')
// Issue a sync query to ensure ReadyForQuery has been processed
// This guarantees transaction status has been updated
client.query('SELECT 1', function () {
// This callback fires after ReadyForQuery is processed
assert.equal(client.getTransactionStatus(), 'E', 'should be in error state')
// Rollback to recover
client.query(
'ROLLBACK',
assert.success(function () {
assert.equal(
client.getTransactionStatus(),
'I',
'should return to idle after rollback from error'
)
client.end(done)
})
)
})
})
})
)
})
)
})
)
})