2010-11-15 13:04:41 +08:00
|
|
|
var helper = require(__dirname + '/../test-helper');
|
|
|
|
var pg = require(__dirname + '/../../../lib');
|
2010-12-14 07:21:40 +08:00
|
|
|
var connectionString = helper.connectionString(__filename);
|
|
|
|
|
|
|
|
|
|
|
|
var sink = new helper.Sink(2, function() {
|
|
|
|
pg.end(connectionString);
|
|
|
|
});
|
2010-11-15 13:04:41 +08:00
|
|
|
|
2010-12-03 07:47:54 +08:00
|
|
|
test('api', function() {
|
2010-12-14 07:21:40 +08:00
|
|
|
pg.connect(connectionString, assert.calls(function(err, client) {
|
|
|
|
assert.equal(err, null, "Failed to connect: " + 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() {
|
|
|
|
client.query("SELECT * FROM band WHERE name = 'the beach boys'", function(err, result) {
|
|
|
|
assert.length(result.rows, 1)
|
|
|
|
assert.equal(result.rows.pop().name, 'the beach boys')
|
|
|
|
});
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
test('prepared statement execution',assert.calls( function() {
|
|
|
|
client.query('SELECT * FROM band WHERE name = $1', ['dead black hearts'],assert.calls( function(err, result) {
|
|
|
|
assert.length(result.rows, 1);
|
|
|
|
assert.equal(result.rows.pop().name, 'dead black hearts');
|
|
|
|
}))
|
|
|
|
|
|
|
|
client.query('SELECT * FROM band WHERE name LIKE $1 ORDER BY name', ['the %'], assert.calls(function(err, result) {
|
|
|
|
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-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() {
|
|
|
|
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-11 07:32:34 +08:00
|
|
|
var connectionString = "pg://asdf@seoiasfd:4884/ieieie";
|
|
|
|
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-11-15 14:10:21 +08:00
|
|
|
})
|