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-12-19 17:41:11 +08:00
|
|
|
var query = client.query("select name from person order by name");
|
2010-10-26 14:47:05 +08:00
|
|
|
|
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 = [];
|
2012-09-10 09:53:12 +08:00
|
|
|
query.on('row', function(row, result) {
|
|
|
|
assert.ok(result);
|
|
|
|
rows.push(row['name']);
|
2010-10-25 14:40:24 +08:00
|
|
|
});
|
2010-12-19 17:41:11 +08:00
|
|
|
query.once('row', function(row) {
|
|
|
|
test('Can iterate through columns', function () {
|
|
|
|
var columnCount = 0;
|
|
|
|
for (column in row) {
|
|
|
|
columnCount++;
|
2012-09-10 09:53:12 +08:00
|
|
|
}
|
2010-12-19 17:41:11 +08:00
|
|
|
if ('length' in row) {
|
2011-10-11 08:21:06 +08:00
|
|
|
assert.lengthIs(row, columnCount, 'Iterating through the columns gives a different length from calling .length.');
|
2010-12-19 17:41:11 +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() {
|
2011-10-11 08:21:06 +08:00
|
|
|
assert.lengthIs(rows, 26);
|
2010-10-26 14:47:05 +08:00
|
|
|
});
|
|
|
|
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();
|
2012-12-11 12:44:58 +08:00
|
|
|
client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');"})
|
2010-10-30 09:10:49 +08:00
|
|
|
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();
|
2012-12-11 12:44:58 +08:00
|
|
|
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({text: "create temp table bang(name varchar(5)); insert into bang(name) values('zoom');"});
|
|
|
|
var result = client.query({text: "select age from boom where age < 2; select name from bang"});
|
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['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));
|
|
|
|
});
|