diff --git a/lib/bigquery/table.js b/lib/bigquery/table.js index 043ce5d0707..3f0e9e92809 100644 --- a/lib/bigquery/table.js +++ b/lib/bigquery/table.js @@ -752,7 +752,7 @@ Table.prototype.insert = function(rows, callback) { 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 diff --git a/test/bigquery/table.js b/test/bigquery/table.js index bae5e5c0faa..8fc3212a62e 100644 --- a/test/bigquery/table.js +++ b/test/bigquery/table.js @@ -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'); @@ -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(); + }); }); });