2010-10-26 14:51:12 +08:00
|
|
|
var helper = require(__dirname + '/test-helper');
|
2011-10-11 08:40:52 +08:00
|
|
|
var util = require('util');
|
2010-10-26 14:51:12 +08:00
|
|
|
|
2017-06-09 23:56:02 +08:00
|
|
|
|
|
|
|
test('non-query error with callback', function () {
|
|
|
|
var client = new Client({
|
|
|
|
user:'asldkfjsadlfkj'
|
|
|
|
});
|
|
|
|
client.connect(assert.calls(function (err) {
|
|
|
|
assert(err);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('non-query error', function() {
|
|
|
|
var client = new Client({
|
|
|
|
user:'asldkfjsadlfkj'
|
|
|
|
});
|
|
|
|
assert.emits(client, 'error');
|
|
|
|
client.connect();
|
|
|
|
});
|
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
var createErorrClient = function() {
|
2010-10-26 14:51:12 +08:00
|
|
|
var client = helper.client();
|
2014-09-15 09:11:51 +08:00
|
|
|
client.once('error', function(err) {
|
|
|
|
//console.log('error', util.inspect(err));
|
|
|
|
assert.fail('Client shoud not throw error during query execution');
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
2010-11-15 06:50:38 +08:00
|
|
|
client.on('drain', client.end.bind(client));
|
|
|
|
return client;
|
|
|
|
};
|
|
|
|
|
2017-06-09 23:56:02 +08:00
|
|
|
test('error handling', function() {
|
2010-11-15 06:50:38 +08:00
|
|
|
test('within a simple query', function() {
|
|
|
|
var client = createErorrClient();
|
|
|
|
|
2011-07-11 02:34:25 +08:00
|
|
|
var query = client.query("select omfg from yodas_dsflsd where pixistix = 'zoiks!!!'");
|
2010-11-15 06:50:38 +08:00
|
|
|
|
|
|
|
assert.emits(query, 'error', function(error) {
|
2014-09-15 09:11:51 +08:00
|
|
|
assert.equal(error.severity, "ERROR");
|
2010-11-15 06:50:38 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('within a prepared statement', function() {
|
|
|
|
|
|
|
|
var client = createErorrClient();
|
|
|
|
|
2011-11-22 11:59:54 +08:00
|
|
|
var q = client.query({text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);", binary: false});
|
2010-11-15 06:50:38 +08:00
|
|
|
|
|
|
|
test("when query is parsing", function() {
|
2011-07-11 02:34:25 +08:00
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
//this query wont parse since there ain't no table named bang
|
2011-03-04 02:29:00 +08:00
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
var ensureFuture = function(testClient) {
|
2011-03-04 02:29:00 +08:00
|
|
|
test("client can issue more queries successfully", function() {
|
|
|
|
var goodQuery = testClient.query("select age from boom");
|
|
|
|
assert.emits(goodQuery, 'row', function(row) {
|
|
|
|
assert.equal(row.age, 28);
|
2010-11-15 06:50:38 +08:00
|
|
|
});
|
2011-03-04 02:29:00 +08:00
|
|
|
});
|
2010-11-15 06:50:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var query = client.query({
|
|
|
|
text: "select * from bang where name = $1",
|
|
|
|
values: ['0']
|
|
|
|
});
|
|
|
|
|
|
|
|
test("query emits the error", function() {
|
|
|
|
assert.emits(query, 'error', function(err) {
|
|
|
|
ensureFuture(client);
|
|
|
|
});
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
2010-11-15 06:50:38 +08:00
|
|
|
|
2011-09-22 05:22:51 +08:00
|
|
|
test("when a query is binding", function() {
|
2011-07-11 02:34:25 +08:00
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
var query = client.query({
|
|
|
|
text: 'select * from boom where age = $1',
|
|
|
|
values: ['asldkfjasdf']
|
|
|
|
});
|
|
|
|
|
|
|
|
test("query emits the error", function() {
|
2011-07-11 02:34:25 +08:00
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
assert.emits(query, 'error', function(err) {
|
2011-03-04 02:29:00 +08:00
|
|
|
test('error has right severity', function() {
|
|
|
|
assert.equal(err.severity, "ERROR");
|
|
|
|
})
|
|
|
|
|
2010-11-15 06:50:38 +08:00
|
|
|
ensureFuture(client);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
//TODO how to test for errors during execution?
|
|
|
|
});
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
2010-10-26 14:51:12 +08:00
|
|
|
});
|
2010-11-15 06:50:38 +08:00
|
|
|
|
|
|
|
test('non-query error', function() {
|
|
|
|
var client = new Client({
|
|
|
|
user:'asldkfjsadlfkj'
|
|
|
|
});
|
|
|
|
assert.emits(client, 'error');
|
|
|
|
client.connect();
|
|
|
|
});
|
|
|
|
|
2011-09-22 05:22:51 +08:00
|
|
|
test('non-query error with callback', function() {
|
|
|
|
var client = new Client({
|
|
|
|
user:'asldkfjsadlfkj'
|
|
|
|
});
|
2011-10-03 16:25:38 +08:00
|
|
|
client.connect(assert.calls(function(error, client) {
|
2011-09-22 05:22:51 +08:00
|
|
|
assert.ok(error);
|
2011-10-03 16:25:38 +08:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test('non-error calls supplied callback', function() {
|
|
|
|
var client = new Client({
|
|
|
|
user: helper.args.user,
|
|
|
|
password: helper.args.password,
|
|
|
|
host: helper.args.host,
|
|
|
|
port: helper.args.port,
|
|
|
|
database: helper.args.database
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
|
|
|
|
2011-10-03 16:25:38 +08:00
|
|
|
client.connect(assert.calls(function(err) {
|
2014-09-15 09:11:51 +08:00
|
|
|
assert.ifError(err);
|
2011-10-03 16:25:38 +08:00
|
|
|
client.end();
|
|
|
|
}))
|
2010-10-26 14:51:12 +08:00
|
|
|
});
|
2011-05-06 08:13:43 +08:00
|
|
|
|
|
|
|
test('when connecting to invalid host', function() {
|
2013-07-08 21:04:53 +08:00
|
|
|
//this test fails about 30% on travis and only on travis...
|
|
|
|
//I'm not sure what the cause could be
|
|
|
|
if(process.env.TRAVIS) return false;
|
|
|
|
|
2011-05-06 08:13:43 +08:00
|
|
|
var client = new Client({
|
2012-10-16 06:43:14 +08:00
|
|
|
user: 'aslkdjfsdf',
|
2011-05-06 08:13:43 +08:00
|
|
|
password: '1234',
|
|
|
|
host: 'asldkfjasdf!!#1308140.com'
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
2014-09-15 09:11:51 +08:00
|
|
|
|
2013-07-06 01:52:04 +08:00
|
|
|
var delay = 5000;
|
|
|
|
var tid = setTimeout(function() {
|
2014-09-15 09:11:51 +08:00
|
|
|
var msg = "When connecting to an invalid host the error event should be emitted but it has been " + delay + " and still no error event."
|
|
|
|
assert(false, msg);
|
2013-07-06 01:52:04 +08:00
|
|
|
}, delay);
|
|
|
|
client.on('error', function() {
|
|
|
|
clearTimeout(tid);
|
|
|
|
})
|
2011-05-06 08:13:43 +08:00
|
|
|
client.connect();
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('when connecting to invalid host with callback', function() {
|
|
|
|
var client = new Client({
|
|
|
|
user: 'brian',
|
|
|
|
password: '1234',
|
|
|
|
host: 'asldkfjasdf!!#1308140.com'
|
|
|
|
});
|
|
|
|
client.connect(function(error, client) {
|
2014-09-15 09:11:51 +08:00
|
|
|
assert(error);
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
|
|
|
});
|
2011-06-02 20:28:05 +08:00
|
|
|
|
|
|
|
test('multiple connection errors (gh#31)', function() {
|
2013-07-06 01:52:04 +08:00
|
|
|
return false;
|
2011-06-02 20:28:05 +08:00
|
|
|
test('with single client', function() {
|
|
|
|
//don't run yet...this test fails...need to think of fix
|
|
|
|
var client = new Client({
|
|
|
|
user: 'blaksdjf',
|
|
|
|
password: 'omfsadfas',
|
|
|
|
host: helper.args.host,
|
|
|
|
port: helper.args.port,
|
|
|
|
database: helper.args.database
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
2011-06-02 20:28:05 +08:00
|
|
|
client.connect();
|
|
|
|
assert.emits(client, 'error', function(e) {
|
|
|
|
client.connect();
|
2011-09-22 05:22:51 +08:00
|
|
|
assert.emits(client, 'error');
|
|
|
|
});
|
|
|
|
});
|
2013-07-06 01:52:04 +08:00
|
|
|
|
|
|
|
test('with callback method', function() {
|
2013-07-23 01:45:26 +08:00
|
|
|
var badConString = "postgres://aslkdfj:oi14081@"+helper.args.host+":"+helper.args.port+"/"+helper.args.database;
|
2013-07-06 01:52:04 +08:00
|
|
|
return false;
|
|
|
|
});
|
2013-03-29 02:24:33 +08:00
|
|
|
});
|
2011-06-02 20:28:05 +08:00
|
|
|
|
2013-03-29 02:24:33 +08:00
|
|
|
test('query receives error on client shutdown', function() {
|
|
|
|
var client = new Client(helper.config);
|
|
|
|
client.connect(assert.calls(function() {
|
|
|
|
client.query('SELECT pg_sleep(5)', assert.calls(function(err, res) {
|
|
|
|
assert(err);
|
|
|
|
}));
|
|
|
|
client.end();
|
|
|
|
assert.emits(client, 'end');
|
|
|
|
}));
|
2011-09-22 05:22:51 +08:00
|
|
|
});
|
2011-06-02 20:28:05 +08:00
|
|
|
|