不要怂,就是干,撸起袖子干!

Commit 760eb59e by Jan Aagaard Meier

Merge pull request #4062 from gutobortolozzo/clsFindOrCreate

FindOrCreate with CLS not using stored transaction
2 parents 68717757 eacbac80
...@@ -1642,6 +1642,10 @@ Model.prototype.findOrCreate = function(options) { ...@@ -1642,6 +1642,10 @@ Model.prototype.findOrCreate = function(options) {
); );
} }
if (options.transaction === undefined && this.sequelize.constructor.cls) {
options.transaction = this.sequelize.constructor.cls.get('transaction');
}
var self = this var self = this
, internalTransaction = !options.transaction , internalTransaction = !options.transaction
, values , values
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, cls = require('continuation-local-storage')
, sinon = require('sinon')
, stub = sinon.stub
, Promise = require('bluebird');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('method findOrCreate', function () {
before(function () {
current.constructor.cls = cls.createNamespace('sequelize');
});
after(function () {
delete current.constructor.cls;
});
beforeEach(function () {
this.User = current.define('User', {}, {
name: 'John'
});
this.transactionStub = stub(this.User.sequelize, 'transaction');
this.transactionStub.returns(new Promise(function () {}));
this.clsStub = stub(current.constructor.cls, 'get');
this.clsStub.returns({ id: 123 });
});
afterEach(function () {
this.transactionStub.restore();
this.clsStub.restore();
});
it('should use transaction from cls if available', function () {
var options = {
where : {
name : 'John'
}
};
this.User.findOrCreate(options);
expect(this.clsStub.calledOnce).to.equal(true, 'expected to ask for transaction');
});
it('should not use transaction from cls if provided as argument', function () {
var options = {
where : {
name : 'John'
},
transaction : { id : 123 }
};
this.User.findOrCreate(options);
expect(this.clsStub.called).to.equal(false);
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!