node-pg-copy-streams/test/copy-from.js
Brian C c4a0e6dd58 Fix compatibility with newer versions of node (#39)
* Eliminate detach/reattach strategy as it isn't able to differentiate between on/once and inconsistenly loses unshifted data depending on node version.  Instead just split stream and send it to copy stream and the connection.stream at the same time.  Disconnecting copy stream just means unpiping.  Added handleCopyData to fulfill query contract but ignore the incoming data.

Add node 4.2.2 to Travis
Minimum postgres 9.2 to allow tests to complete in Travis

Remove test that is no longer needed since we no longer disconnect/reconnect listeners

* Add resume

* Remove node 0.10 and add 0.12

* Re-enable old tests

* Add more versions to the travis test matrix
2016-05-03 13:20:04 -05:00

61 lines
1.7 KiB
JavaScript

var assert = require('assert')
var gonna = require('gonna')
var concat = require('concat-stream')
var _ = require('lodash')
var pg = require('pg')
var copy = require('../').from
var client = function() {
var client = new pg.Client()
client.connect()
return client
}
var testConstruction = function() {
var highWaterMark = 10
var stream = copy('COPY numbers FROM STDIN', {highWaterMark: 10, objectMode: true})
for(var i = 0; i < highWaterMark * 1.5; i++) {
stream.write('1\t2\n')
}
assert(!stream.write('1\t2\n'), 'Should correctly set highWaterMark.')
}
testConstruction()
var testRange = function(top) {
var fromClient = client()
fromClient.query('CREATE TEMP TABLE numbers(num int, bigger_num int)')
var txt = 'COPY numbers FROM STDIN'
var stream = fromClient.query(copy(txt))
var rowEmitCount = 0
stream.on('row', function() {
rowEmitCount++
})
for(var i = 0; i < top; i++) {
stream.write(Buffer('' + i + '\t' + i*10 + '\n'))
}
stream.end()
var countDone = gonna('have correct count')
stream.on('end', function() {
fromClient.query('SELECT COUNT(*) FROM numbers', function(err, res) {
assert.ifError(err)
assert.equal(res.rows[0].count, top, 'expected ' + top + ' rows but got ' + res.rows[0].count)
//console.log('found ', res.rows.length, 'rows')
countDone()
var firstRowDone = gonna('have correct result')
assert.equal(stream.rowCount, top, 'should have rowCount ' + top + ' ')
fromClient.query('SELECT (max(num)) AS num FROM numbers', function(err, res) {
assert.ifError(err)
assert.equal(res.rows[0].num, top-1)
firstRowDone()
fromClient.end()
})
})
})
}
testRange(1000)