11var helper = require ( './test-helper' ) ;
22var Query = helper . pg . Query ;
33
4- test ( "named prepared statement" , function ( ) {
4+ var suite = new helper . Suite ( )
5+
6+ ; ( function ( ) {
57
68 var client = helper . client ( ) ;
79 client . on ( 'drain' , client . end . bind ( client ) ) ;
810
911 var queryName = "user by age and like name" ;
1012 var parseCount = 0 ;
1113
12- test ( "first named prepared statement" , function ( ) {
14+ suite . test ( "first named prepared statement" , function ( done ) {
1315 var query = client . query ( new Query ( {
1416 text : 'select name from person where age <= $1 and name LIKE $2' ,
1517 values : [ 20 , 'Bri%' ] ,
@@ -20,11 +22,10 @@ test("named prepared statement", function() {
2022 assert . equal ( row . name , 'Brian' ) ;
2123 } ) ;
2224
23- assert . emits ( query , 'end' , function ( ) {
24- } ) ;
25+ query . on ( 'end' , ( ) => done ( ) )
2526 } ) ;
2627
27- test ( "second named prepared statement with same name & text" , function ( ) {
28+ suite . test ( "second named prepared statement with same name & text" , function ( done ) {
2829 var cachedQuery = client . query ( new Query ( {
2930 text : 'select name from person where age <= $1 and name LIKE $2' ,
3031 name : queryName ,
@@ -35,11 +36,10 @@ test("named prepared statement", function() {
3536 assert . equal ( row . name , 'Aaron' ) ;
3637 } ) ;
3738
38- assert . emits ( cachedQuery , 'end' , function ( ) {
39- } ) ;
39+ cachedQuery . on ( 'end' , ( ) => done ( ) )
4040 } ) ;
4141
42- test ( "with same name, but without query text" , function ( ) {
42+ suite . test ( "with same name, but without query text" , function ( done ) {
4343 var q = client . query ( new Query ( {
4444 name : queryName ,
4545 values : [ 30 , '%n%' ]
@@ -54,107 +54,92 @@ test("named prepared statement", function() {
5454 } ) ;
5555 } ) ;
5656
57- assert . emits ( q , 'end' , function ( ) { } ) ;
57+ q . on ( 'end' , ( ) => done ( ) )
5858 } ) ;
59- } ) ;
59+ } ) ( ) ;
6060
61- test ( "prepared statements on different clients" , function ( ) {
61+ ; ( function ( ) {
6262 var statementName = "differ" ;
6363 var statement1 = "select count(*)::int4 as count from person" ;
6464 var statement2 = "select count(*)::int4 as count from person where age < $1" ;
6565
66- var client1Finished = false ;
67- var client2Finished = false ;
68-
6966 var client1 = helper . client ( ) ;
70-
7167 var client2 = helper . client ( ) ;
7268
73- test ( "client 1 execution" , function ( ) {
69+ suite . test ( "client 1 execution" , function ( done ) {
7470
7571 var query = client1 . query ( {
7672 name : statementName ,
7773 text : statement1
7874 } , ( err , res ) => {
7975 assert ( ! err ) ;
8076 assert . equal ( res . rows [ 0 ] . count , 26 ) ;
81- if ( client2Finished ) {
82- client1 . end ( ) ;
83- client2 . end ( ) ;
84- } else {
85- client1Finished = true ;
86- }
77+ done ( )
8778 } ) ;
88-
8979 } ) ;
9080
91- test ( 'client 2 execution' , function ( ) {
81+ suite . test ( 'client 2 execution' , function ( done ) {
9282 var query = client2 . query ( new Query ( {
9383 name : statementName ,
9484 text : statement2 ,
9585 values : [ 11 ]
9686 } ) ) ;
9787
98- test ( 'gets right data' , function ( ) {
99- assert . emits ( query , 'row' , function ( row ) {
100- assert . equal ( row . count , 1 ) ;
101- } ) ;
88+ assert . emits ( query , 'row' , function ( row ) {
89+ assert . equal ( row . count , 1 ) ;
10290 } ) ;
10391
10492 assert . emits ( query , 'end' , function ( ) {
105- if ( client1Finished ) {
106- client1 . end ( ) ;
107- client2 . end ( ) ;
108- } else {
109- client2Finished = true ;
110- }
93+ done ( ) ;
11194 } ) ;
11295 } ) ;
11396
114- } ) ;
97+ suite . test ( 'clean up clients' , ( ) => {
98+ return client1 . end ( ) . then ( ( ) => client2 . end ( ) )
99+ } ) ;
100+
101+ } ) ( ) ;
115102
116- test ( 'prepared statement' , function ( ) {
103+ ; ( function ( ) {
117104 var client = helper . client ( ) ;
118- client . on ( 'drain' , client . end . bind ( client ) ) ;
119105 client . query ( 'CREATE TEMP TABLE zoom(name varchar(100));' ) ;
120106 client . query ( "INSERT INTO zoom (name) VALUES ('zed')" ) ;
121107 client . query ( "INSERT INTO zoom (name) VALUES ('postgres')" ) ;
122108 client . query ( "INSERT INTO zoom (name) VALUES ('node postgres')" ) ;
123109
124- var checkForResults = function ( q ) {
125- test ( 'row callback fires for each result' , function ( ) {
126- assert . emits ( q , 'row' , function ( row ) {
127- assert . equal ( row . name , 'node postgres' ) ;
110+ var checkForResults = function ( q ) {
111+ assert . emits ( q , 'row' , function ( row ) {
112+ assert . equal ( row . name , 'node postgres' ) ;
128113
129- assert . emits ( q , 'row' , function ( row ) {
130- assert . equal ( row . name , 'postgres' ) ;
114+ assert . emits ( q , 'row' , function ( row ) {
115+ assert . equal ( row . name , 'postgres' ) ;
131116
132- assert . emits ( q , 'row' , function ( row ) {
133- assert . equal ( row . name , 'zed' ) ;
134- } )
135- } ) ;
136- } )
117+ assert . emits ( q , 'row' , function ( row ) {
118+ assert . equal ( row . name , 'zed' ) ;
119+ } )
120+ } ) ;
137121 } )
138122 } ;
139123
140- test ( 'with small row count' , function ( ) {
124+ suite . test ( 'with small row count' , function ( done ) {
141125 var query = client . query ( new Query ( {
142126 name : 'get names' ,
143127 text : "SELECT name FROM zoom ORDER BY name" ,
144128 rows : 1
145- } ) ) ;
129+ } , done ) ) ;
146130
147131 checkForResults ( query ) ;
148132
149133 } )
150134
151- test ( 'with large row count' , function ( ) {
135+ suite . test ( 'with large row count' , function ( done ) {
152136 var query = client . query ( new Query ( {
153137 name : 'get names' ,
154138 text : 'SELECT name FROM zoom ORDER BY name' ,
155139 rows : 1000
156- } ) )
140+ } , done ) )
157141 checkForResults ( query ) ;
158142 } )
159143
160- } )
144+ suite . test ( 'cleanup' , ( ) => client . end ( ) )
145+ } ) ( )
0 commit comments