1+ "use strict" ;
2+
13var helper = require ( './test-helper' ) ;
24var util = require ( 'util' ) ;
35
46var pg = helper . pg
57
6-
78var createErorrClient = function ( ) {
89 var client = helper . client ( ) ;
910 client . once ( 'error' , function ( err ) {
10- //console.log('error', util.inspect(err));
1111 assert . fail ( 'Client shoud not throw error during query execution' ) ;
1212 } ) ;
1313 client . on ( 'drain' , client . end . bind ( client ) ) ;
1414 return client ;
1515} ;
1616
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' )
2218
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 )
2633 } ) ;
34+ } ) ;
2735
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 ( ) ;
3338
34- test ( "when query is parsing" , function ( ) {
39+ var query = client . query ( new pg . Query ( "select eeeee from yodas_dsflsd where pixistix = 'zoiks!!!'" ) ) ;
3540
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+ } ) ;
3746
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 ( ) ;
4649
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 } ) ;
5151
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+ } ;
5759
58- test ( "when a query is binding " , function ( ) {
60+ suite . test ( "when query is parsing " , ( done ) => {
5961
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+ } ) ) ;
6467
65- test ( "query emits the error" , function ( ) {
68+ assert . emits ( query , 'error' , function ( err ) {
69+ ensureFuture ( client , done ) ;
70+ } ) ;
71+ } ) ;
6672
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 ) {
7174
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+ } ) ) ;
7579
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 ) ;
7883 } ) ;
7984 } ) ;
85+ } ) ( ) ;
8086
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'
8790 } ) ;
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 ( )
9694 } ) ;
95+ client . connect ( ) ;
96+ } ) ;
9797
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+ } ) ) ;
98106} ) ;
99107
100- test ( 'non-error calls supplied callback' , function ( ) {
108+ suite . test ( 'non-error calls supplied callback' , function ( done ) {
101109 var client = new Client ( {
102110 user : helper . args . user ,
103111 password : helper . args . password ,
@@ -108,75 +116,23 @@ test('non-error calls supplied callback', function() {
108116
109117 client . connect ( assert . calls ( function ( err ) {
110118 assert . ifError ( err ) ;
111- client . end ( ) ;
119+ client . end ( done ) ;
112120 } ) )
113121} ) ;
114122
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 ) {
120124 var client = new Client ( {
121- user : 'aslkdjfsdf' ,
122- password : '1234' ,
123- host : 'asldkfjasdf!!#1308140.com'
125+ host : 'asdlfkjasldkfjlaskdfj'
124126 } ) ;
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 ( ) ) ;
135128} ) ;
136129
137- test ( 'when connecting to invalid host with callback' , function ( ) {
130+ suite . test ( 'when connecting to an invalid host with callback' , function ( done ) {
138131 var client = new Client ( {
139- user : 'brian' ,
140- password : '1234' ,
141132 host : 'asldkfjasdf!!#1308140.com'
142133 } ) ;
143134 client . connect ( function ( error , client ) {
144- assert ( error ) ;
135+ assert ( error instanceof Error ) ;
136+ done ( ) ;
145137 } ) ;
146138} ) ;
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