Skip to content

Commit

Permalink
throw if uneven number of columns
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jul 1, 2017
1 parent 3ae6621 commit 810465f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/spanner/src/transaction-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,18 @@ TransactionRequest.prototype.mutate_ = function(method, table, keyVals, cb) {

var columns = uniq(flatten(keyVals.map(Object.keys))).sort();

var values = keyVals.map(function(keyVal) {
var values = keyVals.map(function(keyVal, index) {
var keys = Object.keys(keyVal);

var missingColumns = columns.filter(column => keys.indexOf(column) === -1);

if (missingColumns.length > 0) {
throw new Error([
`Row at index ${index} does not contain the correct number of columns.`,
`Missing columns: ${JSON.stringify(missingColumns)}`
].join('\n\n'));
}

return columns.map(function(column) {
var value = keyVal[column];
return codec.encode(value);
Expand Down
25 changes: 25 additions & 0 deletions packages/spanner/test/transaction-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,31 @@ describe('TransactionRequest', function() {
assert.strictEqual(returnValue, requestReturnValue);
});

it('should throw when rows have incorrect amount of columns', function() {
var invalidEntry = { key1: 'val' };
var caughtError;

try {
transactionRequest.mutate_(METHOD, TABLE, [
invalidEntry,
{ key1: 'val', key2: 'val' }
], assert.ifError);
} catch(e) {
caughtError = e;
} finally {
if (!caughtError) {
throw new Error('Expected error was not thrown.');
}

var expectedErrorMessage = [
'Row at index 0 does not contain the correct number of columns.',
'Missing columns: ["key2"]'
].join('\n\n');

assert.strictEqual(caughtError.message, expectedErrorMessage);
}
});

it('should push the request to the queue if a transaction', function(done) {
transactionRequest.transaction = true;

Expand Down

0 comments on commit 810465f

Please sign in to comment.