Skip to content

Commit

Permalink
Generate insertId if not provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Nov 9, 2017
1 parent f10f61f commit 2e44af4
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 @@ -1007,6 +1007,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 @@ -1016,6 +1017,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 @@ -1035,11 +1038,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 @@ -1057,6 +1067,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 2e44af4

Please sign in to comment.