node-pg-copy-streams/test/copy-from.js
Brian M. Carlson d3d648a623 Initial commit
2013-10-28 17:31:11 -05:00

46 lines
1.2 KiB
JavaScript

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()
client.query('CREATE TEMP TABLE numbers(num int, bigger_num int)')
return client
}
var fromClient = client()
var copy = require('../').from
var txt = 'COPY numbers FROM STDIN'
var stream = fromClient.query(copy(txt))
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')
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)