2010-10-25 14:40:24 +08:00
|
|
|
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() {
|
|
|
|
|
2010-10-25 14:40:24 +08:00
|
|
|
var client = helper.client();
|
2010-10-26 13:31:58 +08:00
|
|
|
|
2010-10-26 14:47:05 +08:00
|
|
|
var query = client.query("select name from person");
|
|
|
|
|
2010-10-30 09:10:49 +08:00
|
|
|
client.on('drain', client.end.bind(client));
|
|
|
|
|
2010-10-26 14:47:05 +08:00
|
|
|
var rows = [];
|
2010-10-25 14:40:24 +08:00
|
|
|
query.on('row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
rows.push(row['name'])
|
2010-10-25 14:40:24 +08:00
|
|
|
});
|
2010-10-26 14:47:05 +08:00
|
|
|
|
2010-11-01 03:43:10 +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");
|
|
|
|
});
|
2010-10-25 14:40:24 +08:00
|
|
|
});
|
2010-10-30 09:10:49 +08:00
|
|
|
});
|
2010-10-25 14:40:24 +08:00
|
|
|
|
2010-10-30 09:10:49 +08:00
|
|
|
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");
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row['name'], 'boom');
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row['name'],'yes');
|
2010-10-30 09:10:49 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
client.on('drain', client.end.bind(client));
|
2010-10-25 14:40:24 +08:00
|
|
|
});
|
2010-10-26 14:47:05 +08:00
|
|
|
|
2010-11-04 12:16:58 +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) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.strictEqual(row['age'], 1);
|
2010-11-04 12:16:58 +08:00
|
|
|
assert.emits(result, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.strictEqual(row['name'], 'zoom');
|
2010-11-04 12:16:58 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
client.on('drain', client.end.bind(client));
|
|
|
|
});
|