test refactoring

This commit is contained in:
brianc 2010-10-23 22:07:00 -05:00
parent a5fba7515e
commit 4743195597
10 changed files with 192 additions and 147 deletions

View File

@ -15,6 +15,6 @@ if(arg == 'all') {
runDir(__dirname+'/test/integration/'); runDir(__dirname+'/test/integration/');
} }
else { else {
runDir(__dirname+'/test/' + + '/'); runDir(__dirname+'/test/' + arg + '/');
} }

View File

@ -1,14 +1,45 @@
var helper = require(__dirname + '/test-helper'); var helper = require(__dirname + '/test-helper');
http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY http://developer.postgresql.org/pgdocs/postgres/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
test('flushing once', function() {
helper.connect(function(con) { helper.connect(function(con) {
con.on('message', function(msg) {
console.log(msg.name); assert.raises(con, 'parseComplete');
}); assert.raises(con, 'bindComplete');
assert.raises(con, 'dataRow');
assert.raises(con, 'commandComplete');
assert.raises(con, 'commandComplete');
assert.raises(con, 'readyForQuery');
con.parse({ con.parse({
text: 'select * from ids' text: 'select * from ids'
}); });
con.bind();
con.execute();
con.flush();
con.on('commandComplete', function() {
con.sync();
});
con.on('readyForQuery', function() {
con.end();
});
});
});
test("sending many flushes", function() {
helper.connect(function(con) {
assert.raises(con, 'parseComplete');
assert.raises(con, 'bindComplete');
assert.raises(con, 'dataRow');
assert.raises(con, 'commandComplete');
assert.raises(con, 'commandComplete');
assert.raises(con, 'readyForQuery');
con.parse({
text: 'select * from ids'
});
con.flush(); con.flush();
con.once('parseComplete', function() { con.once('parseComplete', function() {
@ -21,10 +52,6 @@ helper.connect(function(con) {
con.flush(); con.flush();
}); });
con.once('dataRow', function(msg) {
console.log("row: " + sys.inspect(msg));
});
con.once('commandComplete', function() { con.once('commandComplete', function() {
con.sync(); con.sync();
}); });
@ -34,4 +61,4 @@ helper.connect(function(con) {
}); });
}); });
});

View File

@ -1,15 +1,11 @@
var helper = require(__dirname + '/test-helper'); var helper = require(__dirname + '/test-helper');
test('can log in with clear text password', function() {
helper.authConnect('user_pw', 'postgres', function(con) { helper.authConnect('user_pw', 'postgres', function(con) {
assert.raises(con, 'authenticationCleartextPassword', function() {
con.once('authenticationCleartextPassword', function() {
con.passwordMessage('pass'); con.passwordMessage('pass');
}); });
assert.raises(con, 'readyForQuery', function() {
con.once('readyForQuery', function() {
console.log('successfully connected with cleartext password');
con.end(); con.end();
}); });
}); });
});

View File

@ -1,16 +1,16 @@
var helper = require(__dirname + '/test-helper'); var helper = require(__dirname + '/test-helper');
test('can log in with md5 password', function() {
helper.authConnect('user_md5', 'postgres', function(con) { helper.authConnect('user_md5', 'postgres', function(con) {
con.once('authenticationMD5Password', function(msg) { assert.raises(con, 'authenticationMD5Password', function(msg) {
assert.ok(msg.salt);
var enc = Client.md5('ssap' + 'user_md5'); var enc = Client.md5('ssap' + 'user_md5');
enc = Client.md5(enc + msg.salt.toString('binary')); enc = Client.md5(enc + msg.salt.toString('binary'));
con.passwordMessage('md5'+enc); con.passwordMessage('md5'+enc);
}); });
assert.raises(con, 'readyForQuery', function() {
con.once('readyForQuery', function() {
console.log('successfully connected with md5 password');
con.end(); con.end();
}); });
});
}); });

View File

@ -1,7 +1,8 @@
var helper = require(__dirname+'/test-helper'); var helper = require(__dirname+'/test-helper');
test('can log in with no password', function() {
helper.authConnect(function(con) { helper.authConnect(function(con) {
con.once('readyForQuery', function() { assert.raises(con, 'readyForQuery', function() {
console.log("Succesfully connected without a password");
con.end(); con.end();
}); });
}); });
});

View File

@ -4,16 +4,18 @@ var assert = require('assert');
var rows = []; var rows = [];
//testing the low level 1-1 mapping api of client to postgres messages //testing the low level 1-1 mapping api of client to postgres messages
//it's cumbersome to use the api this way //it's cumbersome to use the api this way
test('simple query', function() {
helper.connect(function(con) { helper.connect(function(con) {
con.query('select * from ids'); con.query('select * from ids');
assert.raises(con, 'dataRow');
con.on('dataRow', function(msg) { con.on('dataRow', function(msg) {
console.log("row: " + sys.inspect(msg.fields));
rows.push(msg.fields); rows.push(msg.fields);
}); });
con.once('readyForQuery', function() { assert.raises(con, 'readyForQuery', function() {
con.end(); con.end();
}); });
}); });
});
process.on('exit', function() { process.on('exit', function() {
assert.equal(rows.length, 2); assert.equal(rows.length, 2);
@ -21,10 +23,3 @@ process.on('exit', function() {
assert.strictEqual(rows[0] [0], '1'); assert.strictEqual(rows[0] [0], '1');
assert.strictEqual(rows[1] [0], '2'); assert.strictEqual(rows[1] [0], '2');
}); });
// client.query('create temporary table bang (id integer)');
// client.query('insert into bang(id) VALUES(1)');
// client.query('select * from bang',function(err, results, fields) {
// assert.equal(err, null);
// });

View File

@ -1,6 +1 @@
var requireLib = function(lib) { require(__dirname + '/../test-helper');
return require(__dirname + '/../../lib/' + lib);
};
Client = requireLib('client');
Connection = requireLib('connection');
sys = require('sys');

102
test/test-helper.js Normal file
View File

@ -0,0 +1,102 @@
sys = require('sys');
assert = require('assert');
require.paths.unshift(__dirname + '/../lib/');
Client = require('client');
EventEmitter = require('events').EventEmitter;
BufferList = require('buffer-list')
buffers = require(__dirname + '/test-buffers');
Connection = require('connection');
assert.same = function(actual, expected) {
for(var key in expected) {
assert.equal(actual[key], expected[key]);
}
};
assert.raises = function(item, eventName, callback) {
var called = false;
var id = setTimeout(function() {
assert.ok(called, "Expected '" + eventName + "' to be called.")
},1000);
item.once(eventName, function() {
called = true;
clearTimeout(id);
assert.ok(true);
if(callback) {
callback.apply(item, arguments);
}
});
};
assert.equalBuffers = function(actual, expected) {
if(actual.length != expected.length) {
console.log(actual);
console.log(expected);
assert.equal(actual.length, expected.length);
}
for(var i = 0; i < actual.length; i++) {
if(actual[i] != expected[i]) {
console.log(actual);
console.log(expected);
}
assert.equal(actual[i],expected[i]);
}
};
assert.empty = function(actual) {
assert.length(actual, 0);
};
assert.length = function(actual, expectedLength) {
assert.equal(actual.length, expectedLength);
};
['equal', 'length', 'empty', 'strictEqual', 'raises', 'equalBuffers', 'same', 'ok'].forEach(function(name) {
var old = assert[name];
assert[name] = function() {
test.assertCount++
old.apply(this, arguments);
};
});
test = function(name, action) {
try{
test.testCount ++;
var result = action();
if(result === false) {
test.ignored.push(name);
process.stdout.write('?');
}else{
process.stdout.write('.');
}
}catch(e) {
process.stdout.write('E');
test.errors.push({name: name, e: e});
}
};
test.assertCount = test.assertCount || 0;
test.testCount = test.testCount || 0;
test.ignored = test.ignored || [];
test.errors = test.errors || [];
test.start = test.start || new Date();
process.on('exit', function() {
console.log('');
var duration = ((new Date() - test.start)/1000);
console.log(test.testCount + ' tests ' + test.assertCount + ' assertions in ' + duration + ' seconds');
test.ignored.forEach(function(name) {
console.log("Ignored: " + name);
});
test.errors.forEach(function(error) {
console.log("Error: " + error.name);
});
test.errors.forEach(function(error) {
throw error.e;
});
});

View File

@ -1,75 +1,4 @@
sys = require('sys'); require(__dirname+'/../test-helper');
assert = require('assert');
Client = require(__dirname+'/../../lib/client');
EventEmitter = require('events').EventEmitter;
BufferList = require(__dirname+'/../../lib/buffer-list');
buffers = require(__dirname+'/test-buffers');
Connection = require(__dirname + '/../../lib/connection')
assert.same = function(actual, expected) {
for(var key in expected) {
assert.equal(actual[key], expected[key]);
}
};
assert.equalBuffers = function(actual, expected) {
if(actual.length != expected.length) {
console.log(actual);
console.log(expected);
assert.equal(actual.length, expected.length);
}
for(var i = 0; i < actual.length; i++) {
if(actual[i] != expected[i]) {
console.log(actual);
console.log(expected);
}
assert.equal(actual[i],expected[i]);
}
};
assert.empty = function(actual) {
assert.length(actual, 0);
};
assert.length = function(actual, expectedLength) {
assert.equal(actual.length, expectedLength);
};
test = function(name, action) {
try{
test.testCount ++;
var result = action();
if(result === false) {
test.ignored.push(name);
process.stdout.write('?');
}else{
process.stdout.write('.');
}
}catch(e) {
process.stdout.write('E');
test.errors.push({name: name, e: e});
}
};
test.testCount = 0;
test.ignored = [];
test.errors = [];
test.start = new Date();
process.on('exit', function() {
console.log('');
var duration = ((new Date() - test.start)/1000);
console.log('Ran ' + test.testCount + ' tests in ' + duration + ' seconds');
test.ignored.forEach(function(name) {
console.log("Ignored: " + name);
});
test.errors.forEach(function(error) {
console.log("Error: " + error.name);
});
test.errors.forEach(function(error) {
throw error.e;
});
});
MemoryStream = function() { MemoryStream = function() {
EventEmitter.call(this); EventEmitter.call(this);
this.packets = []; this.packets = [];