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,37 +1,64 @@
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
helper.connect(function(con) { test('flushing once', function() {
con.on('message', function(msg) { helper.connect(function(con) {
console.log(msg.name);
});
con.parse({ assert.raises(con, 'parseComplete');
text: 'select * from ids' assert.raises(con, 'bindComplete');
}); assert.raises(con, 'dataRow');
con.flush(); assert.raises(con, 'commandComplete');
assert.raises(con, 'commandComplete');
assert.raises(con, 'readyForQuery');
con.once('parseComplete', function() { con.parse({
text: 'select * from ids'
});
con.bind(); con.bind();
con.flush();
});
con.once('bindComplete', function() {
con.execute(); con.execute();
con.flush(); con.flush();
con.on('commandComplete', function() {
con.sync();
});
con.on('readyForQuery', function() {
con.end();
});
}); });
con.once('dataRow', function(msg) {
console.log("row: " + sys.inspect(msg));
});
con.once('commandComplete', function() {
con.sync();
});
con.once('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.once('parseComplete', function() {
con.bind();
con.flush();
});
con.once('bindComplete', function() {
con.execute();
con.flush();
});
con.once('commandComplete', function() {
con.sync();
});
con.once('readyForQuery', function() {
con.end();
});
});
});

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.end();
});
}); });
con.once('readyForQuery', function() {
console.log('successfully connected with cleartext password');
con.end();
});
}); });

View File

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

View File

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

View File

@ -4,14 +4,16 @@ 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
helper.connect(function(con) { test('simple query', function() {
con.query('select * from ids'); helper.connect(function(con) {
con.on('dataRow', function(msg) { con.query('select * from ids');
console.log("row: " + sys.inspect(msg.fields)); assert.raises(con, 'dataRow');
rows.push(msg.fields); con.on('dataRow', function(msg) {
}); rows.push(msg.fields);
con.once('readyForQuery', function() { });
con.end(); assert.raises(con, 'readyForQuery', function() {
con.end();
});
}); });
}); });
@ -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 = [];