@@ -36,33 +36,88 @@ test('initializing from a config string', function() {
36
36
37
37
test ( 'uses the correct values from the config string' , function ( ) {
38
38
var client = new Client ( "postgres://brian:pass@host1:333/databasename" )
39
- assert . equal ( client . user , 'brian' )
40
- assert . equal ( client . password , "pass" )
41
- assert . equal ( client . host , "host1" )
42
- assert . equal ( client . port , 333 )
43
- assert . equal ( client . database , "databasename" )
44
- } )
39
+ assert . equal ( client . user , 'brian' ) ;
40
+ assert . equal ( client . password , "pass" ) ;
41
+ assert . equal ( client . host , "host1" ) ;
42
+ assert . equal ( client . port , 333 ) ;
43
+ assert . equal ( client . database , "databasename" ) ;
44
+ } ) ;
45
45
46
46
test ( 'uses the correct values from the config string with space in password' , function ( ) {
47
47
var client = new Client ( "postgres://brian:pass word@host1:333/databasename" )
48
- assert . equal ( client . user , 'brian' )
49
- assert . equal ( client . password , "pass word" )
50
- assert . equal ( client . host , "host1" )
51
- assert . equal ( client . port , 333 )
52
- assert . equal ( client . database , "databasename" )
53
- } )
48
+ assert . equal ( client . user , 'brian' ) ;
49
+ assert . equal ( client . password , "pass word" ) ;
50
+ assert . equal ( client . host , "host1" ) ;
51
+ assert . equal ( client . port , 333 ) ;
52
+ assert . equal ( client . database , "databasename" ) ;
53
+ } ) ;
54
54
55
55
test ( 'when not including all values the defaults are used' , function ( ) {
56
- var client = new Client ( "postgres://host1" )
57
- assert . equal ( client . user , process . env [ 'PGUSER' ] || process . env . USER )
58
- assert . equal ( client . password , process . env [ 'PGPASSWORD' ] || null )
59
- assert . equal ( client . host , "host1" )
60
- assert . equal ( client . port , process . env [ 'PGPORT' ] || 5432 )
61
- assert . equal ( client . database , process . env [ 'PGDATABASE' ] || process . env . USER )
62
- } )
56
+ var client = new Client ( "postgres://host1" ) ;
57
+ assert . equal ( client . user , process . env [ 'PGUSER' ] || process . env . USER ) ;
58
+ assert . equal ( client . password , process . env [ 'PGPASSWORD' ] || null ) ;
59
+ assert . equal ( client . host , "host1" ) ;
60
+ assert . equal ( client . port , process . env [ 'PGPORT' ] || 5432 ) ;
61
+ assert . equal ( client . database , process . env [ 'PGDATABASE' ] || process . env . USER ) ;
62
+ } ) ;
63
+
64
+ test ( 'when not including all values the environment variables are used' , function ( ) {
65
+ var envUserDefined = process . env [ 'PGUSER' ] !== undefined ;
66
+ var envPasswordDefined = process . env [ 'PGPASSWORD' ] !== undefined ;
67
+ var envDBDefined = process . env [ 'PGDATABASE' ] !== undefined ;
68
+ var envHostDefined = process . env [ 'PGHOST' ] !== undefined ;
69
+ var envPortDefined = process . env [ 'PGPORT' ] !== undefined ;
70
+
71
+ var savedEnvUser = process . env [ 'PGUSER' ] ;
72
+ var savedEnvPassword = process . env [ 'PGPASSWORD' ] ;
73
+ var savedEnvDB = process . env [ 'PGDATABASE' ] ;
74
+ var savedEnvHost = process . env [ 'PGHOST' ] ;
75
+ var savedEnvPort = process . env [ 'PGPORT' ] ;
76
+
77
+ process . env [ 'PGUSER' ] = 'utUser1' ;
78
+ process . env [ 'PGPASSWORD' ] = 'utPass1' ;
79
+ process . env [ 'PGDATABASE' ] = 'utDB1' ;
80
+ process . env [ 'PGHOST' ] = 'utHost1' ;
81
+ process . env [ 'PGPORT' ] = 5464 ;
82
+
83
+ var client = new Client ( "postgres://host1" ) ;
84
+ assert . equal ( client . user , process . env [ 'PGUSER' ] ) ;
85
+ assert . equal ( client . password , process . env [ 'PGPASSWORD' ] ) ;
86
+ assert . equal ( client . host , "host1" ) ;
87
+ assert . equal ( client . port , process . env [ 'PGPORT' ] ) ;
88
+ assert . equal ( client . database , process . env [ 'PGDATABASE' ] ) ;
63
89
90
+ if ( envUserDefined ) {
91
+ process . env [ 'PGUSER' ] = savedEnvUser ;
92
+ } else {
93
+ delete process . env [ 'PGUSER' ] ;
94
+ }
64
95
65
- } )
96
+ if ( envPasswordDefined ) {
97
+ process . env [ 'PGPASSWORD' ] = savedEnvPassword ;
98
+ } else {
99
+ delete process . env [ 'PGPASSWORD' ] ;
100
+ }
101
+
102
+ if ( envDBDefined ) {
103
+ process . env [ 'PGDATABASE' ] = savedEnvDB ;
104
+ } else {
105
+ delete process . env [ 'PGDATABASE' ] ;
106
+ }
107
+
108
+ if ( envHostDefined ) {
109
+ process . env [ 'PGHOST' ] = savedEnvHost ;
110
+ } else {
111
+ delete process . env [ 'PGHOST' ] ;
112
+ }
113
+
114
+ if ( envPortDefined ) {
115
+ process . env [ 'PGPORT' ] = savedEnvPort ;
116
+ } else {
117
+ delete process . env [ 'PGPORT' ] ;
118
+ }
119
+ } ) ;
120
+ } ) ;
66
121
67
122
test ( 'calls connect correctly on connection' , function ( ) {
68
123
var client = new Client ( "/tmp" ) ;
@@ -74,6 +129,6 @@ test('calls connect correctly on connection', function() {
74
129
} ;
75
130
client . connect ( ) ;
76
131
assert . equal ( usedPort , "/tmp/.s.PGSQL." + pgport ) ;
77
- assert . strictEqual ( usedHost , undefined )
78
- } )
132
+ assert . strictEqual ( usedHost , undefined ) ;
133
+ } ) ;
79
134
0 commit comments