@@ -6,25 +6,23 @@ var text = 'SELECT generate_series as num FROM generate_series(0, 5)'
6
6
7
7
describe ( 'cursor' , function ( ) {
8
8
9
- var client ;
9
+ beforeEach ( function ( done ) {
10
+ var client = this . client = new pg . Client ( )
11
+ client . connect ( done )
10
12
11
- var pgCursor = function ( text , values ) {
12
- client . connect ( )
13
- client . on ( 'drain' , client . end . bind ( client ) )
14
- return client . query ( new Cursor ( text , values || [ ] ) )
15
- }
16
-
17
- before ( function ( ) {
18
- client = new pg . Client ( )
13
+ this . pgCursor = function ( text , values ) {
14
+ client . on ( 'drain' , client . end . bind ( client ) )
15
+ return client . query ( new Cursor ( text , values || [ ] ) )
16
+ }
19
17
} )
20
18
21
19
22
- after ( function ( ) {
23
- client . end ( )
20
+ afterEach ( function ( ) {
21
+ this . client . end ( )
24
22
} )
25
23
26
24
it ( 'fetch 6 when asking for 10' , function ( done ) {
27
- var cursor = pgCursor ( text )
25
+ var cursor = this . pgCursor ( text )
28
26
cursor . read ( 10 , function ( err , res ) {
29
27
assert . ifError ( err )
30
28
assert . equal ( res . length , 6 )
@@ -33,7 +31,7 @@ describe('cursor', function() {
33
31
} )
34
32
35
33
it ( 'end before reading to end' , function ( done ) {
36
- var cursor = pgCursor ( text )
34
+ var cursor = this . pgCursor ( text )
37
35
cursor . read ( 3 , function ( err , res ) {
38
36
assert . ifError ( err )
39
37
assert . equal ( res . length , 3 )
@@ -42,7 +40,7 @@ describe('cursor', function() {
42
40
} )
43
41
44
42
it ( 'callback with error' , function ( done ) {
45
- var cursor = pgCursor ( 'select asdfasdf' )
43
+ var cursor = this . pgCursor ( 'select asdfasdf' )
46
44
cursor . read ( 1 , function ( err ) {
47
45
assert ( err )
48
46
done ( )
@@ -51,7 +49,7 @@ describe('cursor', function() {
51
49
52
50
53
51
it ( 'read a partial chunk of data' , function ( done ) {
54
- var cursor = pgCursor ( text )
52
+ var cursor = this . pgCursor ( text )
55
53
cursor . read ( 2 , function ( err , res ) {
56
54
assert . ifError ( err )
57
55
assert . equal ( res . length , 2 )
@@ -70,7 +68,7 @@ describe('cursor', function() {
70
68
} )
71
69
72
70
it ( 'read return length 0 past the end' , function ( done ) {
73
- var cursor = pgCursor ( text )
71
+ var cursor = this . pgCursor ( text )
74
72
cursor . read ( 2 , function ( err , res ) {
75
73
cursor . read ( 100 , function ( err , res ) {
76
74
assert . equal ( res . length , 4 )
@@ -84,19 +82,19 @@ describe('cursor', function() {
84
82
85
83
it ( 'read huge result' , function ( done ) {
86
84
this . timeout ( 10000 )
87
- var text = 'SELECT generate_series as num FROM generate_series(0, 1000000 )'
85
+ var text = 'SELECT generate_series as num FROM generate_series(0, 100000 )'
88
86
var values = [ ]
89
- cursor = pgCursor ( text , values ) ;
87
+ cursor = this . pgCursor ( text , values ) ;
90
88
var count = 0 ;
91
89
var read = function ( ) {
92
- cursor . read ( 1000 , function ( err , rows ) {
90
+ cursor . read ( 100 , function ( err , rows ) {
93
91
if ( err ) return done ( err ) ;
94
92
if ( ! rows . length ) {
95
- assert . equal ( count , 1000001 )
93
+ assert . equal ( count , 100001 )
96
94
return done ( )
97
95
}
98
96
count += rows . length ;
99
- if ( count % 100000 == 0 ) {
97
+ if ( count % 10000 == 0 ) {
100
98
//console.log(count)
101
99
}
102
100
setImmediate ( read )
0 commit comments