From 25af76e2f79ad3a1da9e859aefd9a6db942c2aea Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 13 Nov 2017 14:37:15 -0500 Subject: [PATCH] Generate insertId if not provided. (#2739) --- packages/bigquery/src/table.js | 1 + packages/bigquery/test/table.js | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/bigquery/src/table.js b/packages/bigquery/src/table.js index cdfc817df6c..67e0d67bfca 100644 --- a/packages/bigquery/src/table.js +++ b/packages/bigquery/src/table.js @@ -1020,6 +1020,7 @@ Table.prototype.insert = function(rows, options, callback) { if (!options.raw) { json.rows = arrify(rows).map(function(row) { return { + insertId: uuid.v4(), json: Table.encodeValue_(row) }; }); diff --git a/packages/bigquery/test/table.js b/packages/bigquery/test/table.js index 41da8edc05b..34b1334f6cb 100644 --- a/packages/bigquery/test/table.js +++ b/packages/bigquery/test/table.js @@ -63,7 +63,7 @@ var fakePaginator = { } }; -var fakeUuid = extend({}, uuid); +var fakeUuid = extend(true, {}, uuid); function FakeServiceObject() { this.calledWith_ = arguments; @@ -135,6 +135,7 @@ describe('BigQuery/Table', function() { }); beforeEach(function() { + fakeUuid = extend(fakeUuid, uuid); isCustomTypeOverride = null; makeWritableStreamOverride = null; tableOverrides = {}; @@ -1033,6 +1034,8 @@ describe('BigQuery/Table', function() { }); describe('insert', function() { + var fakeInsertId = 'fake-insert-id'; + var data = [ { state: 'MI', gender: 'M', year: '2015', name: 'Berkley', count: '0' }, { state: 'MI', gender: 'M', year: '2015', name: 'Berkley', count: '0' }, @@ -1052,11 +1055,18 @@ describe('BigQuery/Table', function() { var dataApiFormat = { rows: data.map(function(row) { return { + insertId: fakeInsertId, json: row }; }) }; + beforeEach(function() { + fakeUuid.v4 = function() { + return fakeInsertId; + }; + }); + it('should throw an error if rows is empty', function() { assert.throws(function() { table.insert([]); @@ -1074,6 +1084,15 @@ describe('BigQuery/Table', function() { table.insert(data, done); }); + it('should generate insertId', function(done) { + table.request = function(reqOpts) { + assert.strictEqual(reqOpts.json.rows[0].insertId, fakeInsertId); + done(); + }; + + table.insert([data[0]], done); + }); + it('should execute callback with API response', function(done) { var apiResponse = { insertErrors: [] };