-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathquery-error-handling-tests.js
88 lines (82 loc) · 3.03 KB
/
query-error-handling-tests.js
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
"use strict";
var helper = require('./test-helper');
var util = require('util');
var Query = helper.pg.Query;
test('error during query execution', function() {
var client = new Client(helper.args);
client.connect(assert.success(function() {
var queryText = 'select pg_sleep(10)'
var sleepQuery = new Query(queryText);
var pidColName = 'procpid'
var queryColName = 'current_query';
helper.versionGTE(client, 90200, assert.success(function(isGreater) {
if(isGreater) {
pidColName = 'pid';
queryColName = 'query';
}
var query1 = client.query(sleepQuery, assert.calls(function(err, result) {
assert(err);
client.end();
}));
//ensure query1 does not emit an 'end' event
//because it was killed and received an error
//https://github.com/brianc/node-postgres/issues/547
query1.on('end', function() {
assert.fail('Query with an error should not emit "end" event')
})
setTimeout(function() {
var client2 = new Client(helper.args);
client2.connect(assert.success(function() {
var killIdleQuery = `SELECT ${pidColName}, (SELECT pg_cancel_backend(${pidColName})) AS killed FROM pg_stat_activity WHERE ${queryColName} LIKE $1`;
client2.query(killIdleQuery, [queryText], assert.calls(function(err, res) {
assert.ifError(err);
assert(res.rows.length > 0);
client2.end();
assert.emits(client2, 'end');
}));
}));
}, 300)
}));
}));
});
if (helper.config.native) {
return
}
test('9.3 column error fields', function() {
var client = new Client(helper.args);
client.connect(assert.success(function() {
helper.versionGTE(client, 90300, assert.success(function(isGreater) {
if(!isGreater) {
return client.end();
}
client.query('CREATE TEMP TABLE column_err_test(a int NOT NULL)');
client.query('INSERT INTO column_err_test(a) VALUES (NULL)', function (err) {
assert.equal(err.severity, 'ERROR');
assert.equal(err.code, '23502');
assert.equal(err.table, 'column_err_test');
assert.equal(err.column, 'a');
return client.end();
});
}));
}));
});
test('9.3 constraint error fields', function() {
var client = new Client(helper.args);
client.connect(assert.success(function() {
helper.versionGTE(client, 90300, assert.success(function(isGreater) {
if(!isGreater) {
console.log('skip 9.3 error field on older versions of postgres');
return client.end();
}
client.query('CREATE TEMP TABLE constraint_err_test(a int PRIMARY KEY)');
client.query('INSERT INTO constraint_err_test(a) VALUES (1)');
client.query('INSERT INTO constraint_err_test(a) VALUES (1)', function (err) {
assert.equal(err.severity, 'ERROR');
assert.equal(err.code, '23505');
assert.equal(err.table, 'constraint_err_test');
assert.equal(err.constraint, 'constraint_err_test_pkey');
return client.end();
});
}));
}));
});