|
| 1 | +var helper = require(__dirname + '/../test-helper'); |
| 2 | +var pg = require(__dirname + '/../../../lib'); |
| 3 | +if(helper.args.native) { |
| 4 | + pg = require(__dirname + '/../../../lib').native; |
| 5 | +} |
| 6 | +var ROWS_TO_INSERT = 1000; |
| 7 | +var prepareTable = function (client, callback) { |
| 8 | + client.query( |
| 9 | + 'CREATE TEMP TABLE copy_test (id SERIAL, name CHARACTER VARYING(10), age INT)', |
| 10 | + assert.calls(function (err, result) { |
| 11 | + assert.equal(err, null, "create table query should not fail"); |
| 12 | + callback(); |
| 13 | + }) |
| 14 | + ); |
| 15 | +}; |
| 16 | +test('COPY FROM', function () { |
| 17 | + pg.connect(helper.config, function (error, client) { |
| 18 | + assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error)); |
| 19 | + prepareTable(client, function () { |
| 20 | + var stream = client.copyFrom("COPY copy_test (name, age) FROM stdin WITH CSV"); |
| 21 | + stream.on('error', function (error) { |
| 22 | + assert.ok(false, "COPY FROM stream should not emit errors" + helper.sys.inspect(error)); |
| 23 | + }); |
| 24 | + for (var i = 0; i < ROWS_TO_INSERT; i++) { |
| 25 | + stream.write( String(Date.now() + Math.random()).slice(0,10) + ',' + i + '\n'); |
| 26 | + } |
| 27 | + assert.emits(stream, 'close', function () { |
| 28 | + client.query("SELECT count(*), sum(age) from copy_test", function (err, result) { |
| 29 | + assert.equal(err, null, "Query should not fail"); |
| 30 | + assert.lengthIs(result.rows, 1) |
| 31 | + assert.equal(result.rows[0].sum, ROWS_TO_INSERT * (0 + ROWS_TO_INSERT -1)/2); |
| 32 | + assert.equal(result.rows[0].count, ROWS_TO_INSERT); |
| 33 | + pg.end(helper.config); |
| 34 | + }); |
| 35 | + }, "COPY FROM stream should emit close after query end"); |
| 36 | + stream.end(); |
| 37 | + }); |
| 38 | + }); |
| 39 | +}); |
| 40 | +test('COPY TO', function () { |
| 41 | + pg.connect(helper.config, function (error, client) { |
| 42 | + assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error)); |
| 43 | + prepareTable(client, function () { |
| 44 | + var stream = client.copyTo("COPY person (id, name, age) TO stdin WITH CSV"); |
| 45 | + var buf = new Buffer(0); |
| 46 | + stream.on('error', function (error) { |
| 47 | + assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error)); |
| 48 | + }); |
| 49 | + assert.emits(stream, 'data', function (chunk) { |
| 50 | + buf = Buffer.concat([buf, chunk]); |
| 51 | + }, "COPY IN stream should emit data event for each row"); |
| 52 | + assert.emits(stream, 'end', function () { |
| 53 | + var lines = buf.toString().split('\n'); |
| 54 | + assert.equal(lines.length >= 0, true, "copy in should return rows saved by copy from"); |
| 55 | + assert.equal(lines[0].split(',').length, 3, "each line should consists of 3 fields"); |
| 56 | + pg.end(helper.config); |
| 57 | + }, "COPY IN stream should emit end event after all rows"); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
0 commit comments