diff --git a/test/binary.js b/test/binary.js new file mode 100644 index 0000000..f1d6e0e --- /dev/null +++ b/test/binary.js @@ -0,0 +1,65 @@ +var assert = require('assert') +var gonna = require('gonna') + +var async = require('async') +var concat = require('concat-stream') +var _ = require('lodash') +var pg = require('pg.js') + +var from = require('../').from +var to = require('../').to + +var testBinaryCopy = function() { + var client = function() { + var client = new pg.Client() + client.connect() + return client + } + + var fromClient = client() + var toClient = client() + + queries = [ + 'CREATE TABLE data (num BIGINT, word TEXT)', + 'INSERT INTO data (num, word) VALUES (1, \'hello\'), (2, \'other thing\'), (3, \'goodbye\')', + 'CREATE TABLE data_copy (LIKE data INCLUDING ALL)' + ] + + async.eachSeries(queries, _.bind(fromClient.query, fromClient), function(err) { + assert.ifError(err) + + var fromStream = fromClient.query(to('COPY (SELECT * FROM data) TO STDOUT BINARY')) + var toStream = toClient.query(from('COPY data_copy FROM STDIN BINARY')) + + runStream = function(callback) { + fromStream.on('error', callback) + toStream.on('error', callback) + toStream.on('finish', callback) + fromStream.pipe(toStream) + } + runStream(function(err) { + assert.ifError(err) + + toClient.query('SELECT * FROM data_copy ORDER BY num', function(err, res){ + assert.equal(res.rowCount, 3, 'expected 3 rows but got ' + res.rowCount) + assert.equal(res.rows[0].num, 1) + assert.equal(res.rows[0].word, 'hello') + assert.equal(res.rows[1].num, 2) + assert.equal(res.rows[1].word, 'other thing') + assert.equal(res.rows[2].num, 3) + assert.equal(res.rows[2].word, 'goodbye') + queries = [ + 'DROP TABLE data', + 'DROP TABLE data_copy' + ] + async.each(queries, _.bind(fromClient.query, fromClient), function(err) { + assert.ifError(err) + fromClient.end() + toClient.end() + }) + }) + }) + }) +} + +testBinaryCopy() diff --git a/test/index.js b/test/index.js index 21c5ea9..0985e40 100644 --- a/test/index.js +++ b/test/index.js @@ -1,2 +1,3 @@ require('./copy-from') require('./copy-to') +require('./binary')