1
1
var helper = require ( './test-helper' ) ;
2
2
var Query = helper . pg . Query ;
3
3
4
- test ( "named prepared statement" , function ( ) {
4
+ var suite = new helper . Suite ( )
5
+
6
+ ; ( function ( ) {
5
7
6
8
var client = helper . client ( ) ;
7
9
client . on ( 'drain' , client . end . bind ( client ) ) ;
8
10
9
11
var queryName = "user by age and like name" ;
10
12
var parseCount = 0 ;
11
13
12
- test ( "first named prepared statement" , function ( ) {
14
+ suite . test ( "first named prepared statement" , function ( done ) {
13
15
var query = client . query ( new Query ( {
14
16
text : 'select name from person where age <= $1 and name LIKE $2' ,
15
17
values : [ 20 , 'Bri%' ] ,
@@ -20,11 +22,10 @@ test("named prepared statement", function() {
20
22
assert . equal ( row . name , 'Brian' ) ;
21
23
} ) ;
22
24
23
- assert . emits ( query , 'end' , function ( ) {
24
- } ) ;
25
+ query . on ( 'end' , ( ) => done ( ) )
25
26
} ) ;
26
27
27
- test ( "second named prepared statement with same name & text" , function ( ) {
28
+ suite . test ( "second named prepared statement with same name & text" , function ( done ) {
28
29
var cachedQuery = client . query ( new Query ( {
29
30
text : 'select name from person where age <= $1 and name LIKE $2' ,
30
31
name : queryName ,
@@ -35,11 +36,10 @@ test("named prepared statement", function() {
35
36
assert . equal ( row . name , 'Aaron' ) ;
36
37
} ) ;
37
38
38
- assert . emits ( cachedQuery , 'end' , function ( ) {
39
- } ) ;
39
+ cachedQuery . on ( 'end' , ( ) => done ( ) )
40
40
} ) ;
41
41
42
- test ( "with same name, but without query text" , function ( ) {
42
+ suite . test ( "with same name, but without query text" , function ( done ) {
43
43
var q = client . query ( new Query ( {
44
44
name : queryName ,
45
45
values : [ 30 , '%n%' ]
@@ -54,107 +54,92 @@ test("named prepared statement", function() {
54
54
} ) ;
55
55
} ) ;
56
56
57
- assert . emits ( q , 'end' , function ( ) { } ) ;
57
+ q . on ( 'end' , ( ) => done ( ) )
58
58
} ) ;
59
- } ) ;
59
+ } ) ( ) ;
60
60
61
- test ( "prepared statements on different clients" , function ( ) {
61
+ ; ( function ( ) {
62
62
var statementName = "differ" ;
63
63
var statement1 = "select count(*)::int4 as count from person" ;
64
64
var statement2 = "select count(*)::int4 as count from person where age < $1" ;
65
65
66
- var client1Finished = false ;
67
- var client2Finished = false ;
68
-
69
66
var client1 = helper . client ( ) ;
70
-
71
67
var client2 = helper . client ( ) ;
72
68
73
- test ( "client 1 execution" , function ( ) {
69
+ suite . test ( "client 1 execution" , function ( done ) {
74
70
75
71
var query = client1 . query ( {
76
72
name : statementName ,
77
73
text : statement1
78
74
} , ( err , res ) => {
79
75
assert ( ! err ) ;
80
76
assert . equal ( res . rows [ 0 ] . count , 26 ) ;
81
- if ( client2Finished ) {
82
- client1 . end ( ) ;
83
- client2 . end ( ) ;
84
- } else {
85
- client1Finished = true ;
86
- }
77
+ done ( )
87
78
} ) ;
88
-
89
79
} ) ;
90
80
91
- test ( 'client 2 execution' , function ( ) {
81
+ suite . test ( 'client 2 execution' , function ( done ) {
92
82
var query = client2 . query ( new Query ( {
93
83
name : statementName ,
94
84
text : statement2 ,
95
85
values : [ 11 ]
96
86
} ) ) ;
97
87
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 ) ;
102
90
} ) ;
103
91
104
92
assert . emits ( query , 'end' , function ( ) {
105
- if ( client1Finished ) {
106
- client1 . end ( ) ;
107
- client2 . end ( ) ;
108
- } else {
109
- client2Finished = true ;
110
- }
93
+ done ( ) ;
111
94
} ) ;
112
95
} ) ;
113
96
114
- } ) ;
97
+ suite . test ( 'clean up clients' , ( ) => {
98
+ return client1 . end ( ) . then ( ( ) => client2 . end ( ) )
99
+ } ) ;
100
+
101
+ } ) ( ) ;
115
102
116
- test ( 'prepared statement' , function ( ) {
103
+ ; ( function ( ) {
117
104
var client = helper . client ( ) ;
118
- client . on ( 'drain' , client . end . bind ( client ) ) ;
119
105
client . query ( 'CREATE TEMP TABLE zoom(name varchar(100));' ) ;
120
106
client . query ( "INSERT INTO zoom (name) VALUES ('zed')" ) ;
121
107
client . query ( "INSERT INTO zoom (name) VALUES ('postgres')" ) ;
122
108
client . query ( "INSERT INTO zoom (name) VALUES ('node postgres')" ) ;
123
109
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' ) ;
128
113
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' ) ;
131
116
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
+ } ) ;
137
121
} )
138
122
} ;
139
123
140
- test ( 'with small row count' , function ( ) {
124
+ suite . test ( 'with small row count' , function ( done ) {
141
125
var query = client . query ( new Query ( {
142
126
name : 'get names' ,
143
127
text : "SELECT name FROM zoom ORDER BY name" ,
144
128
rows : 1
145
- } ) ) ;
129
+ } , done ) ) ;
146
130
147
131
checkForResults ( query ) ;
148
132
149
133
} )
150
134
151
- test ( 'with large row count' , function ( ) {
135
+ suite . test ( 'with large row count' , function ( done ) {
152
136
var query = client . query ( new Query ( {
153
137
name : 'get names' ,
154
138
text : 'SELECT name FROM zoom ORDER BY name' ,
155
139
rows : 1000
156
- } ) )
140
+ } , done ) )
157
141
checkForResults ( query ) ;
158
142
} )
159
143
160
- } )
144
+ suite . test ( 'cleanup' , ( ) => client . end ( ) )
145
+ } ) ( )
0 commit comments