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

Commit 86f258c6 by Sascha Depold

Merge branch 'updateAttributes-spec' of https://github.com/durango/sequelize int…

…o durango-updateAttributes-spec

Conflicts:
	lib/dao.js
2 parents 95a23b6d ef31e540
......@@ -108,7 +108,7 @@ module.exports = (function() {
fields.push(updatedAtAttr)
}
if (fields.indexOf(createdAtAttr) === -1) {
if (fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) {
fields.push(createdAtAttr)
}
}
......@@ -240,26 +240,38 @@ module.exports = (function() {
var self = this
var readOnlyAttributes = Object.keys(this.__factory.primaryKeys)
readOnlyAttributes.push('id')
readOnlyAttributes.push('createdAt')
readOnlyAttributes.push('updatedAt')
readOnlyAttributes.push('deletedAt')
if (this.isNewRecord !== true) {
readOnlyAttributes.push(this.daoFactory.options.underscored === true ? 'created_at' : 'createdAt')
}
// readOnlyAttributes.push(this.daoFactory.options.underscored === true ? 'updated_at' : 'updatedAt')
readOnlyAttributes.push(this.daoFactory.options.underscored === true ? 'deleted_at' : 'deletedAt')
var isDirty = this.isDirty
Utils._.each(updates, function(value, attr) {
var updateAllowed = (
(readOnlyAttributes.indexOf(attr) == -1) &&
(readOnlyAttributes.indexOf(Utils._.underscored(attr)) == -1) &&
(self.attributes.indexOf(attr) > -1)
)
if (updateAllowed) {
if (Utils.hasChanged(self[attr], value)) {
isDirty = true
}
self[attr] = value
}
})
// since we're updating the record, we should be updating the updatedAt column..
if (this.daoFactory.options.timestamps === true) {
isDirty = true
self[this.daoFactory.options.underscored === true ? 'updated_at' : 'updatedAt'] = new Date()
}
this.isDirty = isDirty
}
......
......@@ -798,6 +798,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
describe('update', function() {
it('updates the attributes that we select only without updating createdAt', function(done) {
var User = this.sequelize.define('User1', {
username: Sequelize.STRING,
secretValue: Sequelize.STRING
}, {
paranoid:true
})
User.sync({ force: true }).success(function() {
User.create({username: 'Peter', secretValue: '42'}).success(function(user) {
user.updateAttributes({ secretValue: '43' }, ['secretValue']).on('sql', function(sql) {
expect(sql).to.match(/UPDATE\s+[`"]+User1s[`"]+\s+SET\s+[`"]+secretValue[`"]='43',[`"]+updatedAt[`"]+='[^`",]+'\s+WHERE [`"]+id[`"]+=1/)
done()
})
})
})
})
it('allows sql logging of updated statements', function(done) {
var User = this.sequelize.define('User', {
name: Sequelize.STRING,
......
......@@ -169,16 +169,38 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
it("returns false for bulk non-changed attribute", function(done) {
it("returns true for bulk non-changed attribute + model with timestamps", function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.setAttributes({
username: 'user'
})
expect(user.isDirty).to.be.false
expect(user.isDirty).to.be.rue
done()
})
})
it("returns false for bulk non-changed attribute + model without timestamps", function(done) {
var User = this.sequelize.define('User' + parseInt(Math.random() * 10000000), {
username: DataTypes.STRING
}, {
timestamps: false
})
User
.sync({ force: true })
.then(function() {
return User.create({ username: "user" })
})
.then(function(user) {
return user.setAttributes({ username: "user" })
expect(user.isDirty).to.be.false
})
.then(function() {
done()
})
})
it("returns true for changed and bulk non-changed attribute", function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.aNumber = 23
......@@ -1112,17 +1134,21 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
identifier: 'identifier'
}).success(function(user) {
var oldCreatedAt = user.createdAt
, oldUpdatedAt = user.updatedAt
, oldIdentifier = user.identifier
user.updateAttributes({
name: 'foobar',
createdAt: new Date(2000, 1, 1),
identifier: 'another identifier'
}).success(function(user) {
expect((new Date(user.createdAt)).getTime()).to.equal((new Date(oldCreatedAt)).getTime())
expect(user.identifier).to.equal(oldIdentifier)
done()
})
setTimeout(function () {
user.updateAttributes({
name: 'foobar',
createdAt: new Date(2000, 1, 1),
identifier: 'another identifier'
}).success(function(user) {
expect(new Date(user.createdAt)).to.equalDate(new Date(oldCreatedAt))
expect(new Date(user.updatedAt)).to.not.equalTime(new Date(oldUpdatedAt))
expect(user.identifier).to.equal(oldIdentifier)
done()
})
}, 1000)
})
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!