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

Commit fa509566 by Jan Aagaard Meier

Made the set tests mysql specific, and more promisy

1 parent 66e40c39
Showing with 43 additions and 63 deletions
......@@ -563,24 +563,23 @@ module.exports = (function() {
};
/**
* Execute a query which would set an environment or user variable.
* If you want to set variables for a specific query, you should create a transaction and pass it on options.
* Execute a query which would set an environment or user variable. The variables are set per connection, so this function needs a transaction.
*
* @method set
* @param {Object} variables Object with multiple variables.
* @param {Object} options={} Query options.
* @param {Transaction} options.transaction=null The transaction that the query should be executed under
* @return {Promise}
* @param {Object} options Query options.
* @param {Transaction} options.transaction The transaction that the query should be executed under
*
* @return {Promise}
*/
Sequelize.prototype.set = function( variables, options ) {
var query;
// Prepare options
options = Utils._.extend( {}, this.options.set, typeof options === 'object' && options || {} );
options = Utils._.extend({}, this.options.set, typeof options === 'object' && options || {});
if( ! options.transaction || ! ( options.transaction instanceof Transaction ) ) {
return Promise.rejected( new TypeError("options.transaction is required") );
if (!options.transaction || !(options.transaction instanceof Transaction) ) {
throw new TypeError("options.transaction is required");
}
// Override some options, since this isn't a SELECT
......@@ -595,7 +594,7 @@ module.exports = (function() {
return '@'+k +' := '+ ( typeof v === 'string' ? '"'+v+'"' : v );
}).join(', ');
return this.query( query, null, options );
return this.query(query, null, options);
};
/**
......
......@@ -449,64 +449,45 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
}
})
describe('set', function() {
it("should return an promised error if transaction isn't defined", function ( done ) {
var S = this.sequelize;
S.set({ foo: 'bar' })
.done( function ( err ) {
expect( err ).to.be.an.instanceof( TypeError )
done();
});
})
it("one value", function ( done ) {
var S = this.sequelize;
S.transaction().then(function ( t ) {
S
.set({ foo: 'bar' }, { transaction: t })
.then(function () {
return S.query( 'SELECT @foo as `foo`', null, { raw: true, plain: true, transaction: t })
})
.then(function ( data ) {
expect( data ).to.be.ok
expect( data.foo ).to.be.equal( 'bar' )
})
.then(function () {
return t.commit()
})
.then( done )
if (Support.dialectIsMySQL()) {
describe('set', function() {
it("should return an promised error if transaction isn't defined", function () {
expect(function () {
this.sequelize.set({ foo: 'bar' })
}.bind(this)).to.throw(TypeError, "options.transaction is required")
})
})
it("multiple values", function ( done ) {
var S = this.sequelize;
it("one value", function () {
return this.sequelize.transaction().bind(this).then(function (t) {
this.t = t;
return this.sequelize.set({ foo: 'bar' }, { transaction: t });
}).then(function () {
return this.sequelize.query( 'SELECT @foo as `foo`', null, { raw: true, plain: true, transaction: this.t });
}).then(function (data) {
expect(data).to.be.ok
expect(data.foo).to.be.equal('bar')
return this.t.commit();
});
});
S.transaction().then(function ( t ) {
S
.set({
it("multiple values", function () {
return this.sequelize.transaction().bind(this).then(function (t) {
this.t = t;
return this.sequelize.set({
foo: 'bar',
foos: 'bars',
}, { transaction: t })
.then(function () {
return S.query( 'SELECT @foo as `foo`, @foos as `foos`', null, { raw: true, plain: true, transaction: t })
})
.then(function ( data ) {
expect( data ).to.be.ok
expect( data.foo ).to.be.equal( 'bar' )
expect( data.foos ).to.be.equal( 'bars' )
})
.then(function () {
return t.commit()
})
.then( done )
})
})
})
}, { transaction: t });
}).then(function () {
return this.sequelize.query( 'SELECT @foo as `foo`, @foos as `foos`', null, { raw: true, plain: true, transaction: this.t });
}).then(function (data) {
expect(data).to.be.ok;
expect(data.foo).to.be.equal('bar');
expect(data.foos).to.be.equal('bars');
return this.t.commit();
});
});
});
}
describe('define', function() {
it("adds a new dao to the dao manager", function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!