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

Commit 36a51ab3 by Mick Hansen

tests & stuff

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