@@ -58,4 +58,41 @@ test('COPY TO', function () {
5858 } ) ;
5959 } ) ;
6060} ) ;
61+ test ( 'COPY TO, queue queries' , function ( ) {
62+ pg . connect ( helper . config , function ( error , client ) {
63+ assert . equal ( error , null , "Failed to connect: " + helper . sys . inspect ( error ) ) ;
64+ prepareTable ( client , function ( ) {
65+ var query1Done = false ,
66+ copyQueryDone = false ,
67+ query2Done = false ;
68+ client . query ( "SELECT count(*) from person" , function ( ) {
69+ query1Done = true ;
70+ assert . ok ( ! copyQueryDone && ! query2Done , "first query has to be executed before others" ) ;
71+ } ) ;
72+ var stream = client . copyTo ( "COPY person (id, name, age) TO stdin WITH CSV" ) ;
73+ //imitate long query, to make impossible,
74+ //that copy query end callback runs after
75+ //second query callback
76+ client . query ( "SELECT pg_sleep(5)" , function ( ) {
77+ query2Done = true ;
78+ assert . ok ( copyQueryDone && query2Done , "second query has to be executed after others" ) ;
79+ } ) ;
80+ var buf = new Buffer ( 0 ) ;
81+ stream . on ( 'error' , function ( error ) {
82+ assert . ok ( false , "COPY TO stream should not emit errors" + helper . sys . inspect ( error ) ) ;
83+ } ) ;
84+ assert . emits ( stream , 'data' , function ( chunk ) {
85+ buf = Buffer . concat ( [ buf , chunk ] ) ;
86+ } , "COPY IN stream should emit data event for each row" ) ;
87+ assert . emits ( stream , 'end' , function ( ) {
88+ copyQueryDone = true ;
89+ assert . ok ( query1Done && ! query2Done , "copy query has to be executed before second query and after first" ) ;
90+ var lines = buf . toString ( ) . split ( '\n' ) ;
91+ assert . equal ( lines . length >= 0 , true , "copy in should return rows saved by copy from" ) ;
92+ assert . equal ( lines [ 0 ] . split ( ',' ) . length , 3 , "each line should consists of 3 fields" ) ;
93+ pg . end ( helper . config ) ;
94+ } , "COPY IN stream should emit end event after all rows" ) ;
95+ } ) ;
96+ } ) ;
97+ } ) ;
6198
0 commit comments