Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

deepEqual passes for numbers now

still fails for setting DocumentArrays to values equal
to existing docs
  • Loading branch information...
commit 71af4a1e16110712736ee4ea21138502914539bb 1 parent f01ff16
@aheckmann aheckmann authored
Showing with 42 additions and 29 deletions.
  1. +8 −5 lib/utils.js
  2. +34 −24 test/model.test.js
View
13 lib/utils.js
@@ -153,6 +153,11 @@ exports.deepEqual = function deepEqual (a, b) {
if (a.prototype !== b.prototype) return false;
+ // Handle MongooseNumbers
+ if (a instanceof Number && b instanceof Number) {
+ return a.valueOf() === b.valueOf();
+ }
+
try {
var ka = Object.keys(a),
kb = Object.keys(b),
@@ -165,14 +170,17 @@ exports.deepEqual = function deepEqual (a, b) {
// hasOwnProperty)
if (ka.length != kb.length)
return false;
+
//the same set of keys (although not necessarily the same order),
ka.sort();
kb.sort();
+
//~~~cheap key test
for (i = ka.length - 1; i >= 0; i--) {
if (ka[i] != kb[i])
return false;
}
+
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) {
@@ -180,11 +188,6 @@ exports.deepEqual = function deepEqual (a, b) {
if (!deepEqual(a[key], b[key])) return false;
}
- // Handle MongooseNumber edge case
- if (a.valueOf && b.valueOf && a.valueOf() !== b.valueOf()) {
- return false;
- }
-
return true;
};
View
58 test/model.test.js
@@ -618,40 +618,50 @@ module.exports = {
db.close();
},
-
+
'test isModified on a Mongoose Document - Modifying an existing record' : function(){
var db = start()
, BlogPost = db.model('BlogPost', collection)
- , doc = {
- title : 'Test'
- , slug : 'test'
- , date : new Date
- , meta : {
- date : new Date
- , visitors : 5
- }
- , published : true
- , owners : [new DocumentObjectId, new DocumentObjectId]
- , comments : [
- { title: 'Test', date: new Date, body: 'Test' }
- , { title: 'Super', date: new Date, body: 'Cool' }
- ]
- };
+
+ var doc = {
+ title : 'Test'
+ , slug : 'test'
+ , date : new Date
+ , meta : {
+ date : new Date
+ , visitors : 5
+ }
+ , published : true
+ , mixed : { x: [ { y: [1,'yes', 2] } ] }
+ , numbers : []
+ , owners : [new DocumentObjectId, new DocumentObjectId]
+ , comments : [
+ { title: 'Test', date: new Date, body: 'Test' }
+ , { title: 'Super', date: new Date, body: 'Cool' }
+ ]
+ };
BlogPost.create(doc, function (err, post) {
BlogPost.findById(post.id, function (err, postRead) {
//set the same data again back to the document.
//expected result, nothing should be set to modified
postRead.set(doc);
-
+
+ postRead.isModified('title').should.be.false;
+ postRead.isModified('slug').should.be.false;
+ postRead.isModified('date').should.be.false;
+ postRead.isModified('meta.date').should.be.false;
postRead.isModified('meta.visitors').should.be.false;
- postRead.isModified('owners').should.be.false;
-
- //uncomment this, this should the bug with respect to Embedded array
- //documents where it is set to modified even though it is not
- //postRead.isModified('comments').should.be.false;
-
- db.close();
+ postRead.isModified('published').should.be.false;
+ postRead.isModified('mixed').should.be.false;
+ postRead.isModified('numbers').should.be.false;
+ postRead.isModified('owners').should.be.false;
+
+ //uncomment this, this should the bug with respect to Embedded array
+ //documents where it is set to modified even though it is not
+ //postRead.isModified('comments').should.be.false;
+
+ db.close();
});
});
},

2 comments on commit 71af4a1

@gitfy

Is there way to fix the embedded document issues ?

The issue happens when the embedded document is created during the Document.prototype.set where it is not 'init' -ed but rather 'set'.

It starts in Document.prototype.set, from :

this.try(function(){
  val = schema.applySetters(schema.cast(val, self), self);
}))

then in the DocumentArray.prototype.cast, it goes to do a set which automatically without any check marks the array as modified.

@aheckmann
Owner

i'm sure there is a way :)

I opened a specific ticket for it here #490

Please sign in to comment.
Something went wrong with that request. Please try again.