2010-11-15 13:04:41 +08:00
|
|
|
var helper = require(__dirname + '/../test-helper');
|
|
|
|
var pg = require(__dirname + '/../../../lib');
|
2011-03-02 05:03:51 +08:00
|
|
|
|
|
|
|
if(helper.args.native) {
|
2011-08-12 09:59:56 +08:00
|
|
|
pg = require(__dirname + '/../../../lib').native;
|
2011-03-02 05:03:51 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:40:52 +08:00
|
|
|
if(helper.args.libpq) {
|
|
|
|
pg = require(__dirname + "/../../../lib/binding");
|
|
|
|
}
|
2010-12-14 07:21:40 +08:00
|
|
|
var connectionString = helper.connectionString(__filename);
|
|
|
|
|
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() {
|
2010-12-25 10:01:11 +08:00
|
|
|
log("ending connection pool: %s", connectionString);
|
2010-12-14 07:21:40 +08:00
|
|
|
pg.end(connectionString);
|
|
|
|
});
|
2010-11-15 13:04:41 +08:00
|
|
|
|
2010-12-03 07:47:54 +08:00
|
|
|
test('api', function() {
|
2010-12-25 10:01:11 +08:00
|
|
|
log("connecting to %s", connectionString)
|
2010-12-14 07:21:40 +08:00
|
|
|
pg.connect(connectionString, assert.calls(function(err, client) {
|
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) {
|
2010-12-03 07:47:54 +08:00
|
|
|
assert.length(result.rows, 1)
|
|
|
|
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")
|
2010-12-03 07:47:54 +08:00
|
|
|
assert.length(result.rows, 1);
|
|
|
|
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")
|
2010-12-03 07:47:54 +08:00
|
|
|
assert.length(result.rows, 2);
|
|
|
|
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();
|
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() {
|
2010-12-14 07:21:40 +08:00
|
|
|
pg.connect(connectionString, assert.calls(function(err, client) {
|
|
|
|
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();
|
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")
|
2010-12-11 07:32:34 +08:00
|
|
|
pg.connect(connectionString, assert.calls(function(err, client) {
|
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();
|
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() {
|
2010-12-17 13:48:58 +08:00
|
|
|
pg.connect(connectionString, assert.calls(function(err, client) {
|
|
|
|
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();
|
|
|
|
}))
|
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() {
|
|
|
|
pg.connect(connectionString, assert.calls(function(err, client) {
|
|
|
|
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();
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
})
|
2011-04-21 11:48:40 +08:00
|
|
|
|
|
|
|
test('can provide callback and config object', function() {
|
|
|
|
pg.connect(connectionString, assert.calls(function(err, client) {
|
|
|
|
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())
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
})
|