node-postgres/test/integration/client/simple-query-tests.js

57 lines
1.9 KiB
JavaScript
Raw Normal View History

var helper = require(__dirname+"/test-helper");
//before running this test make sure you run the script create-test-tables
2010-10-26 14:47:05 +08:00
test("simple query interface", function() {
var client = helper.client();
2010-10-26 14:47:05 +08:00
var query = client.query("select name from person");
client.on('drain', client.end.bind(client));
2010-10-26 14:47:05 +08:00
var rows = [];
query.on('row', function(row) {
rows.push(row[0])
});
2010-10-26 14:47:05 +08:00
assert.emits(query, 'end', function() {
2010-10-26 14:47:05 +08:00
test("returned right number of rows", function() {
assert.length(rows, 26);
});
test("row ordering", function(){
assert.equal(rows[0], "Aaron");
assert.equal(rows[25], "Zanzabar");
});
});
});
test("multiple simple queries", function() {
var client = helper.client();
client.query("create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');")
client.query("insert into bang(name) VALUES ('yes');");
var query = client.query("select name from bang");
assert.emits(query, 'row', function(row) {
assert.equal(row[0], 'boom');
assert.emits(query, 'row', function(row) {
assert.equal(row[0],'yes');
});
});
client.on('drain', client.end.bind(client));
});
2010-10-26 14:47:05 +08:00
test("multiple select statements", function() {
var client = helper.client();
client.query("create temp table boom(age integer); insert into boom(age) values(1); insert into boom(age) values(2); insert into boom(age) values(3)");
client.query("create temp table bang(name varchar(5)); insert into bang(name) values('zoom');");
var result = client.query("select age from boom where age < 2; select name from bang");
assert.emits(result, 'row', function(row) {
assert.strictEqual(row[0], 1);
assert.length(row, 1);
assert.emits(result, 'row', function(row) {
assert.length(row, 1);
assert.strictEqual(row[0], 'zoom');
});
});
client.on('drain', client.end.bind(client));
});