Skip to content

Commit

Permalink
feat: support doomed transaction scope on test cases (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
xujihui1985 authored and fengmk2 committed Nov 19, 2018
1 parent 6f362c1 commit b227bc1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,41 @@ proto.beginTransactionScope = function* (scope, ctx) {
}
};

/**
* doomed to be rollbacked after transaction scope
* useful on writing test that depend on database
*
* @param {Function} scope - scope with code
* @param {Object} [ctx] - transaction env context, like koa's ctx.
* To make sure only one active transaction on this ctx.
* @return {Object} - scope return result
*/
proto.beginDoomedTransactionScope = function* (scope, ctx) {
ctx = ctx || {};
if (!ctx._transactionConnection) {
ctx._transactionConnection = yield this.beginTransaction();
ctx._transactionScopeCount = 1;
} else {
ctx._transactionScopeCount++;
}
const tran = ctx._transactionConnection;
try {
const result = yield scope(tran);
ctx._transactionScopeCount--;
if (ctx._transactionScopeCount === 0) {
ctx._transactionConnection = null;
}
return result;
} catch (err) {
if (ctx._transactionConnection) {
ctx._transactionConnection = null;
}
throw err;
} finally {
yield tran.rollback();
}
};

proto.end = function(callback) {
// callback style
if (callback) {
Expand Down
28 changes: 28 additions & 0 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,34 @@ describe('client.test.js', function() {
});
});

describe('beginDoomedTransactionScope(scope)', function() {

it('should insert 0 rows in a doomed transaction with ctx', function* () {
const ctx = {};
const db = this.db;

function* insert() {
return yield db.beginDoomedTransactionScope(function* (conn) {
yield conn.query('insert into ??(name, email, gmt_create, gmt_modified) \
values(?, ?, now(), now())',
[ table, prefix + 'beginDoomedTransactionScopeCtx1', prefix + 'm@beginDoomedTransactionScopeCtx1.com' ]);
yield conn.query('insert into ??(name, email, gmt_create, gmt_modified) \
values(?, ?, now(), now())',
[ table, prefix + 'beginDoomedTransactionScopeCtx2', prefix + 'm@beginDoomedTransactionScopeCtx1.com' ]);
return true;
}, ctx);
}

yield insert();

const rows = yield db.query('select * from ?? where email=? order by id',
[ table, prefix + 'm@beginDoomedTransactionScopeCtx1.com' ]);
assert.equal(rows.length, 0);
assert.equal(ctx._transactionConnection, null);
assert.equal(ctx._transactionScopeCount, 0);
});
});

describe('get(table, obj, options), select(table, options)', function() {
before(function* () {
let result = yield this.db.insert(table, {
Expand Down

0 comments on commit b227bc1

Please sign in to comment.