1
+ "use strict" ;
2
+
1
3
var helper = require ( './test-helper' ) ;
2
4
var util = require ( 'util' ) ;
3
5
4
6
var pg = helper . pg
5
7
6
-
7
8
var createErorrClient = function ( ) {
8
9
var client = helper . client ( ) ;
9
10
client . once ( 'error' , function ( err ) {
10
- //console.log('error', util.inspect(err));
11
11
assert . fail ( 'Client shoud not throw error during query execution' ) ;
12
12
} ) ;
13
13
client . on ( 'drain' , client . end . bind ( client ) ) ;
14
14
return client ;
15
15
} ;
16
16
17
- test ( 'error handling' , function ( ) {
18
- test ( 'within a simple query' , function ( ) {
19
- var client = createErorrClient ( ) ;
20
-
21
- var query = client . query ( new pg . Query ( "select eeeee from yodas_dsflsd where pixistix = 'zoiks!!!'" ) ) ;
17
+ const suite = new helper . Suite ( 'error handling' )
22
18
23
- assert . emits ( query , 'error' , function ( error ) {
24
- assert . equal ( error . severity , "ERROR" ) ;
25
- } ) ;
19
+ suite . test ( 'query receives error on client shutdown' , false , function ( done ) {
20
+ var client = new Client ( ) ;
21
+ client . connect ( function ( err ) {
22
+ if ( err ) {
23
+ return done ( err )
24
+ }
25
+ client . query ( 'SELECT pg_sleep(5)' , assert . calls ( function ( err , res ) {
26
+ assert ( err instanceof Error )
27
+ done ( )
28
+ } ) ) ;
29
+ setTimeout ( ( ) => {
30
+ client . end ( )
31
+ assert . emits ( client , 'end' ) ;
32
+ } , 50 )
26
33
} ) ;
34
+ } ) ;
27
35
28
- test ( 'within a prepared statement' , function ( ) {
29
-
30
- var client = createErorrClient ( ) ;
31
-
32
- var q = client . query ( { text : "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);" , binary : false } ) ;
36
+ suite . test ( 'within a simple query' , ( done ) => {
37
+ var client = createErorrClient ( ) ;
33
38
34
- test ( "when query is parsing" , function ( ) {
39
+ var query = client . query ( new pg . Query ( "select eeeee from yodas_dsflsd where pixistix = 'zoiks!!!'" ) ) ;
35
40
36
- //this query wont parse since there ain't no table named bang
41
+ assert . emits ( query , 'error' , function ( error ) {
42
+ assert . equal ( error . severity , "ERROR" ) ;
43
+ done ( ) ;
44
+ } ) ;
45
+ } ) ;
37
46
38
- var ensureFuture = function ( testClient ) {
39
- test ( "client can issue more queries successfully" , function ( ) {
40
- var goodQuery = testClient . query ( new pg . Query ( "select age from boom" ) ) ;
41
- assert . emits ( goodQuery , 'row' , function ( row ) {
42
- assert . equal ( row . age , 28 ) ;
43
- } ) ;
44
- } ) ;
45
- } ;
47
+ ( function ( ) {
48
+ var client = createErorrClient ( ) ;
46
49
47
- var query = client . query ( new pg . Query ( {
48
- text : "select * from bang where name = $1" ,
49
- values : [ '0' ]
50
- } ) ) ;
50
+ var q = client . query ( { text : "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);" , binary : false } ) ;
51
51
52
- test ( "query emits the error" , function ( ) {
53
- assert . emits ( query , 'error' , function ( err ) {
54
- ensureFuture ( client ) ;
55
- } ) ;
56
- } ) ;
52
+ var ensureFuture = function ( testClient , done ) {
53
+ var goodQuery = testClient . query ( new pg . Query ( "select age from boom" ) ) ;
54
+ assert . emits ( goodQuery , 'row' , function ( row ) {
55
+ assert . equal ( row . age , 28 ) ;
56
+ done ( ) ;
57
+ } ) ;
58
+ } ;
57
59
58
- test ( "when a query is binding " , function ( ) {
60
+ suite . test ( "when query is parsing " , ( done ) => {
59
61
60
- var query = client . query ( new pg . Query ( {
61
- text : 'select * from boom where age = $1' ,
62
- values : [ 'asldkfjasdf' ]
63
- } ) ) ;
62
+ //this query wont parse since there isn't a table named bang
63
+ var query = client . query ( new pg . Query ( {
64
+ text : "select * from bang where name = $1" ,
65
+ values : [ '0' ]
66
+ } ) ) ;
64
67
65
- test ( "query emits the error" , function ( ) {
68
+ assert . emits ( query , 'error' , function ( err ) {
69
+ ensureFuture ( client , done ) ;
70
+ } ) ;
71
+ } ) ;
66
72
67
- assert . emits ( query , 'error' , function ( err ) {
68
- test ( 'error has right severity' , function ( ) {
69
- assert . equal ( err . severity , "ERROR" ) ;
70
- } )
73
+ suite . test ( "when a query is binding" , function ( done ) {
71
74
72
- ensureFuture ( client ) ;
73
- } ) ;
74
- } ) ;
75
+ var query = client . query ( new pg . Query ( {
76
+ text : 'select * from boom where age = $1' ,
77
+ values : [ 'asldkfjasdf' ]
78
+ } ) ) ;
75
79
76
- //TODO how to test for errors during execution?
77
- } ) ;
80
+ assert . emits ( query , 'error' , function ( err ) {
81
+ assert . equal ( err . severity , "ERROR" ) ;
82
+ ensureFuture ( client , done ) ;
78
83
} ) ;
79
84
} ) ;
85
+ } ) ( ) ;
80
86
81
- test ( 'non-query error' , function ( ) {
82
- var client = new Client ( {
83
- user :'asldkfjsadlfkj'
84
- } ) ;
85
- assert . emits ( client , 'error' ) ;
86
- client . connect ( ) ;
87
+ suite . test ( 'non-query error' , function ( done ) {
88
+ var client = new Client ( {
89
+ user :'asldkfjsadlfkj'
87
90
} ) ;
88
-
89
- test ( 'non-query error with callback' , function ( ) {
90
- var client = new Client ( {
91
- user :'asldkfjsadlfkj'
92
- } ) ;
93
- client . connect ( assert . calls ( function ( error , client ) {
94
- assert . ok ( error ) ;
95
- } ) ) ;
91
+ client . on ( 'error' , ( err ) => {
92
+ assert ( err instanceof Error )
93
+ done ( )
96
94
} ) ;
95
+ client . connect ( ) ;
96
+ } ) ;
97
97
98
+ suite . test ( 'non-query error with callback' , function ( done ) {
99
+ var client = new Client ( {
100
+ user :'asldkfjsadlfkj'
101
+ } ) ;
102
+ client . connect ( assert . calls ( function ( error , client ) {
103
+ assert ( error instanceof Error )
104
+ done ( )
105
+ } ) ) ;
98
106
} ) ;
99
107
100
- test ( 'non-error calls supplied callback' , function ( ) {
108
+ suite . test ( 'non-error calls supplied callback' , function ( done ) {
101
109
var client = new Client ( {
102
110
user : helper . args . user ,
103
111
password : helper . args . password ,
@@ -108,75 +116,23 @@ test('non-error calls supplied callback', function() {
108
116
109
117
client . connect ( assert . calls ( function ( err ) {
110
118
assert . ifError ( err ) ;
111
- client . end ( ) ;
119
+ client . end ( done ) ;
112
120
} ) )
113
121
} ) ;
114
122
115
- test ( 'when connecting to invalid host' , function ( ) {
116
- //this test fails about 30% on travis and only on travis...
117
- //I'm not sure what the cause could be
118
- if ( process . env . TRAVIS ) return false ;
119
-
123
+ suite . test ( 'when connecting to invalid host with promise' , function ( done ) {
120
124
var client = new Client ( {
121
- user : 'aslkdjfsdf' ,
122
- password : '1234' ,
123
- host : 'asldkfjasdf!!#1308140.com'
125
+ host : 'asdlfkjasldkfjlaskdfj'
124
126
} ) ;
125
-
126
- var delay = 5000 ;
127
- var tid = setTimeout ( function ( ) {
128
- var msg = "When connecting to an invalid host the error event should be emitted but it has been " + delay + " and still no error event."
129
- assert ( false , msg ) ;
130
- } , delay ) ;
131
- client . on ( 'error' , function ( ) {
132
- clearTimeout ( tid ) ;
133
- } )
134
- client . connect ( ) ;
127
+ client . connect ( ) . catch ( ( e ) => done ( ) ) ;
135
128
} ) ;
136
129
137
- test ( 'when connecting to invalid host with callback' , function ( ) {
130
+ suite . test ( 'when connecting to an invalid host with callback' , function ( done ) {
138
131
var client = new Client ( {
139
- user : 'brian' ,
140
- password : '1234' ,
141
132
host : 'asldkfjasdf!!#1308140.com'
142
133
} ) ;
143
134
client . connect ( function ( error , client ) {
144
- assert ( error ) ;
135
+ assert ( error instanceof Error ) ;
136
+ done ( ) ;
145
137
} ) ;
146
138
} ) ;
147
-
148
- test ( 'multiple connection errors (gh#31)' , function ( ) {
149
- return false ;
150
- test ( 'with single client' , function ( ) {
151
- //don't run yet...this test fails...need to think of fix
152
- var client = new Client ( {
153
- user : 'blaksdjf' ,
154
- password : 'omfsadfas' ,
155
- host : helper . args . host ,
156
- port : helper . args . port ,
157
- database : helper . args . database
158
- } ) ;
159
- client . connect ( ) ;
160
- assert . emits ( client , 'error' , function ( e ) {
161
- client . connect ( ) ;
162
- assert . emits ( client , 'error' ) ;
163
- } ) ;
164
- } ) ;
165
-
166
- test ( 'with callback method' , function ( ) {
167
- var badConString = "postgres://aslkdfj:oi14081@" + helper . args . host + ":" + helper . args . port + "/" + helper . args . database ;
168
- return false ;
169
- } ) ;
170
- } ) ;
171
-
172
- test ( 'query receives error on client shutdown' , function ( ) {
173
- var client = new Client ( helper . config ) ;
174
- client . connect ( assert . calls ( function ( ) {
175
- client . query ( 'SELECT pg_sleep(5)' , assert . calls ( function ( err , res ) {
176
- assert ( err ) ;
177
- } ) ) ;
178
- client . end ( ) ;
179
- assert . emits ( client , 'end' ) ;
180
- } ) ) ;
181
- } ) ;
182
-
0 commit comments