Skip to content

Commit

Permalink
bigquery#insert: return & test API errors. fixes googleapis#336
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Dec 22, 2014
1 parent cde8c45 commit be2a2ec
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/bigquery/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,11 @@ Table.prototype.insert = function(rows, callback) {
return;
}

console.log(JSON.stringify(resp, null, 2))

var failedToInsert = (resp.insertErrors || []).map(function(insertError) {
return {
error: insertError.errors.map(function(error) {
errors: insertError.errors.map(function(error) {
return {
message: error.message,
reason: error.reason
Expand Down
55 changes: 54 additions & 1 deletion test/bigquery/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'use strict';

var assert = require('assert');
var crypto = require('crypto');
var extend = require('extend');
var File = require('../../lib/storage/file');
var Stream = require('stream');
Expand Down Expand Up @@ -801,12 +802,64 @@ describe('BigQuery/Table', function() {
{ state: 'MI', gender: 'M', year: '2015', name: 'Berkley', count: '0' }
];

var dataApiFormat = {
rows: data.map(function(row) {
var rowObject = {};
var md5 = crypto.createHash('md5');
md5.update(JSON.stringify(row));
rowObject.insertId = md5.digest('hex');
rowObject.json = row;
return rowObject;
})
};

it('should save data', function(done) {
table.makeReq_ = function(method, path, query, body) {
assert.equal(method, 'POST');
assert.equal(path, '/insertAll');
assert.strictEqual(query, null);
assert.deepEqual(body, dataApiFormat);
done();
};

table.insert(data, done);
});

it('should execute callback', function(done) {
table.makeReq_ = function(method, path, query, body, callback) {
callback(null, { insertErrors: [] });
};

table.insert(data, done);
table.insert(data, function(err, insertErrors) {
assert.ifError(err);
assert.deepEqual(insertErrors, []);
done();
});
});

it('should return errors to the callback', function(done) {
var row0Error = { message: 'Error.', reason: 'notFound' };
var row1Error = { message: 'Error.', reason: 'notFound' };

table.makeReq_ = function(method, path, query, body, callback) {
callback(null, {
insertErrors: [
{ index: 0, errors: [row0Error] },
{ index: 1, errors: [row1Error] }
]
});
};

table.insert(data, function(err, insertErrors) {
assert.ifError(err);

assert.deepEqual(insertErrors, [
{ row: dataApiFormat.rows[0].json, errors: [row0Error] },
{ row: dataApiFormat.rows[1].json, errors: [row1Error] }
]);

done();
});
});
});

Expand Down

0 comments on commit be2a2ec

Please sign in to comment.