Skip to content

Commit

Permalink
Merge pull request #4023 from smihica/#4011
Browse files Browse the repository at this point in the history
option object shouldn't be updated internally.
  • Loading branch information
janmeier committed Aug 4, 2015
2 parents 3603863 + 8cd25ce commit 03008f7
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 11 deletions.
18 changes: 9 additions & 9 deletions lib/instance.js
Expand Up @@ -482,7 +482,7 @@ Instance.prototype.save = function(options) {
if (arguments.length > 1) {
throw new Error('The second argument was removed in favor of the options object.');
}
options = _.defaults(options || {}, {
options = _.defaults({}, options, {
hooks: true,
validate: true
});
Expand Down Expand Up @@ -724,7 +724,7 @@ Instance.prototype.save = function(options) {
* @return {Promise<this>}
*/
Instance.prototype.reload = function(options) {
options = _.defaults(options || {}, {
options = _.defaults({}, options, {
where: this.where(),
limit: 1,
include: this.options.include || null
Expand Down Expand Up @@ -809,7 +809,7 @@ Instance.prototype.destroy = function(options) {
options = Utils._.extend({
hooks: true,
force: false
}, options || {});
}, options);

return Promise.bind(this).then(function() {
// Run before hook
Expand All @@ -821,7 +821,7 @@ Instance.prototype.destroy = function(options) {

if (this.Model._timestampAttributes.deletedAt && options.force === false) {
this.setDataValue(this.Model._timestampAttributes.deletedAt, new Date());
return this.save(_.extend(_.clone(options), {hooks : false}));
return this.save(_.extend({}, options, {hooks : false}));
} else {
where = {};
var primaryKeys = this.Model.primaryKeyAttributes;
Expand Down Expand Up @@ -855,7 +855,7 @@ Instance.prototype.restore = function(options) {
options = Utils._.extend({
hooks: true,
force: false
}, options || {});
}, options);

return Promise.bind(this).then(function() {
// Run before hook
Expand All @@ -864,7 +864,7 @@ Instance.prototype.restore = function(options) {
}
}).then(function() {
this.setDataValue(this.Model._timestampAttributes.deletedAt, null);
return this.save(_.extend(_.clone(options), {hooks : false, omitNull : false}));
return this.save(_.extend({}, options, {hooks : false, omitNull : false}));
}).tap(function() {
// Run after hook
if (options.hooks) {
Expand Down Expand Up @@ -913,13 +913,13 @@ Instance.prototype.increment = function(fields, options) {
}
}

options = _.defaults(options || {}, {
options = _.defaults({}, options, {
by: 1,
attributes: {},
where: {}
});

where = _.extend(options.where || {}, identifier);
where = _.extend({}, options.where, identifier);

if (Utils._.isString(fields)) {
values[fields] = options.by;
Expand Down Expand Up @@ -970,7 +970,7 @@ Instance.prototype.increment = function(fields, options) {
* @return {Promise}
*/
Instance.prototype.decrement = function(fields, options) {
options = _.defaults(options || {}, {
options = _.defaults({}, options, {
by: 1
});

Expand Down
51 changes: 51 additions & 0 deletions test/unit/instance/decrement.test.js
@@ -0,0 +1,51 @@
'use strict';

var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, Sequelize = Support.Sequelize
, sinon = require('sinon');

describe(Support.getTestDialectTeaser('Instance'), function() {
describe('decrement', function () {
describe('options tests', function() {
var stub
, Model = current.define('User', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true,
}
})
, instance;

before(function() {
stub = sinon.stub(current, 'query').returns(
Sequelize.Promise.resolve({
_previousDataValues: {id: 3},
dataValues: {id: 1}
})
);
});

after(function() {
stub.restore();
});

it('should allow decrements even if options are not given', function () {
instance = Model.build({id: 3}, {isNewRecord: false});
expect(function () {
instance.decrement(['id']);
}).to.not.throw();
});

it('should not modify options when it given to decrement', function () {
instance = Model.build({id: 3}, {isNewRecord: false});
var options = { by: 2 };
instance.decrement(['id'], options);
expect(options).to.deep.equal({ by: 2 });
});
});
});
});
51 changes: 51 additions & 0 deletions test/unit/instance/destroy.test.js
@@ -0,0 +1,51 @@
'use strict';

var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, Sequelize = Support.Sequelize
, sinon = require('sinon');

describe(Support.getTestDialectTeaser('Instance'), function() {
describe('destroy', function () {
describe('options tests', function() {
var stub
, Model = current.define('User', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true,
}
})
, instance;

before(function() {
stub = sinon.stub(current, 'query').returns(
Sequelize.Promise.resolve({
_previousDataValues: {},
dataValues: {id: 1}
})
);
});

after(function() {
stub.restore();
});

it('should allow destroies even if options are not given', function () {
instance = Model.build({id: 1}, {isNewRecord: false});
expect(function () {
instance.destroy();
}).to.not.throw();
});

it('should not modify options when it given to destroy', function () {
instance = Model.build({id: 1}, {isNewRecord: false});
var options = { transaction: null };
instance.destroy(options);
expect(options).to.deep.equal({ transaction: null });
});
});
});
});
51 changes: 51 additions & 0 deletions test/unit/instance/increment.test.js
@@ -0,0 +1,51 @@
'use strict';

var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, Sequelize = Support.Sequelize
, sinon = require('sinon');

describe(Support.getTestDialectTeaser('Instance'), function() {
describe('increment', function () {
describe('options tests', function() {
var stub
, Model = current.define('User', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true,
}
})
, instance;

before(function() {
stub = sinon.stub(current, 'query').returns(
Sequelize.Promise.resolve({
_previousDataValues: {id: 1},
dataValues: {id: 3}
})
);
});

after(function() {
stub.restore();
});

it('should allow increments even if options are not given', function () {
instance = Model.build({id: 1}, {isNewRecord: false});
expect(function () {
instance.increment(['id']);
}).to.not.throw();
});

it('should not modify options when it given to increment', function () {
instance = Model.build({id: 1}, {isNewRecord: false});
var options = { by: 2 };
instance.increment(['id'], options);
expect(options).to.deep.equal({ by: 2 });
});
});
});
});
57 changes: 57 additions & 0 deletions test/unit/instance/reload.test.js
@@ -0,0 +1,57 @@
'use strict';

var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, Sequelize = Support.Sequelize
, sinon = require('sinon');

describe(Support.getTestDialectTeaser('Instance'), function() {
describe('reload', function () {
describe('options tests', function() {
var stub
, Model = current.define('User', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true,
},
deletedAt: {
type: Sequelize.DATE,
}
}, {
paranoid: true
})
, instance;

before(function() {
stub = sinon.stub(current, 'query').returns(
Sequelize.Promise.resolve({
_previousDataValues: {id: 1},
dataValues: {id: 2}
})
);
});

after(function() {
stub.restore();
});

it('should allow reloads even if options are not given', function () {
instance = Model.build({id: 1}, {isNewRecord: false});
expect(function () {
instance.reload();
}).to.not.throw();
});

it('should not modify options when it given to reload', function () {
instance = Model.build({id: 1}, {isNewRecord: false});
var options = { transaction: null };
instance.reload(options);
expect(options).to.deep.equal({ transaction: null });
});

});
});
});
57 changes: 57 additions & 0 deletions test/unit/instance/restore.test.js
@@ -0,0 +1,57 @@
'use strict';

var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize
, Sequelize = Support.Sequelize
, sinon = require('sinon');

describe(Support.getTestDialectTeaser('Instance'), function() {
describe('restore', function () {
describe('options tests', function() {
var stub
, Model = current.define('User', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true,
},
deletedAt: {
type: Sequelize.DATE,
}
}, {
paranoid: true
})
, instance;

before(function() {
stub = sinon.stub(current, 'query').returns(
Sequelize.Promise.resolve({
_previousDataValues: {id: 1},
dataValues: {id: 2}
})
);
});

after(function() {
stub.restore();
});

it('should allow restores even if options are not given', function () {
instance = Model.build({id: 1}, {isNewRecord: false});
expect(function () {
instance.restore();
}).to.not.throw();
});

it('should not modify options when it given to restore', function () {
instance = Model.build({id: 1}, {isNewRecord: false});
var options = { transaction: null };
instance.restore(options);
expect(options).to.deep.equal({ transaction: null });
});
});
});

});

0 comments on commit 03008f7

Please sign in to comment.