Skip to content

Commit

Permalink
Fixes #234
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Nov 7, 2016
1 parent 2cf8055 commit 48b80c5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 86 deletions.
133 changes: 53 additions & 80 deletions datastore/concepts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

'use strict';

const assert = require('power-assert');
var asyncUtil = require('async');
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
Expand All @@ -29,7 +30,7 @@ module.exports = {
};

// This mock is used in the documentation snippets.
var datastore = {
let datastore = {
delete: function () {},
get: function () {},
insert: function () {},
Expand Down Expand Up @@ -1032,44 +1033,37 @@ Query.prototype.testCursorPaging = function (callback) {

Query.prototype.testEventualConsistentQuery = function () {
// [START eventual_consistent_query]
// Read consistency cannot be specified in gcloud-node.
// Read consistency cannot be specified in google-cloud-node.
// [END eventual_consistent_query]
};

// [START transactional_update]
function transferFunds (fromKey, toKey, amount, callback) {
var transaction = datastore.transaction();

transaction.run(function (err) {
if (err) {
return callback(err);
}

transaction.get([
fromKey,
toKey
], function (err, accounts) {
if (err) {
return transaction.rollback(function (_err) {
return callback(_err || err);
});
}

accounts[0].data.balance -= amount;
accounts[1].data.balance += amount;

transaction.save(accounts);

transaction.commit(function (err) {
if (err) {
return callback(err);
function transferFunds (fromKey, toKey, amount) {
const transaction = datastore.transaction();

return transaction.run()
.then(() => Promise.all([transaction.get(fromKey), transaction.get(toKey)]))
.then((results) => {
const accounts = results
.map((result) => result[0]);

accounts[0].balance -= amount;
accounts[1].balance += amount;

transaction.save([
{
key: fromKey,
data: accounts[0]
},
{
key: toKey,
data: accounts[1]
}
]);

// The transaction completed successfully.
callback();
});
});
});
return transaction.commit();
})
.catch(() => transaction.rollback());
}
// [END transactional_update]

Expand All @@ -1087,8 +1081,8 @@ function Transaction (projectId) {
this.amountToTransfer = 10;
}

Transaction.prototype.restoreBankAccountBalances = function (config, callback) {
var saveArray = config.keys.map(function (key) {
Transaction.prototype.restoreBankAccountBalances = function (config) {
const entities = config.keys.map((key) => {
return {
key: key,
data: {
Expand All @@ -1097,60 +1091,39 @@ Transaction.prototype.restoreBankAccountBalances = function (config, callback) {
};
});

this.datastore.save(saveArray, callback);
console.log(entities);

return this.datastore.save(entities);
};

Transaction.prototype.testTransactionalUpdate = function (callback) {
var self = this;
Transaction.prototype.testTransactionalUpdate = function () {
const fromKey = this.fromKey;
const toKey = this.toKey;
const originalBalance = this.originalBalance;
const amountToTransfer = this.amountToTransfer;
const datastoreMock = datastore;

var fromKey = this.fromKey;
var toKey = this.toKey;
var originalBalance = this.originalBalance;
var amountToTransfer = this.amountToTransfer;
// Overwrite so the real Datastore instance is used in `transferFunds`.
datastore = this.datastore;

this.restoreBankAccountBalances({
return this.restoreBankAccountBalances({
keys: [fromKey, toKey],
balance: originalBalance
}, function (err) {
if (err) {
callback(err);
return;
}

// Overwrite so the real Datastore instance is used in `transferFunds`.
var datastoreMock = datastore;
datastore = self.datastore;

transferFunds(fromKey, toKey, amountToTransfer, function (err) {
})
.then(() => transferFunds(fromKey, toKey, amountToTransfer))
.then(() => Promise.all([this.datastore.get(fromKey), this.datastore.get(toKey)]))
.then((results) => {
const accounts = results.map((result) => result[0]);
// Restore `datastore` to the mock API.
datastore = datastoreMock;

if (err) {
callback(err);
return;
}

self.datastore.get([
fromKey,
toKey
], function (err, accounts) {
if (err) {
callback(err);
return;
}

var transactionWasSuccessful =
accounts[0].data.balance === originalBalance - amountToTransfer &&
accounts[1].data.balance === originalBalance + amountToTransfer;

if (!transactionWasSuccessful) {
callback(new Error('Accounts were not updated successfully.'));
} else {
callback();
}
});
assert.equal(accounts[0].balance, originalBalance - amountToTransfer);
assert.equal(accounts[1].balance, originalBalance + amountToTransfer);
})
.catch((err) => {
// Restore `datastore` to the mock API.
datastore = datastoreMock;
return Promise.reject(err);
});
});
};

Transaction.prototype.testTransactionalRetry = function (callback) {
Expand Down
8 changes: 4 additions & 4 deletions datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
},
"dependencies": {
"@google-cloud/datastore": "^0.1.1",
"async": "^2.0.1",
"yargs": "^5.0.0"
"@google-cloud/datastore": "^0.5.0",
"async": "^2.1.2",
"yargs": "^6.3.0"
},
"devDependencies": {
"mocha": "^3.0.2"
"mocha": "^3.1.2"
},
"engines": {
"node": ">=4.3.2"
Expand Down
4 changes: 2 additions & 2 deletions datastore/system-test/concepts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe('datastore:concepts', function () {

// Transactions

it('performs a transactional update', function (done) {
transaction.testTransactionalUpdate(done);
it(`performs a transactional update`, () => {
return transaction.testTransactionalUpdate();
});

it('performs retries if necessary', function (done) {
Expand Down

0 comments on commit 48b80c5

Please sign in to comment.