@@ -4,6 +4,7 @@ if(helper.args.native) {
4
4
pg = require ( __dirname + '/../../../lib' ) . native ;
5
5
}
6
6
var ROWS_TO_INSERT = 1000 ;
7
+
7
8
var prepareTable = function ( client , callback ) {
8
9
client . query (
9
10
'CREATE TEMP TABLE copy_test (id SERIAL, name CHARACTER VARYING(10), age INT)' ,
@@ -13,8 +14,9 @@ var prepareTable = function (client, callback) {
13
14
} )
14
15
) ;
15
16
} ;
17
+
16
18
test ( 'COPY FROM' , function ( ) {
17
- pg . connect ( helper . config , function ( error , client ) {
19
+ pg . connect ( helper . config , assert . calls ( function ( error , client ) {
18
20
assert . equal ( error , null , "Failed to connect: " + helper . sys . inspect ( error ) ) ;
19
21
prepareTable ( client , function ( ) {
20
22
var stream = client . copyFrom ( "COPY copy_test (name, age) FROM stdin WITH CSV" ) ;
@@ -25,20 +27,21 @@ test('COPY FROM', function () {
25
27
stream . write ( String ( Date . now ( ) + Math . random ( ) ) . slice ( 0 , 10 ) + ',' + i + '\n' ) ;
26
28
}
27
29
assert . emits ( stream , 'close' , function ( ) {
28
- client . query ( "SELECT count(*), sum(age) from copy_test" , function ( err , result ) {
30
+ client . query ( "SELECT count(*), sum(age) from copy_test" , assert . calls ( function ( err , result ) {
29
31
assert . equal ( err , null , "Query should not fail" ) ;
30
32
assert . lengthIs ( result . rows , 1 )
31
33
assert . equal ( result . rows [ 0 ] . sum , ROWS_TO_INSERT * ( 0 + ROWS_TO_INSERT - 1 ) / 2 ) ;
32
34
assert . equal ( result . rows [ 0 ] . count , ROWS_TO_INSERT ) ;
33
35
pg . end ( helper . config ) ;
34
- } ) ;
36
+ } ) ) ;
35
37
} , "COPY FROM stream should emit close after query end" ) ;
36
38
stream . end ( ) ;
37
39
} ) ;
38
- } ) ;
40
+ } ) ) ;
39
41
} ) ;
42
+
40
43
test ( 'COPY TO' , function ( ) {
41
- pg . connect ( helper . config , function ( error , client ) {
44
+ pg . connect ( helper . config , assert . calls ( function ( error , client ) {
42
45
assert . equal ( error , null , "Failed to connect: " + helper . sys . inspect ( error ) ) ;
43
46
prepareTable ( client , function ( ) {
44
47
var stream = client . copyTo ( "COPY person (id, name, age) TO stdin WITH CSV" ) ;
@@ -56,10 +59,11 @@ test('COPY TO', function () {
56
59
pg . end ( helper . config ) ;
57
60
} , "COPY IN stream should emit end event after all rows" ) ;
58
61
} ) ;
59
- } ) ;
62
+ } ) ) ;
60
63
} ) ;
64
+
61
65
test ( 'COPY TO, queue queries' , function ( ) {
62
- pg . connect ( helper . config , function ( error , client ) {
66
+ pg . connect ( helper . config , assert . calls ( function ( error , client ) {
63
67
assert . equal ( error , null , "Failed to connect: " + helper . sys . inspect ( error ) ) ;
64
68
prepareTable ( client , function ( ) {
65
69
var query1Done = false ,
@@ -73,10 +77,10 @@ test('COPY TO, queue queries', function () {
73
77
//imitate long query, to make impossible,
74
78
//that copy query end callback runs after
75
79
//second query callback
76
- client . query ( "SELECT pg_sleep(5 )" , function ( ) {
80
+ client . query ( "SELECT pg_sleep(1 )" , assert . calls ( function ( ) {
77
81
query2Done = true ;
78
82
assert . ok ( copyQueryDone && query2Done , "second query has to be executed after others" ) ;
79
- } ) ;
83
+ } ) ) ;
80
84
var buf = new Buffer ( 0 ) ;
81
85
stream . on ( 'error' , function ( error ) {
82
86
assert . ok ( false , "COPY TO stream should not emit errors" + helper . sys . inspect ( error ) ) ;
@@ -93,15 +97,16 @@ test('COPY TO, queue queries', function () {
93
97
pg . end ( helper . config ) ;
94
98
} , "COPY IN stream should emit end event after all rows" ) ;
95
99
} ) ;
96
- } ) ;
100
+ } ) ) ;
97
101
} ) ;
102
+
98
103
test ( "COPY TO incorrect usage with large data" , function ( ) {
99
104
//when many data is loaded from database (and it takes a lot of time)
100
105
//there are chance, that query will be canceled before it ends
101
106
//but if there are not so much data, cancel message may be
102
107
//send after copy query ends
103
108
//so we need to test both situations
104
- pg . connect ( helper . config , function ( error , client ) {
109
+ pg . connect ( helper . config , assert . calls ( function ( error , client ) {
105
110
assert . equal ( error , null , "Failed to connect: " + helper . sys . inspect ( error ) ) ;
106
111
//intentionally incorrect usage of copy.
107
112
//this has to report error in standart way, instead of just throwing exception
@@ -116,10 +121,11 @@ test("COPY TO incorrect usage with large data", function () {
116
121
} ) ) ;
117
122
} )
118
123
) ;
119
- } ) ;
124
+ } ) ) ;
120
125
} ) ;
126
+
121
127
test ( "COPY TO incorrect usage with small data" , function ( ) {
122
- pg . connect ( helper . config , function ( error , client ) {
128
+ pg . connect ( helper . config , assert . calls ( function ( error , client ) {
123
129
assert . equal ( error , null , "Failed to connect: " + helper . sys . inspect ( error ) ) ;
124
130
//intentionally incorrect usage of copy.
125
131
//this has to report error in standart way, instead of just throwing exception
@@ -134,11 +140,11 @@ test("COPY TO incorrect usage with small data", function () {
134
140
} ) ) ;
135
141
} )
136
142
) ;
137
- } ) ;
143
+ } ) ) ;
138
144
} ) ;
139
145
140
146
test ( "COPY FROM incorrect usage" , function ( ) {
141
- pg . connect ( helper . config , function ( error , client ) {
147
+ pg . connect ( helper . config , assert . calls ( function ( error , client ) {
142
148
assert . equal ( error , null , "Failed to connect: " + helper . sys . inspect ( error ) ) ;
143
149
prepareTable ( client , function ( ) {
144
150
//intentionally incorrect usage of copy.
@@ -155,6 +161,6 @@ test("COPY FROM incorrect usage", function () {
155
161
} )
156
162
) ;
157
163
} ) ;
158
- } ) ;
164
+ } ) ) ;
159
165
} ) ;
160
166
0 commit comments