2013-10-29 06:31:11 +08:00
|
|
|
var assert = require('assert')
|
|
|
|
var gonna = require('gonna')
|
|
|
|
|
|
|
|
var concat = require('concat-stream')
|
|
|
|
var _ = require('lodash')
|
|
|
|
var pg = require('pg.js')
|
|
|
|
|
|
|
|
var testRange = function(top) {
|
|
|
|
var client = function() {
|
|
|
|
var client = new pg.Client()
|
|
|
|
client.connect()
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
var fromClient = client()
|
|
|
|
var copy = require('../').to
|
|
|
|
|
2013-10-29 10:27:15 +08:00
|
|
|
var txt = 'COPY (SELECT * from generate_series(0, ' + (top - 1) + ')) TO STDOUT'
|
2013-10-29 06:31:11 +08:00
|
|
|
|
|
|
|
var stream = fromClient.query(copy(txt))
|
2013-10-29 10:27:15 +08:00
|
|
|
var rowEmitCount = 0
|
|
|
|
stream.on('row', function() {
|
|
|
|
rowEmitCount++
|
|
|
|
})
|
2013-10-29 06:31:11 +08:00
|
|
|
var done = gonna('finish piping out', 1000, function() {
|
|
|
|
fromClient.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
stream.pipe(concat(function(buf) {
|
|
|
|
var res = buf.toString('utf8')
|
2013-10-29 10:27:15 +08:00
|
|
|
var expected = _.range(0, top).join('\n') + '\n'
|
2013-10-29 06:31:11 +08:00
|
|
|
assert.equal(res, expected)
|
2013-10-29 10:27:15 +08:00
|
|
|
assert.equal(rowEmitCount, top, 'should have emitted "row" ' + top + ' times but got ' + rowEmitCount)
|
2013-10-29 06:31:11 +08:00
|
|
|
done()
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
testRange(10000)
|