Skip to content

Commit

Permalink
refactor + unit tests + system test
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jun 30, 2017
1 parent 1e58fd6 commit d3f1d5d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
2 changes: 2 additions & 0 deletions packages/spanner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"@google-cloud/common": "^0.13.0",
"@google-cloud/common-grpc": "^0.3.0",
"array-uniq": "^1.0.3",
"arrify": "^1.0.1",
"checkpoint-stream": "^0.1.0",
"events-intercept": "^2.0.0",
Expand All @@ -44,6 +45,7 @@
"google-proto-files": "^0.12.0",
"is": "^3.1.0",
"lodash.chunk": "^4.2.0",
"lodash.flatten": "^4.4.0",
"lodash.snakecase": "^4.1.1",
"merge-stream": "^1.0.1",
"methmeth": "^1.1.0",
Expand Down
36 changes: 11 additions & 25 deletions packages/spanner/src/transaction-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
var arrify = require('arrify');
var common = require('@google-cloud/common');
var extend = require('extend');
var flatten = require('lodash.flatten');
var is = require('is');
var uniq = require('array-uniq');

/**
* @type {module:spanner/codec}
Expand Down Expand Up @@ -682,33 +684,17 @@ TransactionRequest.prototype.upsert = function(table, keyVals, callback) {
TransactionRequest.prototype.mutate_ = function(method, table, keyVals, cb) {
keyVals = arrify(keyVals);

var mutation = {};

var columns = keyVals.reduce(function(allKeys, keyVal) {
var keys = Object.keys(keyVal);
var key;

for (var i = 0, ii = keys.length; i < ii; ++i) {
key = keys[i];

if (allKeys.indexOf(key) === -1) {
allKeys.push(key);
}
}

return allKeys;
}, []).sort();
var columns = uniq(flatten(keyVals.map(Object.keys))).sort();

mutation[method] = {
table: table,
columns: columns,
values: keyVals.map(function(keyVal) {
return columns.map(function(key) {
var value = keyVal[key];
var values = keyVals.map(function(keyVal) {
return columns.map(function(key) {
var value = keyVal[key];
return codec.encode(is.undefined(value) ? null : value);
});
});

return codec.encode(value === undefined ? null : value);
});
})
var mutation = {
[method]: { table, columns, values }
};

if (this.transaction) {
Expand Down
16 changes: 16 additions & 0 deletions packages/spanner/system-test/spanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ var spanner = new Spanner(env);
}, execAfterOperationComplete(done));
});

describe('uneven rows', function() {
it('should allow inserting uneven rows', function(done) {
table.insert([
{
Key: generateName('id'),
BoolValue: true
},
{
Key: generateName('id'),
BoolValue: true,
IntValue: 10
}
], done);
});
});

describe('structs', function() {
it('should correctly decode structs', function(done) {
var query = 'SELECT ARRAY(SELECT as struct 1, "hello")';
Expand Down
52 changes: 40 additions & 12 deletions packages/spanner/test/transaction-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,26 +759,30 @@ describe('TransactionRequest', function() {
describe('mutate_', function() {
var METHOD = 'methodName';
var TABLE = 'table-name';
var KEYVALS = { key: 'value' };

var ENCODED_VALUE = {
encoded: true
};
var KEYVALS = [
{ key: 'value', nullable: true },
{ key: 'value' } // missing nullable value
];

var EXPECTED_MUTATION = {};
EXPECTED_MUTATION[METHOD] = {
table: TABLE,
columns: Object.keys(KEYVALS),
columns: ['key', 'nullable'],
values: [
[
ENCODED_VALUE
KEYVALS[0].key,
KEYVALS[0].nullable
],
[
KEYVALS[1].key,
null
]
]
};

beforeEach(function() {
fakeCodec.encode = function() {
return ENCODED_VALUE;
fakeCodec.encode = function(value) {
return value;
};
});

Expand All @@ -787,9 +791,33 @@ describe('TransactionRequest', function() {

function callback() {}

fakeCodec.encode = function(key) {
assert.strictEqual(key, KEYVALS[Object.keys(KEYVALS)[0]]);
return ENCODED_VALUE;
var numEncodeRequests = 0;
fakeCodec.encode = function(value) {
numEncodeRequests++;

switch (numEncodeRequests) {
case 1: {
assert.strictEqual(value, KEYVALS[0].key);
break;
}

case 2: {
assert.strictEqual(value, KEYVALS[0].nullable);
break;
}

case 3: {
assert.strictEqual(value, KEYVALS[1].key);
break;
}

case 4: {
assert.strictEqual(value, null);
break;
}
}

return value;
};

var expectedReqOpts = {
Expand Down

0 comments on commit d3f1d5d

Please sign in to comment.