2010-11-15 13:04:41 +08:00
|
|
|
var helper = require(__dirname + '/../test-helper');
|
2013-03-08 05:57:00 +08:00
|
|
|
var pg = helper.pg;
|
2011-03-02 05:03:51 +08:00
|
|
|
|
2010-12-25 10:01:11 +08:00
|
|
|
var log = function() {
|
|
|
|
//console.log.apply(console, arguments);
|
|
|
|
}
|
2010-12-14 07:21:40 +08:00
|
|
|
|
2011-01-19 12:20:23 +08:00
|
|
|
var sink = new helper.Sink(5, 10000, function() {
|
2011-11-22 11:42:43 +08:00
|
|
|
log("ending connection pool: %j", helper.config);
|
|
|
|
pg.end(helper.config);
|
2010-12-14 07:21:40 +08:00
|
|
|
});
|
2010-11-15 13:04:41 +08:00
|
|
|
|
2010-12-03 07:47:54 +08:00
|
|
|
test('api', function() {
|
2011-11-22 11:42:43 +08:00
|
|
|
log("connecting to %j", helper.config)
|
2012-12-05 07:53:04 +08:00
|
|
|
//test weird callback behavior with node-pool
|
|
|
|
pg.connect(helper.config, function(err) {
|
|
|
|
assert.isNull(err);
|
|
|
|
arguments[1].emit('drain');
|
2013-03-08 05:57:00 +08:00
|
|
|
arguments[2]();
|
2012-12-05 07:53:04 +08:00
|
|
|
});
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
2011-02-24 09:50:43 +08:00
|
|
|
assert.equal(err, null, "Failed to connect: " + helper.sys.inspect(err));
|
2010-12-03 07:47:54 +08:00
|
|
|
|
|
|
|
client.query('CREATE TEMP TABLE band(name varchar(100))');
|
|
|
|
|
|
|
|
['the flaming lips', 'wolf parade', 'radiohead', 'bright eyes', 'the beach boys', 'dead black hearts'].forEach(function(bandName) {
|
|
|
|
var query = client.query("INSERT INTO band (name) VALUES ('"+ bandName +"')")
|
2010-11-15 15:12:29 +08:00
|
|
|
});
|
|
|
|
|
2010-12-03 07:47:54 +08:00
|
|
|
|
|
|
|
test('simple query execution',assert.calls( function() {
|
2010-12-25 10:01:11 +08:00
|
|
|
log("executing simple query")
|
|
|
|
client.query("SELECT * FROM band WHERE name = 'the beach boys'", assert.calls(function(err, result) {
|
2011-10-11 08:21:06 +08:00
|
|
|
assert.lengthIs(result.rows, 1)
|
2010-12-03 07:47:54 +08:00
|
|
|
assert.equal(result.rows.pop().name, 'the beach boys')
|
2010-12-25 10:01:11 +08:00
|
|
|
log("simple query executed")
|
|
|
|
}));
|
2010-12-03 07:47:54 +08:00
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
test('prepared statement execution',assert.calls( function() {
|
2010-12-25 10:01:11 +08:00
|
|
|
log("executing prepared statement 1")
|
2010-12-03 07:47:54 +08:00
|
|
|
client.query('SELECT * FROM band WHERE name = $1', ['dead black hearts'],assert.calls( function(err, result) {
|
2010-12-25 10:01:11 +08:00
|
|
|
log("Prepared statement 1 finished")
|
2011-10-11 08:21:06 +08:00
|
|
|
assert.lengthIs(result.rows, 1);
|
2010-12-03 07:47:54 +08:00
|
|
|
assert.equal(result.rows.pop().name, 'dead black hearts');
|
|
|
|
}))
|
|
|
|
|
2010-12-25 10:01:11 +08:00
|
|
|
log("executing prepared statement two")
|
2010-12-03 07:47:54 +08:00
|
|
|
client.query('SELECT * FROM band WHERE name LIKE $1 ORDER BY name', ['the %'], assert.calls(function(err, result) {
|
2010-12-25 10:01:11 +08:00
|
|
|
log("prepared statement two finished")
|
2011-10-11 08:21:06 +08:00
|
|
|
assert.lengthIs(result.rows, 2);
|
2010-12-03 07:47:54 +08:00
|
|
|
assert.equal(result.rows.pop().name, 'the flaming lips');
|
|
|
|
assert.equal(result.rows.pop().name, 'the beach boys');
|
2010-12-14 07:21:40 +08:00
|
|
|
sink.add();
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2010-12-03 07:47:54 +08:00
|
|
|
}))
|
|
|
|
}))
|
|
|
|
}))
|
2010-11-15 14:10:21 +08:00
|
|
|
})
|
|
|
|
|
2010-12-03 07:47:54 +08:00
|
|
|
test('executing nested queries', function() {
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
2010-12-14 07:21:40 +08:00
|
|
|
assert.isNull(err);
|
2010-12-25 10:01:11 +08:00
|
|
|
log("connected for nested queriese")
|
2010-12-03 07:47:54 +08:00
|
|
|
client.query('select now as now from NOW()', assert.calls(function(err, result) {
|
|
|
|
assert.equal(new Date().getYear(), result.rows[0].now.getYear())
|
|
|
|
client.query('select now as now_again FROM NOW()', assert.calls(function() {
|
|
|
|
client.query('select * FROM NOW()', assert.calls(function() {
|
2010-12-25 10:01:11 +08:00
|
|
|
log('all nested queries recieved')
|
2010-12-03 07:47:54 +08:00
|
|
|
assert.ok('all queries hit')
|
2010-12-14 07:21:40 +08:00
|
|
|
sink.add();
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2010-12-03 07:47:54 +08:00
|
|
|
}))
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
}))
|
2010-11-15 14:10:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('raises error if cannot connect', function() {
|
2010-12-25 10:01:11 +08:00
|
|
|
var connectionString = "pg://sfalsdkf:asdf@localhost/ieieie";
|
|
|
|
log("trying to connect to invalid place for error")
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(connectionString, assert.calls(function(err, client, done) {
|
2010-12-03 07:47:54 +08:00
|
|
|
assert.ok(err, 'should have raised an error')
|
2010-12-25 10:01:11 +08:00
|
|
|
log("invalid connection supplied error to callback")
|
|
|
|
sink.add();
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2010-12-03 07:47:54 +08:00
|
|
|
}))
|
2010-11-15 14:10:21 +08:00
|
|
|
})
|
2010-12-17 13:48:58 +08:00
|
|
|
|
2010-12-25 10:01:11 +08:00
|
|
|
test("query errors are handled and do not bubble if callback is provded", function() {
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
2010-12-17 13:48:58 +08:00
|
|
|
assert.isNull(err)
|
2010-12-25 10:01:11 +08:00
|
|
|
log("checking for query error")
|
2010-12-17 13:48:58 +08:00
|
|
|
client.query("SELECT OISDJF FROM LEIWLISEJLSE", assert.calls(function(err, result) {
|
|
|
|
assert.ok(err);
|
2010-12-25 10:01:11 +08:00
|
|
|
log("query error supplied error to callback")
|
2011-04-21 11:48:40 +08:00
|
|
|
sink.add();
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2011-04-21 11:48:40 +08:00
|
|
|
}))
|
2010-12-17 13:48:58 +08:00
|
|
|
}))
|
|
|
|
})
|
2011-01-19 12:20:23 +08:00
|
|
|
|
|
|
|
test('callback is fired once and only once', function() {
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
2011-01-19 12:20:23 +08:00
|
|
|
assert.isNull(err);
|
|
|
|
client.query("CREATE TEMP TABLE boom(name varchar(10))");
|
|
|
|
var callCount = 0;
|
|
|
|
client.query([
|
|
|
|
"INSERT INTO boom(name) VALUES('hai')",
|
|
|
|
"INSERT INTO boom(name) VALUES('boom')",
|
|
|
|
"INSERT INTO boom(name) VALUES('zoom')",
|
|
|
|
].join(";"), function(err, callback) {
|
|
|
|
assert.equal(callCount++, 0, "Call count should be 0. More means this callback fired more than once.");
|
|
|
|
sink.add();
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2011-01-19 12:20:23 +08:00
|
|
|
})
|
|
|
|
}))
|
|
|
|
})
|
2011-04-21 11:48:40 +08:00
|
|
|
|
|
|
|
test('can provide callback and config object', function() {
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
2011-04-21 11:48:40 +08:00
|
|
|
assert.isNull(err);
|
|
|
|
client.query({
|
|
|
|
name: 'boom',
|
|
|
|
text: 'select NOW()'
|
|
|
|
}, assert.calls(function(err, result) {
|
|
|
|
assert.isNull(err);
|
|
|
|
assert.equal(result.rows[0].now.getYear(), new Date().getYear())
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2011-04-21 11:48:40 +08:00
|
|
|
}))
|
|
|
|
}))
|
|
|
|
})
|
2011-08-16 09:37:46 +08:00
|
|
|
|
|
|
|
test('can provide callback and config and parameters', function() {
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
2011-08-16 09:37:46 +08:00
|
|
|
assert.isNull(err);
|
|
|
|
var config = {
|
|
|
|
text: 'select $1::text as val'
|
|
|
|
};
|
|
|
|
client.query(config, ['hi'], assert.calls(function(err, result) {
|
|
|
|
assert.isNull(err);
|
|
|
|
assert.equal(result.rows.length, 1);
|
|
|
|
assert.equal(result.rows[0].val, 'hi');
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2011-08-16 09:37:46 +08:00
|
|
|
}))
|
|
|
|
}))
|
|
|
|
})
|
2011-10-14 23:31:12 +08:00
|
|
|
|
|
|
|
test('null and undefined are both inserted as NULL', function() {
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(helper.config, assert.calls(function(err, client, done) {
|
2011-10-14 23:31:12 +08:00
|
|
|
assert.isNull(err);
|
|
|
|
client.query("CREATE TEMP TABLE my_nulls(a varchar(1), b varchar(1), c integer, d integer, e date, f date)");
|
|
|
|
client.query("INSERT INTO my_nulls(a,b,c,d,e,f) VALUES ($1,$2,$3,$4,$5,$6)", [ null, undefined, null, undefined, null, undefined ]);
|
|
|
|
client.query("SELECT * FROM my_nulls", assert.calls(function(err, result) {
|
|
|
|
assert.isNull(err);
|
|
|
|
assert.equal(result.rows.length, 1);
|
|
|
|
assert.isNull(result.rows[0].a);
|
|
|
|
assert.isNull(result.rows[0].b);
|
|
|
|
assert.isNull(result.rows[0].c);
|
|
|
|
assert.isNull(result.rows[0].d);
|
|
|
|
assert.isNull(result.rows[0].e);
|
|
|
|
assert.isNull(result.rows[0].f);
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2011-10-14 23:31:12 +08:00
|
|
|
}))
|
|
|
|
}))
|
|
|
|
})
|