Permalink
Browse files

make pg.connect config/conString argument optional

  • Loading branch information...
1 parent 7cfd76c commit a7fbe95e726b3dd96e34e498a99db8e7d0f4878c @brianc committed Aug 12, 2011
View
@@ -9,13 +9,20 @@ module.exports = {
}
var connect = function(config, callback) {
- //lookup pool using config as key
- //TODO this don't work so hot w/ object configs
- var pool = clientPools[config];
+ var poolName = config;
+ if(typeof config === 'function') {
+ callback = config;
+ config = defaults;
+ }
+ //get unique pool name
+ if(typeof config === 'object') {
+ poolName = config.user + config.database + config.host + config.port;
+ }
+ var pool = clientPools[poolName];
//create pool if doesn't exist
if(!pool) {
- pool = clientPools[config] = new Pool(defaults.poolSize, function() {
+ pool = clientPools[poolName] = new Pool(defaults.poolSize, function() {
var client = new Client(config);
client.connected = false;
return client;
@@ -0,0 +1,14 @@
+var helper = require(__dirname + '/test-helper');
+
+//setup defaults
+helper.pg.defaults.user = helper.args.user;
+helper.pg.defaults.password = helper.args.password;
+helper.pg.defaults.host = helper.args.host;
+helper.pg.defaults.port = helper.args.port;
+helper.pg.defaults.database = helper.args.database;
+helper.pg.defaults.poolSize = 1;
+
+helper.pg.connect(assert.calls(function(err, client) {
+ assert.isNull(err);
+ client.end();
+}));
@@ -1,6 +1,11 @@
var helper = require(__dirname + '/test-helper');
helper.pg.defaults.poolSize = 1;
+helper.pg.defaults.user = helper.args.user;
+helper.pg.defaults.password = helper.args.password;
+helper.pg.defaults.database = helper.args.database;
+helper.pg.defaults.port = helper.args.port;
+helper.pg.defaults.host = helper.args.host;
var args = {
user: helper.args.user,
@@ -10,23 +15,58 @@ var args = {
host: helper.args.host
}
-helper.pg.connect(args, assert.calls(function(err, client) {
- assert.isNull(err);
- client.iGotAccessed = true;
- client.query("SELECT NOW()")
-}))
-
var moreArgs = {
- user: helper.args.user + "2",
- host: helper.args.host,
+ database: helper.args.database,
password: helper.args.password,
+ port: helper.args.port,
+ user: helper.args.user,
+ host: helper.args.host,
+ zomg: true
+}
+
+var badArgs = {
+ user: helper.args.user + 'laksdjfl',
+ host: helper.args.host,
+ password: helper.args.password + 'asldkfjlas',
database: helper.args.database,
port: helper.args.port,
zomg: true
}
-helper.pg.connect(moreArgs, assert.calls(function(err, client) {
- assert.isNull(err);
- assert.ok(client.iGotAccessed === true)
- client.end();
-}))
+test('connecting with complete config', function() {
+
+ helper.pg.connect(args, assert.calls(function(err, client) {
+ assert.isNull(err);
+ client.iGotAccessed = true;
+ client.query("SELECT NOW()")
+ }));
+
+});
+
+test('connecting with different config object', function() {
+
+ helper.pg.connect(moreArgs, assert.calls(function(err, client) {
+ assert.isNull(err);
+ assert.ok(client.iGotAccessed === true)
+ client.query("SELECT NOW()");
+ }))
+
+});
+
+test('connecting with all defaults', function() {
+
+ helper.pg.connect(assert.calls(function(err, client) {
+ assert.isNull(err);
+ assert.ok(client.iGotAccessed === true);
+ client.end();
+ }));
+
+});
+
+test('connecting with invalid config', function() {
+
+ helper.pg.connect(badArgs, assert.calls(function(err, client) {
+ assert.ok(err != null, "Expected connection error using invalid connection credentials");
+ }));
+
+});

0 comments on commit a7fbe95

Please sign in to comment.