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

Commit 378fd96a by Sascha Depold

store enums as text for sqlite + added test that checks if stored enum value is restored correctly

1 parent 3e1a2f89
......@@ -121,7 +121,7 @@ module.exports = (function() {
var definition = this.daoFactory.rawAttributes[attrName]
, isEnum = (definition.type === 'ENUM')
, hasValue = (typeof values[attrName] !== 'undefined')
, valueOutOfScope = (definition.values.indexOf(values[attrName]) === -1)
, valueOutOfScope = ((definition.values || []).indexOf(values[attrName]) === -1)
if (isEnum && hasValue && valueOutOfScope) {
throw new Error('Value "' + values[attrName] + '" for ENUM ' + attrName + ' is out of allowed scope. Allowed values: ' + definition.values.join(', '))
......
......@@ -152,7 +152,11 @@ module.exports = (function() {
, replacements = { type: dataType.type }
if (dataType.type === DataTypes.ENUM) {
replacements.type = "INTEGER"
replacements.type = "TEXT"
if (!(Array.isArray(dataType.values) && (dataType.values.length > 0))) {
throw new Error('Values for ENUM haven\'t been defined.')
}
}
if (dataType.hasOwnProperty('allowNull') && !dataType.allowNull && !dataType.primaryKey) {
......
......@@ -346,6 +346,28 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
done()
})
})
describe('enums', function() {
before(function(done) {
this.Item = this.sequelize.define('Item', {
state: { type: Helpers.Sequelize.ENUM, values: ['available', 'in_cart', 'shipped'] }
})
this.sequelize.sync({ force: true }).success(function() {
this.Item.create({ state: 'available' }).success(function(item) {
this.item = item
done()
}.bind(this))
}.bind(this))
})
it('correctly restores enum values', function(done) {
this.Item.find({ where: { state: 'available' }}).success(function(item) {
expect(item.id).toEqual(this.item.id)
done()
}.bind(this))
})
})
})
describe('find', function find() {
......
......@@ -143,7 +143,7 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() {
this.Review.sync({ force: true }).success(done)
})
it('=>raises an error if no values are defined', function() {
it('raises an error if no values are defined', function() {
Helpers.assertException(function() {
this.sequelize.define('omnomnom', {
bla: { type: Helpers.Sequelize.ENUM }
......@@ -167,7 +167,7 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() {
}.bind(this))
})
it("=>doesn't save an instance if value is not in the range of enums", function() {
it("doesn't save an instance if value is not in the range of enums", function() {
Helpers.assertException(function() {
this.Review.create({ status: 'fnord' })
}.bind(this), 'Value "fnord" for ENUM status is out of allowed scope. Allowed values: scheduled, active, finished')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!