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

Boolean conversion is now only done on valid boolean values/types. Added tests t…

…o cover this functionality.
1 parent 93f2fed3
...@@ -348,7 +348,15 @@ module.exports = (function() { ...@@ -348,7 +348,15 @@ module.exports = (function() {
// Bit fields are returned as buffers // Bit fields are returned as buffers
value = value[0]; value = value[0];
} }
value = _.isString(value) ? !!parseInt(value, 10) : !!value;
if (_.isString(value)) {
// Only take action on valid boolean strings.
value = (value === "true") ? true : (value === "false") ? false : value;
} else if (_.isNumber(value)) {
// Only take action on valid boolean integers.
value = (value === 1) ? true : (value === 0) ? false : value;
}
} }
if (!options.raw && originalValue !== value) { if (!options.raw && originalValue !== value) {
......
...@@ -8,12 +8,15 @@ var chai = require('chai') ...@@ -8,12 +8,15 @@ var chai = require('chai')
, dialect = Support.getTestDialect() , dialect = Support.getTestDialect()
, config = require(__dirname + '/../config/config') , config = require(__dirname + '/../config/config')
, sinon = require('sinon') , sinon = require('sinon')
, sinonChai = require('sinon-chai')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, uuid = require('node-uuid') , uuid = require('node-uuid')
, _ = require('lodash') , _ = require('lodash')
, current = Support.sequelize; , current = Support.sequelize;
chai.should();
chai.use(datetime); chai.use(datetime);
chai.use(sinonChai);
chai.config.includeStack = true; chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('Instance'), function() { describe(Support.getTestDialectTeaser('Instance'), function() {
...@@ -41,6 +44,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -41,6 +44,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
dateAllowNullTrue: { dateAllowNullTrue: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: true allowNull: true
},
isSuperUser: {
type: DataTypes.BOOLEAN,
defaultValue: false
} }
}); });
this.User.sync({ force: true }).success(function() { this.User.sync({ force: true }).success(function() {
...@@ -738,6 +746,106 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -738,6 +746,106 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
}); });
}); });
describe('super user boolean', function() {
it('should default to false', function() {
return this.User.build({
username: 'a user'
})
.save()
.bind(this)
.then(function() {
return this.User.find({
where: {
username: 'a user'
}
})
.then(function(user) {
expect(user.isSuperUser).to.be.false;
});
});
});
it('should override default when given truthy boolean', function() {
return this.User.build({
username: 'a user',
isSuperUser: true
})
.save()
.bind(this)
.then(function() {
return this.User.find({
where: {
username: 'a user'
}
})
.then(function(user) {
expect(user.isSuperUser).to.be.true;
});
});
});
it('should override default when given truthy boolean-string ("true")', function() {
return this.User.build({
username: 'a user',
isSuperUser: "true"
})
.save()
.bind(this)
.then(function() {
return this.User.find({
where: {
username: 'a user'
}
})
.then(function(user) {
expect(user.isSuperUser).to.be.true;
});
});
});
it('should override default when given truthy boolean-int (1)', function() {
return this.User.build({
username: 'a user',
isSuperUser: 1
})
.save()
.bind(this)
.then(function() {
return this.User.find({
where: {
username: 'a user'
}
})
.then(function(user) {
expect(user.isSuperUser).to.be.true;
});
});
});
it('should throw error when given value of incorrect type', function() {
function didApplyValue(cb) {
cb();
throw new Error("Value incorrectly applied!!!");
}
var spy = sinon.spy();
return this.User.build({
username: 'a user',
isSuperUser: "INCORRECT_VALUE_TYPE"
})
.save()
.then(function () {
didApplyValue(spy);
})
.catch(function(err) {
spy.should.have.callCount(0);
expect(err).to.exist;
expect(err.message).to.exist;
});
});
});
}); });
describe('complete', function() { describe('complete', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!