Skip to content

Commit

Permalink
Generate insertId if not provided. (#2739)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored Nov 13, 2017
1 parent cb85d5f commit 25af76e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/bigquery/src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
});
Expand Down
21 changes: 20 additions & 1 deletion packages/bigquery/test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var fakePaginator = {
}
};

var fakeUuid = extend({}, uuid);
var fakeUuid = extend(true, {}, uuid);

function FakeServiceObject() {
this.calledWith_ = arguments;
Expand Down Expand Up @@ -135,6 +135,7 @@ describe('BigQuery/Table', function() {
});

beforeEach(function() {
fakeUuid = extend(fakeUuid, uuid);
isCustomTypeOverride = null;
makeWritableStreamOverride = null;
tableOverrides = {};
Expand Down Expand Up @@ -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' },
Expand All @@ -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([]);
Expand All @@ -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: [] };

Expand Down

0 comments on commit 25af76e

Please sign in to comment.