node-postgres/test/integration/client/array-tests.js

129 lines
4.2 KiB
JavaScript
Raw Normal View History

2011-01-22 06:53:24 +08:00
var helper = require(__dirname + "/test-helper");
var pg = helper.pg;
test('parsing array results', function() {
pg.connect(helper.config, assert.calls(function(err, client) {
2011-01-22 06:53:24 +08:00
assert.isNull(err);
client.query("CREATE TEMP TABLE why(names text[], numbors integer[])");
client.query('INSERT INTO why(names, numbors) VALUES(\'{"aaron", "brian","a b c" }\', \'{1, 2, 3}\')').on('error', console.log);
test('numbers', function() {
// client.connection.on('message', console.log)
client.query('SELECT numbors FROM why', assert.success(function(result) {
assert.lengthIs(result.rows[0].numbors, 3);
2011-01-22 06:53:24 +08:00
assert.equal(result.rows[0].numbors[0], 1);
assert.equal(result.rows[0].numbors[1], 2);
assert.equal(result.rows[0].numbors[2], 3);
}))
})
test('parses string arrays', function() {
client.query('SELECT names FROM why', assert.success(function(result) {
var names = result.rows[0].names;
assert.lengthIs(names, 3);
2011-01-22 06:53:24 +08:00
assert.equal(names[0], 'aaron');
assert.equal(names[1], 'brian');
assert.equal(names[2], "a b c");
pg.end();
}))
})
2012-02-24 06:13:59 +08:00
test('empty array', function(){
client.query("SELECT '{}'::text[] as names", assert.success(function(result) {
var names = result.rows[0].names;
assert.lengthIs(names, 0);
pg.end();
}))
})
test('element containing comma', function(){
client.query("SELECT '{\"joe,bob\",jim}'::text[] as names", assert.success(function(result) {
var names = result.rows[0].names;
assert.lengthIs(names, 2);
assert.equal(names[0], 'joe,bob');
assert.equal(names[1], 'jim');
pg.end();
}))
})
test('bracket in quotes', function(){
client.query("SELECT '{\"{\",\"}\"}'::text[] as names", assert.success(function(result) {
var names = result.rows[0].names;
assert.lengthIs(names, 2);
assert.equal(names[0], '{');
assert.equal(names[1], '}');
pg.end();
}))
})
test('null value', function(){
2012-03-07 09:08:11 +08:00
client.query("SELECT '{joe,null,bob,\"NULL\"}'::text[] as names", assert.success(function(result) {
2012-02-24 06:13:59 +08:00
var names = result.rows[0].names;
2012-03-07 09:08:11 +08:00
assert.lengthIs(names, 4);
2012-02-24 06:13:59 +08:00
assert.equal(names[0], 'joe');
assert.equal(names[1], null);
assert.equal(names[2], 'bob');
2012-03-07 09:08:11 +08:00
assert.equal(names[3], 'NULL');
2012-02-24 06:13:59 +08:00
pg.end();
}))
})
test('element containing quote char', function(){
client.query("SELECT '{\"joe''\",jim'',\"bob\\\\\"\"}'::text[] as names", assert.success(function(result) {
var names = result.rows[0].names;
assert.lengthIs(names, 3);
assert.equal(names[0], 'joe\'');
assert.equal(names[1], 'jim\'');
assert.equal(names[2], 'bob"');
pg.end();
}))
})
test('nested array', function(){
client.query("SELECT '{{1,joe},{2,bob}}'::text[] as names", assert.success(function(result) {
var names = result.rows[0].names;
assert.lengthIs(names, 2);
assert.lengthIs(names[0], 2);
assert.equal(names[0][0], '1');
assert.equal(names[0][1], 'joe');
assert.lengthIs(names[1], 2);
assert.equal(names[1][0], '2');
assert.equal(names[1][1], 'bob');
pg.end();
}))
})
test('integer array', function(){
client.query("SELECT '{1,2,3}'::integer[] as names", assert.success(function(result) {
var names = result.rows[0].names;
assert.lengthIs(names, 3);
assert.equal(names[0], 1);
assert.equal(names[1], 2);
assert.equal(names[2], 3);
pg.end();
}))
})
test('integer nested array', function(){
client.query("SELECT '{{1,100},{2,100},{3,100}}'::integer[] as names", assert.success(function(result) {
var names = result.rows[0].names;
assert.lengthIs(names, 3);
assert.equal(names[0][0], 1);
assert.equal(names[0][1], 100);
assert.equal(names[1][0], 2);
assert.equal(names[1][1], 100);
assert.equal(names[2][0], 3);
assert.equal(names[2][1], 100);
pg.end();
}))
})
2011-01-22 06:53:24 +08:00
}))
})