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

Commit 36a51ab3 by Mick Hansen

tests & stuff

1 parent 278d0a2c
Showing with 40 additions and 26 deletions
......@@ -18,8 +18,6 @@ module.exports = (function() {
this.isNewRecord = options.isNewRecord
initValues.call(this, values, options)
this.isDirty = options.isDirty
}
Utils._.extend(DAO.prototype, Mixin.prototype)
......@@ -49,7 +47,7 @@ module.exports = (function() {
Object.defineProperty(DAO.prototype, 'isDirty', {
get: function() {
return this.changed()
return !!this.changed()
}
})
......@@ -160,7 +158,7 @@ module.exports = (function() {
value = !!value
}
this._previousDataValues[key] = originalValue
if (originalValue !== value) this._previousDataValues[key] = originalValue
this.dataValues[key] = value
}
}
......@@ -174,9 +172,11 @@ module.exports = (function() {
}
return false
}
return Object.keys(this.dataValues).some(function (key) {
var changed = Object.keys(this.dataValues).filter(function (key) {
return this.changed(key)
}.bind(this))
return changed.length ? changed : false
}
DAO.prototype.previous = function(key) {
......@@ -244,10 +244,11 @@ module.exports = (function() {
options.fields = this.attributes
}
var self = this
, values = options.fields ? {} : (this.options.includeNames ? _.omit(this.dataValues, this.options.includeNames) : this.dataValues)
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
, createdAtAttr = Utils._.underscoredIf(this.__options.createdAt, this.__options.underscored)
var self = this
, values = options.fields ? {} : (this.options.includeNames ? _.omit(this.dataValues, this.options.includeNames) : this.dataValues)
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
, createdAtAttr = Utils._.underscoredIf(this.__options.createdAt, this.__options.underscored)
, originalValues = Utils._.clone(this.dataValues)
if (options.fields) {
if (self.__options.timestamps) {
......@@ -365,19 +366,14 @@ module.exports = (function() {
self.set(values, {raw: true})
self.QueryInterface[query].apply(self.QueryInterface, args)
.on('sql', function(sql) {
emitter.emit('sql', sql)
})
.error(function(err) {
emitter.emit('error', err)
})
.proxy(emitter, {events: ['sql', 'error']})
.success(function(result) {
self.__factory.runHooks('after' + hook, result.values, function(err, newValues) {
if (!!err) {
return emitter.emit('error', err)
}
result.dataValues = _.extend(result.dataValues, newValues)
result._previousDataValues = result.dataValues = _.extend(result.dataValues, newValues)
emitter.emit('success', result)
})
})
......
......@@ -145,7 +145,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
var user = User.build()
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.false
expect(user.changed()).to.be.false
expect(user.changed()).not.to.be.ok
expect(user.isDirty).to.be.false
})
......@@ -159,7 +159,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.true
expect(user.changed()).to.be.true
expect(user.changed()).to.be.ok
expect(user.isDirty).to.be.true
})
......@@ -168,20 +168,38 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
name: {type: DataTypes.STRING}
})
User.sync().done(function (err) {
var user = User.build({
name: 'Jan Meier'
})
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.true
expect(user.changed()).to.be.ok
expect(user.isDirty).to.be.true
user.save().done(function (err) {
expect(err).not.to.be.ok
expect(user.changed('name')).to.be.false
expect(user.changed()).not.to.be.ok
expect(user.isDirty).to.be.false
done()
})
})
})
it('setting the same value twice should not impact the result', function () {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
var user = User.build({
name: 'Jan Meier'
})
user.set('name', 'Mick Hansen')
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.true
expect(user.changed()).to.be.true
expect(user.changed()).to.be.ok
expect(user.isDirty).to.be.true
user.save().done(function (err) {
expect(user.changed('name')).to.be.false
expect(user.changed()).to.be.false
expect(user.isDirty).to.be.false
done()
})
expect(user.previous('name')).to.equal('Jan Meier')
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!