Initial commit

This commit is contained in:
Brian M. Carlson
2013-10-28 17:31:11 -05:00
commit d3d648a623
7 changed files with 284 additions and 0 deletions

45
test/copy-from.js Normal file
View File

@@ -0,0 +1,45 @@
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)

33
test/copy-to.js Normal file
View File

@@ -0,0 +1,33 @@
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
var txt = 'COPY (SELECT * from generate_series(0, ' + top + ')) TO STDOUT'
var stream = fromClient.query(copy(txt))
var done = gonna('finish piping out', 1000, function() {
fromClient.end()
})
stream.pipe(concat(function(buf) {
var res = buf.toString('utf8')
var expected = _.range(0, top+1).join('\n') + '\n'
assert.equal(res, expected)
done()
}))
}
testRange(10000)

2
test/index.js Normal file
View File

@@ -0,0 +1,2 @@
require('./copy-from')
require('./copy-to')