-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconfiguration-tests.js
158 lines (135 loc) · 4.68 KB
/
configuration-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict'
require(__dirname + '/test-helper')
var pguser = process.env['PGUSER'] || process.env.USER
var pgdatabase = process.env['PGDATABASE'] || process.env.USER
var pgport = process.env['PGPORT'] || 5432
test('client settings', function () {
test('defaults', function () {
var client = new Client()
assert.equal(client.user, pguser)
assert.equal(client.database, pgdatabase)
assert.equal(client.port, pgport)
assert.equal(client.ssl, false)
})
test('custom', function () {
var user = 'brian'
var database = 'pgjstest'
var password = 'boom'
var client = new Client({
user: user,
database: database,
port: 321,
password: password,
ssl: true
})
assert.equal(client.user, user)
assert.equal(client.database, database)
assert.equal(client.port, 321)
assert.equal(client.password, password)
assert.equal(client.ssl, true)
})
test('custom ssl default on', function () {
var old = process.env.PGSSLMODE
process.env.PGSSLMODE = 'prefer'
var client = new Client()
process.env.PGSSLMODE = old
assert.equal(client.ssl, true)
})
test('custom ssl force off', function () {
var old = process.env.PGSSLMODE
process.env.PGSSLMODE = 'prefer'
var client = new Client({
ssl: false
})
process.env.PGSSLMODE = old
assert.equal(client.ssl, false)
})
})
test('initializing from a config string', function () {
test('uses connectionString property', function () {
var client = new Client({
connectionString: 'postgres://brian:pass@host1:333/databasename'
})
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')
})
test('uses the correct values from the config string', function () {
var client = new Client('postgres://brian:pass@host1:333/databasename')
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')
})
test('when not including all values the defaults are used', function () {
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'])
if (envUserDefined) {
process.env['PGUSER'] = savedEnvUser
} else {
delete process.env['PGUSER']
}
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']
}
})
})
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()
assert.equal(usedPort, '/tmp/.s.PGSQL.' + pgport)
assert.strictEqual(usedHost, undefined)
})