Skip to content

Commit 3f8c632

Browse files
pass transaction instance in run()
1 parent 7ccc1b2 commit 3f8c632

File tree

2 files changed

+83
-62
lines changed

2 files changed

+83
-62
lines changed

lib/datastore/transaction.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ Transaction.prototype.commit = function(callback) {
201201
if (err) {
202202
// Rollback automatically for the user.
203203
self.rollback(function() {
204-
// Provide the response items from the failed commit to the user. Even
205-
// a failed rollback should be transparent.
204+
// Provide the error & API response from the failed commit to the user.
205+
// Even a failed rollback should be transparent.
206206
// RE: https://github.com/GoogleCloudPlatform/gcloud-node/pull/1369#discussion_r66833976
207207
callback(err, resp);
208208
});
@@ -330,7 +330,7 @@ Transaction.prototype.rollback = function(callback) {
330330
method: 'rollback'
331331
};
332332

333-
this.request_(protoOpts, {}, function(err, resp) {
333+
this.request_(protoOpts, function(err, resp) {
334334
self.skipCommit = true;
335335

336336
callback(err || null, resp);
@@ -344,10 +344,12 @@ Transaction.prototype.rollback = function(callback) {
344344
* @param {function} callback - The function to execute within the context of
345345
* a transaction.
346346
* @param {?error} callback.err - An error returned while making this request.
347+
* @param {module:datastore/transaction} callback.transaction - This transaction
348+
* instance.
347349
* @param {object} callback.apiResponse - The full API response.
348350
*
349351
* @example
350-
* transaction.run(function(err) {
352+
* transaction.run(function(err, transaction) {
351353
* // Perform Datastore transactional operations.
352354
* var key = datastore.key(['Company', 123]);
353355
*
@@ -377,15 +379,15 @@ Transaction.prototype.run = function(callback) {
377379
method: 'beginTransaction'
378380
};
379381

380-
this.request_(protoOpts, {}, function(err, resp) {
382+
this.request_(protoOpts, function(err, resp) {
381383
if (err) {
382-
callback(err, resp);
384+
callback(err, null, resp);
383385
return;
384386
}
385387

386388
self.id = resp.transaction;
387389

388-
callback(null, resp);
390+
callback(null, self, resp);
389391
});
390392
};
391393

test/datastore/transaction.js

+74-55
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,6 @@ describe('Transaction', function() {
119119
});
120120
});
121121

122-
describe('createQuery', function() {
123-
it('should return query from datastore.createQuery', function() {
124-
var args = [0, 1, 2, 3];
125-
var createQueryReturnValue = {};
126-
127-
transaction.datastore.createQuery = function() {
128-
assert.strictEqual(this, transaction);
129-
assert.strictEqual(arguments[0], args[0]);
130-
assert.strictEqual(arguments[1], args[1]);
131-
assert.strictEqual(arguments[2], args[2]);
132-
assert.strictEqual(arguments[3], args[3]);
133-
return createQueryReturnValue;
134-
};
135-
136-
var query = transaction.createQuery.apply(transaction, args);
137-
assert.strictEqual(query, createQueryReturnValue);
138-
});
139-
});
140-
141122
describe('commit', function() {
142123
beforeEach(function() {
143124
transaction.id = TRANSACTION_ID;
@@ -165,9 +146,12 @@ describe('Transaction', function() {
165146
var error = new Error('Error.');
166147
var apiResponse = {};
167148

149+
var rollbackError = new Error('Error.');
150+
var rollbackApiResponse = {};
151+
168152
beforeEach(function() {
169153
transaction.rollback = function(callback) {
170-
callback();
154+
callback(rollbackError, rollbackApiResponse);
171155
};
172156

173157
transaction.request_ = function(protoOpts, reqOpts, callback) {
@@ -176,7 +160,7 @@ describe('Transaction', function() {
176160
};
177161
});
178162

179-
it('should pass the error to the callback', function(done) {
163+
it('should pass the commit error to the callback', function(done) {
180164
transaction.commit(function(err, resp) {
181165
assert.strictEqual(err, error);
182166
assert.strictEqual(resp, apiResponse);
@@ -314,6 +298,25 @@ describe('Transaction', function() {
314298
});
315299
});
316300

301+
describe('createQuery', function() {
302+
it('should return query from datastore.createQuery', function() {
303+
var args = [0, 1, 2, 3];
304+
var createQueryReturnValue = {};
305+
306+
transaction.datastore.createQuery = function() {
307+
assert.strictEqual(this, transaction);
308+
assert.strictEqual(arguments[0], args[0]);
309+
assert.strictEqual(arguments[1], args[1]);
310+
assert.strictEqual(arguments[2], args[2]);
311+
assert.strictEqual(arguments[3], args[3]);
312+
return createQueryReturnValue;
313+
};
314+
315+
var query = transaction.createQuery.apply(transaction, args);
316+
assert.strictEqual(query, createQueryReturnValue);
317+
});
318+
});
319+
317320
describe('delete', function() {
318321
it('should push entities into a queue', function() {
319322
var keys = [
@@ -397,51 +400,67 @@ describe('Transaction', function() {
397400
});
398401

399402
describe('run', function() {
400-
it('should begin', function(done) {
401-
transaction.request_ = function(protoOpts, reqOpts, callback) {
402-
callback = callback || reqOpts;
403-
assert.strictEqual(protoOpts.service, 'Datastore');
404-
assert.equal(protoOpts.method, 'beginTransaction');
403+
it('should make the correct API request', function(done) {
404+
transaction.request_ = function(protoOpts) {
405+
assert.deepEqual(protoOpts, {
406+
service: 'Datastore',
407+
method: 'beginTransaction'
408+
});
409+
405410
done();
406411
};
407412

408-
transaction.run();
413+
transaction.run(assert.ifError);
409414
});
410415

411-
it('should set transaction id', function(done) {
412-
transaction.request_ = function(protoOpts, reqOpts, callback) {
413-
callback = callback || reqOpts;
414-
callback(null, { transaction: TRANSACTION_ID });
415-
};
416-
transaction.run(function(err) {
417-
assert.ifError(err);
418-
assert.equal(transaction.id, TRANSACTION_ID);
419-
done();
416+
describe('error', function() {
417+
var error = new Error('Error.');
418+
var apiResponse = {};
419+
420+
beforeEach(function() {
421+
transaction.request_ = function(protoOpts, callback) {
422+
callback(error, apiResponse);
423+
};
420424
});
421-
});
422425

423-
it('should pass error to callback', function(done) {
424-
var error = new Error('Error.');
425-
transaction.request_ = function(protoOpts, reqOpts, callback) {
426-
callback = callback || reqOpts;
427-
callback(error);
428-
};
429-
transaction.run(function(err) {
430-
assert.deepEqual(err, error);
431-
done();
426+
it('should pass error & API response to callback', function(done) {
427+
transaction.run(function(err, transaction, apiResponse_) {
428+
assert.strictEqual(err, error);
429+
assert.strictEqual(transaction, null);
430+
assert.strictEqual(apiResponse_, apiResponse);
431+
done();
432+
});
432433
});
433434
});
434435

435-
it('should pass apiResponse to callback', function(done) {
436-
var resp = { success: true };
437-
transaction.request_ = function(protoOpts, reqOpts, callback) {
438-
callback = callback || reqOpts;
439-
callback(null, resp);
436+
describe('success', function() {
437+
var apiResponse = {
438+
transaction: TRANSACTION_ID
440439
};
441-
transaction.run(function(err, apiResponse) {
442-
assert.ifError(err);
443-
assert.deepEqual(resp, apiResponse);
444-
done();
440+
441+
beforeEach(function() {
442+
transaction.request_ = function(protoOpts, callback) {
443+
callback(null, apiResponse);
444+
};
445+
});
446+
447+
it('should set transaction id', function(done) {
448+
delete transaction.id;
449+
450+
transaction.run(function(err) {
451+
assert.ifError(err);
452+
assert.equal(transaction.id, TRANSACTION_ID);
453+
done();
454+
});
455+
});
456+
457+
it('should exec callback with Transaction & apiResponse', function(done) {
458+
transaction.run(function(err, transaction_, apiResponse_) {
459+
assert.ifError(err);
460+
assert.strictEqual(transaction_, transaction);
461+
assert.deepEqual(apiResponse_, apiResponse);
462+
done();
463+
});
445464
});
446465
});
447466
});

0 commit comments

Comments
 (0)