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

Commit a855a2ae by MrVoltz Committed by Jan Aagaard Meier

Prove that Instance.equals() still doesn't work and fix it (#5605)

* Added failing test to prove that #5301 is not fixed yet

* Instance.equals() compares only model and primary key. Fixes #5103

* Instance .equals() PK check accounts for multiple PKs
1 parent 7579d2a2
...@@ -1011,27 +1011,25 @@ Instance.prototype.decrement = function(fields, options) { ...@@ -1011,27 +1011,25 @@ Instance.prototype.decrement = function(fields, options) {
}; };
/** /**
* Check whether all values of this and `other` Instance are the same * Check whether this and `other` Instance refer to the same row
* *
* @param {Instance} other * @param {Instance} other
* @return {Boolean} * @return {Boolean}
*/ */
Instance.prototype.equals = function(other) { Instance.prototype.equals = function(other) {
var result = true; var self = this;
if (!other || !other.dataValues) { if (!other || !other.Model) {
return false; return false;
} }
Utils._.each(this.dataValues, function(value, key) { if (other.Model !== this.Model) {
if (Utils._.isDate(value) && Utils._.isDate(other.dataValues[key])) { return false;
result = result && (value.getTime() === other.dataValues[key].getTime());
} else {
result = result && (value === other.dataValues[key]);
} }
});
return result; return Utils._.every(this.Model.primaryKeyAttributes, function(attribute) {
return self.get(attribute, {raw: true}) === other.get(attribute, {raw: true});
});
}; };
/** /**
......
...@@ -1878,7 +1878,9 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1878,7 +1878,9 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
expect(user1.get('Projects')).to.not.exist; expect(user1.get('Projects')).to.not.exist;
expect(user2.get('Projects')).to.exist; expect(user2.get('Projects')).to.exist;
expect(user1.equals(user2)).to.be.true; expect(user1.equals(user2)).to.be.true;
expect(user2.equals(user1)).to.be.true;
expect(user1.equals(user3)).to.not.be.true; expect(user1.equals(user3)).to.not.be.true;
expect(user3.equals(user1)).to.not.be.true;
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!