1
- var assert = require ( 'assert' )
2
- var Cursor = require ( '../' )
3
- var pg = require ( 'pg' )
1
+ const assert = require ( 'assert' )
2
+ const Cursor = require ( '../' )
3
+ const pg = require ( 'pg' )
4
4
5
- var text = 'SELECT generate_series as num FROM generate_series(0, 5)'
5
+ const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
6
6
7
7
describe ( 'cursor' , function ( ) {
8
8
beforeEach ( function ( done ) {
9
- var client = ( this . client = new pg . Client ( ) )
9
+ const client = ( this . client = new pg . Client ( ) )
10
10
client . connect ( done )
11
11
12
12
this . pgCursor = function ( text , values ) {
13
- client . on ( 'drain' , client . end . bind ( client ) )
14
13
return client . query ( new Cursor ( text , values || [ ] ) )
15
14
}
16
15
} )
@@ -20,16 +19,16 @@ describe('cursor', function() {
20
19
} )
21
20
22
21
it ( 'fetch 6 when asking for 10' , function ( done ) {
23
- var cursor = this . pgCursor ( text )
22
+ const cursor = this . pgCursor ( text )
24
23
cursor . read ( 10 , function ( err , res ) {
25
24
assert . ifError ( err )
26
25
assert . equal ( res . length , 6 )
27
26
done ( )
28
27
} )
29
28
} )
30
29
31
- it ( 'end before reading to end' , function ( done ) {
32
- var cursor = this . pgCursor ( text )
30
+ it . only ( 'end before reading to end' , function ( done ) {
31
+ const cursor = this . pgCursor ( text )
33
32
cursor . read ( 3 , function ( err , res ) {
34
33
assert . ifError ( err )
35
34
assert . equal ( res . length , 3 )
@@ -38,15 +37,15 @@ describe('cursor', function() {
38
37
} )
39
38
40
39
it ( 'callback with error' , function ( done ) {
41
- var cursor = this . pgCursor ( 'select asdfasdf' )
40
+ const cursor = this . pgCursor ( 'select asdfasdf' )
42
41
cursor . read ( 1 , function ( err ) {
43
42
assert ( err )
44
43
done ( )
45
44
} )
46
45
} )
47
46
48
47
it ( 'read a partial chunk of data' , function ( done ) {
49
- var cursor = this . pgCursor ( text )
48
+ const cursor = this . pgCursor ( text )
50
49
cursor . read ( 2 , function ( err , res ) {
51
50
assert . ifError ( err )
52
51
assert . equal ( res . length , 2 )
@@ -68,7 +67,7 @@ describe('cursor', function() {
68
67
} )
69
68
70
69
it ( 'read return length 0 past the end' , function ( done ) {
71
- var cursor = this . pgCursor ( text )
70
+ const cursor = this . pgCursor ( text )
72
71
cursor . read ( 2 , function ( err ) {
73
72
assert ( ! err )
74
73
cursor . read ( 100 , function ( err , res ) {
@@ -85,11 +84,11 @@ describe('cursor', function() {
85
84
86
85
it ( 'read huge result' , function ( done ) {
87
86
this . timeout ( 10000 )
88
- var text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
89
- var values = [ ]
90
- var cursor = this . pgCursor ( text , values )
91
- var count = 0
92
- var read = function ( ) {
87
+ const text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
88
+ const values = [ ]
89
+ const cursor = this . pgCursor ( text , values )
90
+ let count = 0
91
+ const read = function ( ) {
93
92
cursor . read ( 100 , function ( err , rows ) {
94
93
if ( err ) return done ( err )
95
94
if ( ! rows . length ) {
@@ -107,9 +106,9 @@ describe('cursor', function() {
107
106
} )
108
107
109
108
it ( 'normalizes parameter values' , function ( done ) {
110
- var text = 'SELECT $1::json me'
111
- var values = [ { name : 'brian' } ]
112
- var cursor = this . pgCursor ( text , values )
109
+ const text = 'SELECT $1::json me'
110
+ const values = [ { name : 'brian' } ]
111
+ const cursor = this . pgCursor ( text , values )
113
112
cursor . read ( 1 , function ( err , rows ) {
114
113
if ( err ) return done ( err )
115
114
assert . equal ( rows [ 0 ] . me . name , 'brian' )
@@ -122,7 +121,7 @@ describe('cursor', function() {
122
121
} )
123
122
124
123
it ( 'returns result along with rows' , function ( done ) {
125
- var cursor = this . pgCursor ( text )
124
+ const cursor = this . pgCursor ( text )
126
125
cursor . read ( 1 , function ( err , rows , result ) {
127
126
assert . ifError ( err )
128
127
assert . equal ( rows . length , 1 )
@@ -133,7 +132,7 @@ describe('cursor', function() {
133
132
} )
134
133
135
134
it ( 'emits row events' , function ( done ) {
136
- var cursor = this . pgCursor ( text )
135
+ const cursor = this . pgCursor ( text )
137
136
cursor . read ( 10 )
138
137
cursor . on ( 'row' , ( row , result ) => result . addRow ( row ) )
139
138
cursor . on ( 'end' , result => {
@@ -143,7 +142,7 @@ describe('cursor', function() {
143
142
} )
144
143
145
144
it ( 'emits row events when cursor is closed manually' , function ( done ) {
146
- var cursor = this . pgCursor ( text )
145
+ const cursor = this . pgCursor ( text )
147
146
cursor . on ( 'row' , ( row , result ) => result . addRow ( row ) )
148
147
cursor . on ( 'end' , result => {
149
148
assert . equal ( result . rows . length , 3 )
@@ -154,19 +153,19 @@ describe('cursor', function() {
154
153
} )
155
154
156
155
it ( 'emits error events' , function ( done ) {
157
- var cursor = this . pgCursor ( 'select asdfasdf' )
156
+ const cursor = this . pgCursor ( 'select asdfasdf' )
158
157
cursor . on ( 'error' , function ( err ) {
159
158
assert ( err )
160
159
done ( )
161
160
} )
162
161
} )
163
162
164
163
it ( 'returns rowCount on insert' , function ( done ) {
165
- var pgCursor = this . pgCursor
164
+ const pgCursor = this . pgCursor
166
165
this . client
167
166
. query ( 'CREATE TEMPORARY TABLE pg_cursor_test (foo VARCHAR(1), bar VARCHAR(1))' )
168
167
. then ( function ( ) {
169
- var cursor = pgCursor ( 'insert into pg_cursor_test values($1, $2)' , [ 'a' , 'b' ] )
168
+ const cursor = pgCursor ( 'insert into pg_cursor_test values($1, $2)' , [ 'a' , 'b' ] )
170
169
cursor . read ( 1 , function ( err , rows , result ) {
171
170
assert . ifError ( err )
172
171
assert . equal ( rows . length , 0 )
0 commit comments