2010-10-03 13:27:16 +08:00
|
|
|
require(__dirname+'/test-helper');
|
2010-10-11 11:42:13 +08:00
|
|
|
|
2014-03-18 17:05:55 +08:00
|
|
|
var pguser = process.env['PGUSER'] || process.env.USER;
|
|
|
|
var pgdatabase = process.env['PGDATABASE'] || process.env.USER;
|
|
|
|
var pgport = process.env['PGPORT'] || 5432;
|
|
|
|
|
2010-10-03 13:27:16 +08:00
|
|
|
test('client settings', function() {
|
2010-10-11 11:42:13 +08:00
|
|
|
|
2010-10-03 13:27:16 +08:00
|
|
|
test('defaults', function() {
|
|
|
|
var client = new Client();
|
2014-03-18 17:05:55 +08:00
|
|
|
assert.equal(client.user, pguser);
|
|
|
|
assert.equal(client.database, pgdatabase);
|
|
|
|
assert.equal(client.port, pgport);
|
2010-10-03 13:27:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('custom', function() {
|
|
|
|
var user = 'brian';
|
|
|
|
var database = 'pgjstest';
|
2010-10-20 12:26:25 +08:00
|
|
|
var password = 'boom';
|
2010-10-03 13:27:16 +08:00
|
|
|
var client = new Client({
|
|
|
|
user: user,
|
|
|
|
database: database,
|
2010-10-20 12:26:25 +08:00
|
|
|
port: 321,
|
|
|
|
password: password
|
2010-10-03 13:27:16 +08:00
|
|
|
});
|
2010-12-29 00:44:33 +08:00
|
|
|
|
2010-10-03 13:27:16 +08:00
|
|
|
assert.equal(client.user, user);
|
|
|
|
assert.equal(client.database, database);
|
|
|
|
assert.equal(client.port, 321);
|
2010-10-20 12:26:25 +08:00
|
|
|
assert.equal(client.password, password);
|
2010-10-03 13:27:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2010-12-29 00:44:33 +08:00
|
|
|
|
|
|
|
test('initializing from a config string', function() {
|
|
|
|
|
|
|
|
test('uses the correct values from the config string', function() {
|
2013-07-23 01:45:26 +08:00
|
|
|
var client = new Client("postgres://brian:pass@host1:333/databasename")
|
2014-06-16 05:33:23 +08:00
|
|
|
assert.equal(client.user, 'brian');
|
|
|
|
assert.equal(client.password, "pass");
|
|
|
|
assert.equal(client.host, "host1");
|
|
|
|
assert.equal(client.port, 333);
|
|
|
|
assert.equal(client.database, "databasename");
|
|
|
|
});
|
2010-12-29 00:44:33 +08:00
|
|
|
|
2013-01-25 09:05:55 +08:00
|
|
|
test('uses the correct values from the config string with space in password', function() {
|
2013-07-23 01:45:26 +08:00
|
|
|
var client = new Client("postgres://brian:pass word@host1:333/databasename")
|
2014-06-16 05:33:23 +08:00
|
|
|
assert.equal(client.user, 'brian');
|
|
|
|
assert.equal(client.password, "pass word");
|
|
|
|
assert.equal(client.host, "host1");
|
|
|
|
assert.equal(client.port, 333);
|
|
|
|
assert.equal(client.database, "databasename");
|
|
|
|
});
|
2013-01-25 09:05:55 +08:00
|
|
|
|
2010-12-29 00:44:33 +08:00
|
|
|
test('when not including all values the defaults are used', function() {
|
2014-06-16 05:33:23 +08:00
|
|
|
var client = new Client("postgres://host1");
|
|
|
|
assert.equal(client.user, process.env['PGUSER'] || process.env.USER);
|
|
|
|
assert.equal(client.password, process.env['PGPASSWORD'] || null);
|
|
|
|
assert.equal(client.host, "host1");
|
|
|
|
assert.equal(client.port, process.env['PGPORT'] || 5432);
|
|
|
|
assert.equal(client.database, process.env['PGDATABASE'] || process.env.USER);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when not including all values the environment variables are used', function() {
|
|
|
|
var envUserDefined = process.env['PGUSER'] !== undefined;
|
|
|
|
var envPasswordDefined = process.env['PGPASSWORD'] !== undefined;
|
|
|
|
var envDBDefined = process.env['PGDATABASE'] !== undefined;
|
|
|
|
var envHostDefined = process.env['PGHOST'] !== undefined;
|
|
|
|
var envPortDefined = process.env['PGPORT'] !== undefined;
|
|
|
|
|
|
|
|
var savedEnvUser = process.env['PGUSER'];
|
|
|
|
var savedEnvPassword = process.env['PGPASSWORD'];
|
|
|
|
var savedEnvDB = process.env['PGDATABASE'];
|
|
|
|
var savedEnvHost = process.env['PGHOST'];
|
|
|
|
var savedEnvPort = process.env['PGPORT'];
|
|
|
|
|
|
|
|
process.env['PGUSER'] = 'utUser1';
|
|
|
|
process.env['PGPASSWORD'] = 'utPass1';
|
|
|
|
process.env['PGDATABASE'] = 'utDB1';
|
|
|
|
process.env['PGHOST'] = 'utHost1';
|
|
|
|
process.env['PGPORT'] = 5464;
|
|
|
|
|
|
|
|
var client = new Client("postgres://host1");
|
|
|
|
assert.equal(client.user, process.env['PGUSER']);
|
|
|
|
assert.equal(client.password, process.env['PGPASSWORD']);
|
|
|
|
assert.equal(client.host, "host1");
|
|
|
|
assert.equal(client.port, process.env['PGPORT']);
|
|
|
|
assert.equal(client.database, process.env['PGDATABASE']);
|
2010-12-29 00:44:33 +08:00
|
|
|
|
2014-06-16 05:33:23 +08:00
|
|
|
if (envUserDefined) {
|
|
|
|
process.env['PGUSER'] = savedEnvUser;
|
|
|
|
} else {
|
|
|
|
delete process.env['PGUSER'];
|
|
|
|
}
|
2010-12-29 00:44:33 +08:00
|
|
|
|
2014-06-16 05:33:23 +08:00
|
|
|
if (envPasswordDefined) {
|
|
|
|
process.env['PGPASSWORD'] = savedEnvPassword;
|
|
|
|
} else {
|
|
|
|
delete process.env['PGPASSWORD'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (envDBDefined) {
|
|
|
|
process.env['PGDATABASE'] = savedEnvDB;
|
|
|
|
} else {
|
|
|
|
delete process.env['PGDATABASE'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (envHostDefined) {
|
|
|
|
process.env['PGHOST'] = savedEnvHost;
|
|
|
|
} else {
|
|
|
|
delete process.env['PGHOST'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (envPortDefined) {
|
|
|
|
process.env['PGPORT'] = savedEnvPort;
|
|
|
|
} else {
|
|
|
|
delete process.env['PGPORT'];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2011-03-02 04:35:14 +08:00
|
|
|
|
|
|
|
test('calls connect correctly on connection', function() {
|
|
|
|
var client = new Client("/tmp");
|
|
|
|
var usedPort = "";
|
|
|
|
var usedHost = "";
|
|
|
|
client.connection.connect = function(port, host) {
|
|
|
|
usedPort = port;
|
|
|
|
usedHost = host;
|
|
|
|
};
|
|
|
|
client.connect();
|
2014-03-18 17:05:55 +08:00
|
|
|
assert.equal(usedPort, "/tmp/.s.PGSQL." + pgport);
|
2014-06-16 05:33:23 +08:00
|
|
|
assert.strictEqual(usedHost, undefined);
|
|
|
|
});
|
2011-03-02 04:35:14 +08:00
|
|
|
|