From 58bd2a6ae2396c146d31b7db4023be837864cc62 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Wed, 22 Apr 2015 09:39:16 +0200 Subject: [PATCH] Added tests for Omnom --- lib/utils/Omnom.js | 44 ++++++++++-- lib/utils/Omnom.js.map | 2 +- lib/utils/Omnom.ts | 118 ++++++++++++++++++++---------- test/Omnom.js | 133 ++++++++++++++++++++++++++++++++++ test/Omnom.js.map | 1 + test/Omnom.ts | 160 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 416 insertions(+), 42 deletions(-) create mode 100644 test/Omnom.js create mode 100644 test/Omnom.js.map create mode 100644 test/Omnom.ts diff --git a/lib/utils/Omnom.js b/lib/utils/Omnom.js index 3f39a13..b8446a3 100644 --- a/lib/utils/Omnom.js +++ b/lib/utils/Omnom.js @@ -1,6 +1,4 @@ -/// -/// -/// +/// var _ = require('lodash'); var MongoDB = require('mongodb'); var Omnom = (function () { @@ -18,6 +16,10 @@ var Omnom = (function () { }); Omnom.prototype.diff = function (original, modified) { this.onObject(original, modified); + return this; + }; + Omnom.diff = function (original, modified) { + return new Omnom().diff(original, modified).changes; }; Omnom.prototype.onObject = function (original, modified, changePath) { if (original === undefined || original === null) @@ -68,16 +70,48 @@ var Omnom = (function () { else return this.set(changePath, modified); } + // Check if we can get from original => modified using just pushes + if (original.length < modified.length) { + var canPush = true; + for (i = 0; i < original.length; i++) + if (this.almostEqual(original[i], modified[i]) < 1) { + canPush = false; + break; + } + if (canPush) { + for (i = original.length; i < modified.length; i++) + this.push(changePath, modified[i]); + return; + } + } + // Otherwise, we need to use $set to generate the new array + // Check how many manipulations would need to be performed, if it's more than half the array size + // then rather re-create the array + var sets = []; + var partials = []; + for (i = 0; i < modified.length; i++) { + var equality = this.almostEqual(original[i], modified[i]); + if (equality === 0) + sets.push(i); + else if (equality < 1) + partials.push(i); + } + if (sets.length > modified.length / 2) + return this.set(changePath, modified); + for (i = 0; i < sets.length; i++) + this.set(this.resolve(changePath, sets[i].toString()), modified[sets[i]]); + for (i = 0; i < partials.length; i++) + this.onObject(original[partials[i]], modified[partials[i]], this.resolve(changePath, partials[i].toString())); }; Omnom.prototype.set = function (path, value) { if (!this.changes.$set) this.changes.$set = {}; this.changes.$set[path] = value; }; - Omnom.prototype.unset = function (path, value) { + Omnom.prototype.unset = function (path) { if (!this.changes.$unset) this.changes.$unset = {}; - this.changes.$unset[path] = value; + this.changes.$unset[path] = 1; }; Omnom.prototype.inc = function (path, value) { if (!this.changes.$inc) diff --git a/lib/utils/Omnom.js.map b/lib/utils/Omnom.js.map index 4ed23f0..77b7e69 100644 --- a/lib/utils/Omnom.js.map +++ b/lib/utils/Omnom.js.map @@ -1 +1 @@ -{"version":3,"file":"Omnom.js","sourceRoot":"","sources":["Omnom.ts"],"names":["Omnom","Omnom.constructor","Omnom.changes","Omnom.diff","Omnom.onObject","Omnom.onArray","Omnom.set","Omnom.unset","Omnom.inc","Omnom.push","Omnom.pull","Omnom.pullAll","Omnom.resolve","Omnom.almostEqual"],"mappings":"AAAA,qDAAqD;AACrD,2DAA2D;AAC3D,yDAAyD;AAEzD,IAAO,CAAC,WAAW,QAAQ,CAAC,CAAC;AAC7B,IAAO,OAAO,WAAW,SAAS,CAAC,CAAC;AAIpC,IAAM,KAAK;IACPA,SADEA,KAAKA,CACYA,OAEbA;QAFMC,uBAENA,GAFMA,YAENA;QAFaA,YAAOA,GAAPA,OAAOA,CAEpBA;QACFA,IAAIA,CAACA,QAAQA,GAAGA,EAAEA,CAACA;IACvBA,CAACA;IAUDD,sBAAIA,0BAAOA;aAAXA;YAQIE,MAAMA,CAACA,IAAIA,CAACA,QAAQA,CAACA;QACzBA,CAACA;;;OAAAF;IAMDA,oBAAIA,GAAJA,UAAKA,QAAaA,EAAEA,QAAaA;QAC7BG,IAAIA,CAACA,QAAQA,CAACA,QAAQA,EAAEA,QAAQA,CAACA,CAACA;IACtCA,CAACA;IAOOH,wBAAQA,GAAhBA,UAAiBA,QAAaA,EAAEA,QAAaA,EAA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A,CAACA,MAAMA,EAAEA,GAAGA,EAAEA;YAChCA,EAAEA,CAAAA,CAACA,CAACA,GAAGA,CAACA,OAAOA,CAACA,GAAGA,CAACA,GAAGA,CAACA,CAACA,CAACA;gBAACA,UAAUA,CAACA,IAAIA,CAACA,GAAGA,CAACA,GAAGA,CAACA,CAACA,CAACA;QAEzDA,IAAIA,SAASA,GAAGA,GAAGA,CAACA,MAAMA,GAAGA,GAAGA,CAACA,MAAMA,GAAGA,UAAUA,CAACA,MAAMA,CAACA;QAC5DA,IAAIA,cAAcA,GAAGA,SAASA,GAAGA,UAAUA,CAACA,MAAMA,CAACA;QAEnDA,IAAIA,eAAeA,GAAGA,CAACA,CAACA;QACxBA,GAAGA,CAAAA,CAACA,GAAGA,CAACA,CAACA,GAAGA,CAACA,EAAEA,CAACA,GAAGA,UAAUA,CAACA,MAAMA,EAAEA,CAACA,EAAEA;YACrCA,EAAEA,CAAAA,CAACA,IAAIA,CAACA,WAAWA,CAACA,EAAEA,CAACA,UAAUA,CAACA,CAACA,CAACA,CAACA,EAAEA,EAAEA,CAACA,UAAUA,CAACA,CAACA,CAACA,CAACA,CAACA,GAAGA,CAACA,CAACA;gBAACA,eAAeA,EAAEA,CAACA;QAErFA,MAAMA,CAACA,CAACA,GAAGA,CAACA,cAAcA,GAAGA,SAASA,CAACA,GAAGA,CAACA,eAAeA,GAAGA,UAAUA,CAACA,MAAMA,CAACA,CAACA;IACpFA,CAACA;IACLb,YAACA;AAADA,CAACA,AAtLD,IAsLC;AAxLD,iBAAS,KAAK,CAAC"} \ No newline at end of file +{"version":3,"file":"Omnom.js","sourceRoot":"","sources":["Omnom.ts"],"names":["Omnom","Omnom.constructor","Omnom.changes","Omnom.diff","Omnom.onObject","Omnom.onArray","Omnom.set","Omnom.unset","Omnom.inc","Omnom.push","Omnom.pull","Omnom.pullAll","Omnom.resolve","Omnom.almostEqual"],"mappings":"AACA,AADA,+CAA+C;AAC/C,IAAO,CAAC,WAAW,QAAQ,CAAC,CA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ACA,GAAGA,CAACA,cAAcA,GAAGA,SAASA,CAACA,GAAGA,CAACA,eAAeA,GAAGA,UAAUA,CAACA,MAAMA,CAACA,CAACA;IACpFA,CAACA;IACLb,YAACA;AAADA,CAACA,AApOD,IAoOC;AAtOD,iBAAS,KAAK,CAAC"} \ No newline at end of file diff --git a/lib/utils/Omnom.ts b/lib/utils/Omnom.ts index 5f55bc7..b57fb9a 100644 --- a/lib/utils/Omnom.ts +++ b/lib/utils/Omnom.ts @@ -30,56 +30,64 @@ class Omnom { return this._changes; } - diff(original: number, modified: number); - diff(original: [any], modified: any[]); - diff(original: MongoDB.ObjectID, modified: MongoDB.ObjectID); - diff(original: Object, modified: Object); - diff(original: any, modified: any) { + diff(original: number, modified: number): Omnom; + diff(original: [any], modified: any[]): Omnom; + diff(original: MongoDB.ObjectID, modified: MongoDB.ObjectID): Omnom; + diff(original: Object, modified: Object): Omnom; + diff(original: any, modified: any): Omnom { this.onObject(original, modified); + return this; } + static diff(original: number, modified: number); + static diff(original: [any], modified: any[]); + static diff(original: MongoDB.ObjectID, modified: MongoDB.ObjectID); + static diff(original: Object, modified: Object); + static diff(original: any, modified: any) { + return new Omnom().diff(original, modified).changes; + } private onObject(original: number, modified: number, changePath?: string); private onObject(original: [any], modified: any[], changePath?: string); private onObject(original: MongoDB.ObjectID, modified: MongoDB.ObjectID, changePath?: string); private onObject(original: Object, modified: Object, changePath?: string); private onObject(original: any, modified: any, changePath?: string) { - if(original === undefined || original === null) + if (original === undefined || original === null) return (original !== modified) && this.set(changePath, modified); - if(typeof original == 'number' && typeof modified == 'number' && original !== modified) { - if(this.options.atomicNumbers) return this.inc(changePath, modified - original); + if (typeof original == 'number' && typeof modified == 'number' && original !== modified) { + if (this.options.atomicNumbers) return this.inc(changePath, modified - original); return this.set(changePath, modified); } - if(Array.isArray(original) && Array.isArray(modified)) + if (Array.isArray(original) && Array.isArray(modified)) return this.onArray(original, modified, changePath); - if(original instanceof MongoDB.ObjectID && modified instanceof MongoDB.ObjectID) + if (original instanceof MongoDB.ObjectID && modified instanceof MongoDB.ObjectID) return !original.equals(modified) && this.set(changePath, modified); - if(!_.isPlainObject(original) || !_.isPlainObject(modified)) + if (!_.isPlainObject(original) || !_.isPlainObject(modified)) return !_.isEqual(original, modified) && this.set(changePath, modified); - _.each(modified, function(value, key) { + _.each(modified, function (value, key) { // Handle array diffs in their own special way - if(Array.isArray(value) && Array.isArray(original[key])) this.onArray(original[key], value, this.resolve(changePath, key)); + if (Array.isArray(value) && Array.isArray(original[key])) this.onArray(original[key], value, this.resolve(changePath, key)); // Otherwise, just keep going else this.onObject(original[key], value, this.resolve(changePath, key)); }, this); // Unset removed properties - _.each(original, function(value, key) { - if(modified[key] === undefined || modified[key] === null) return this.unset(this.resolve(changePath, key)); + _.each(original, function (value, key) { + if (modified[key] === undefined || modified[key] === null) return this.unset(this.resolve(changePath, key)); }, this); } private onArray(original: [any], modified: [any], changePath: string) { - var i,j; + var i, j; // Check if we can get from original => modified using just pulls - if(original.length > modified.length) { + if (original.length > modified.length) { var pulls = []; for (i = 0, j = 0; i < original.length && j < modified.length; i++) { if (this.almostEqual(original[i], modified[j])) j++; @@ -99,35 +107,73 @@ class Omnom { // regardless (if we want to do so in a single operation anyway) else return this.set(changePath, modified); } + + // Check if we can get from original => modified using just pushes + if (original.length < modified.length) { + var canPush = true; + for (i = 0; i < original.length; i++) + if (this.almostEqual(original[i], modified[i]) < 1) { + canPush = false; + break; + } + + if (canPush) { + for (i = original.length; i < modified.length; i++) + this.push(changePath, modified[i]); + return; + } + } + + // Otherwise, we need to use $set to generate the new array + + // Check how many manipulations would need to be performed, if it's more than half the array size + // then rather re-create the array + + var sets = []; + var partials = []; + for (i = 0; i < modified.length; i++) { + var equality = this.almostEqual(original[i], modified[i]); + if (equality === 0) sets.push(i); + else if (equality < 1) partials.push(i); + } + + if (sets.length > modified.length / 2) + return this.set(changePath, modified); + + for (i = 0; i < sets.length; i++) + this.set(this.resolve(changePath, sets[i].toString()), modified[sets[i]]); + + for (i = 0; i < partials.length; i++) + this.onObject(original[partials[i]], modified[partials[i]], this.resolve(changePath, partials[i].toString())); } private set(path: string, value: any) { - if(!this.changes.$set) + if (!this.changes.$set) this.changes.$set = {}; this.changes.$set[path] = value; } - private unset(path: string, value: any) { - if(!this.changes.$unset) + private unset(path: string) { + if (!this.changes.$unset) this.changes.$unset = {}; - this.changes.$unset[path] = value; + this.changes.$unset[path] = 1; } private inc(path: string, value: number) { - if(!this.changes.$inc) + if (!this.changes.$inc) this.changes.$inc = {}; this.changes.$inc[path] = value; } private push(path: string, value: any) { - if(!this.changes.$push) + if (!this.changes.$push) this.changes.$push = {}; - if(this.changes.$push[path]) { - if(this.changes.$push[path].$each) + if (this.changes.$push[path]) { + if (this.changes.$push[path].$each) this.changes.$push[path].$each.push(value); else this.changes.$push[path] = { $each: [this.changes.$push[path], value] }; @@ -135,14 +181,14 @@ class Omnom { } private pull(path: string, value: any) { - if(!this.changes.$pull) + if (!this.changes.$pull) this.changes.$pull = {}; - if(this.changes.$pullAll && this.changes.$pullAll[path]) { + if (this.changes.$pullAll && this.changes.$pullAll[path]) { return this.changes.$pullAll[path].push(value); } - if(this.changes.$pull[path]) { + if (this.changes.$pull[path]) { this.pullAll(path, [this.changes.$pull[path], value]); delete this.changes.$pull[path]; return; @@ -152,7 +198,7 @@ class Omnom { } private pullAll(path: string, values: any[]) { - if(!this.changes.$pullAll) + if (!this.changes.$pullAll) this.changes.$pullAll = {}; this.changes.$pullAll[path] = values; @@ -160,29 +206,29 @@ class Omnom { private resolve(...args) { var validArguments = []; - args.forEach(function(arg) { - if(arg) validArguments.push(arg); + args.forEach(function (arg) { + if (arg) validArguments.push(arg); }); return validArguments.join('.'); } private almostEqual(o1: Object, o2: Object); private almostEqual(o1: any, o2: any) { - if(!_.isPlainObject(o1) || !_.isPlainObject(o2)) return o1 == o2 ? 1 : 0; + if (!_.isPlainObject(o1) || !_.isPlainObject(o2)) return o1 == o2 ? 1 : 0; var o1i, o1k = Object.keys(o1); var o2i, o2k = Object.keys(o2); var commonKeys = []; - for(o1i = 0; o1i < o1k.length; o1i++) - if(~o2k.indexOf(o1k[o1i])) commonKeys.push(o1k[o1i]); + for (o1i = 0; o1i < o1k.length; o1i++) + if (~o2k.indexOf(o1k[o1i])) commonKeys.push(o1k[o1i]); var totalKeys = o1k.length + o2k.length - commonKeys.length; var keysDifference = totalKeys - commonKeys.length; var requiredChanges = 0; - for(var i = 0; i < commonKeys.length; i++) - if(this.almostEqual(o1[commonKeys[i]], o2[commonKeys[i]]) < 1) requiredChanges++; + for (var i = 0; i < commonKeys.length; i++) + if (this.almostEqual(o1[commonKeys[i]], o2[commonKeys[i]]) < 1) requiredChanges++; return 1 - (keysDifference / totalKeys) - (requiredChanges / commonKeys.length); } diff --git a/test/Omnom.js b/test/Omnom.js new file mode 100644 index 0000000..f3e8420 --- /dev/null +++ b/test/Omnom.js @@ -0,0 +1,133 @@ +/// +var Omnom = require('../lib/utils/Omnom'); +var MongoDB = require('mongodb'); +describe("Omnom", function () { + it("should correctly diff basic objects", function () { + var oldObject = { + a: 1, + b: 'test', + c: 2, + d: 'constant', + e: 'old' + }; + var newObject = { + a: 3, + b: 'tested', + c: 2, + d: 'constant', + f: 'new' + }; + var expectedDiff = { + $set: { a: 3, b: 'tested', f: 'new' }, + $unset: { e: 1 } + }; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + it('should correctly diff complex objects', function () { + var oldObject = { + a: { value: 1 }, + b: { value1: 1, value2: 1 }, + c: { value: 2 }, + d: { value: {} }, + e: { value: true } + }; + var newObject = { + a: { value: 3 }, + b: { value1: 'tested', value2: 2 }, + c: { value: 2 }, + d: { value: {} }, + e: { value2: false } + }; + var expectedDiff = { + $set: { 'a.value': 3, 'b.value1': 'tested', 'b.value2': 2, 'e.value2': false }, + $unset: { 'e.value': 1 } + }; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + it('should correctly diff ObjectIDs', function () { + var oldID = new MongoDB.ObjectID(); + var newID = MongoDB.ObjectID.createFromHexString(oldID.toHexString()); + var oldObject = { _id: oldID }; + var newObject = { _id: newID }; + var expectedDiff = {}; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + newID = new MongoDB.ObjectID(); + oldObject = { _id: oldID }; + newObject = { _id: newID }; + expectedDiff = { + $set: { _id: newID } + }; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + describe('arrays', function () { + it('should correctly handle arrays which can be pulled', function () { + var oldObject = { a: [1, 2, 3, 4], b: [1, 2, 3, 4] }; + var newObject = { a: [1, 3, 4], b: [1, 3] }; + var expectedDiff = { + $pull: { a: 2 }, + $pullAll: { b: [2, 4] } + }; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + it('should correctly handle arrays which can be pushed', function () { + var oldObject = { a: [1, 2, 3, 4], b: [1, 2, 3, 4] }; + var newObject = { a: [1, 2, 3, 4, 5], b: [1, 2, 3, 4, 5, 6] }; + var expectedDiff = { + $push: { a: 5, b: { $each: [5, 6] } } + }; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + it('should correctly handle arrays which should be replaced', function () { + var oldObject = { a: [1, 2], b: [1, 2, 3] }; + var newObject = { a: [5, 4, 3], b: [5, 4, 3, 2] }; + var expectedDiff = { + $set: { + a: [5, 4, 3], + b: [5, 4, 3, 2] + } + }; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + it("should correctly handle arrays which can be partially modified", function () { + var oldObject = { a: [1, 2, 3, 4], b: [1, 2, 3, 4] }; + var newObject = { a: [1, 2, 5, 4, 5], b: [1, 2, 5, 4, 5, 6] }; + var expectedDiff = { + $set: { + 'a.2': 5, + 'a.4': 5, + 'b.2': 5, + 'b.4': 5, + 'b.5': 6 + } + }; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + it("should correctly diff array elements as objects", function () { + var postDate = new Date(); + var oldObject = { + comments: [ + { id: 1, title: 'Title 1', text: 'test text 1', posted: postDate }, + { id: 2, title: 'Title 2', text: 'test text 2', posted: postDate }, + { id: 3, title: 'Title 3', text: 'test text 3', posted: postDate } + ] + }; + var newDate = new Date(postDate.getTime() + 50); + var newObject = { + comments: [ + { id: 1, title: 'Title 1', text: 'tested text 1', posted: postDate }, + { id: 2, title: 'Title 2', text: 'tested text 2', posted: postDate }, + { id: 3, title: 'Title 3', text: 'test text 3', posted: newDate } + ] + }; + var expectedDiff = { + $set: { + 'comments.0.text': 'tested text 1', + 'comments.1.text': 'tested text 2', + 'comments.2.posted': newDate + } + }; + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + }); +}); +//# sourceMappingURL=Omnom.js.map \ No newline at end of file diff --git a/test/Omnom.js.map b/test/Omnom.js.map new file mode 100644 index 0000000..144ed22 --- /dev/null +++ b/test/Omnom.js.map @@ -0,0 +1 @@ +{"version":3,"file":"Omnom.js","sourceRoot":"","sources":["Omnom.ts"],"names":[],"mappings":"AACA,AADA,4CAA4C;AAC5C,IAAO,KAAK,WAAW,oBAAoB,CAAC,CAAC;AAC7C,IAAO,OAAO,WAAW,SAAS,CAAC,CAAC;AAEpC,QAAQ,CAAC,OAAO,EAAC;IACb,EAAE,CAAC,qCAAqC,EAAC;QACrC,IAAI,SAAS,GAAG;YACZ,CAAC,EAAE,CAAC;YACJ,CAAC,EAAE,MAAM;YACT,CAAC,EAAE,CAAC;YACJ,CAAC,EAAE,UAAU;YACb,CAAC,EAAE,KAAK;SACX,CAAC;QAEF,IAAI,SAAS,GAAG;YACZ,CAAC,EAAE,CAAC;YACJ,CAAC,EAAE,QAAQ;YACX,CAAC,EAAE,CAAC;YACJ,CAAC,EAAE,UAAU;YACb,CAAC,EAAE,KAAK;SACX,CAAC;QAEF,IAAI,YAAY,GAAG;YACf,IAAI,EA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AAS,EAAE,IAAI,EAAE,eAAe,EAAE,MAAM,EAAE,QAAQ,EAAE;oBACpE,EAAE,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE;iBACpE;aACJ,CAAC;YAEF,IAAI,YAAY,GAAG;gBACf,IAAI,EAAE;oBACF,iBAAiB,EAAE,eAAe;oBAClC,iBAAiB,EAAE,eAAe;oBAClC,mBAAmB,EAAE,OAAO;iBAC/B;aACJ,CAAC;YAEF,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QACpF,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/test/Omnom.ts b/test/Omnom.ts new file mode 100644 index 0000000..00f7916 --- /dev/null +++ b/test/Omnom.ts @@ -0,0 +1,160 @@ +/// +import Omnom = require('../lib/utils/Omnom'); +import MongoDB = require('mongodb'); + +describe("Omnom",() => { + it("should correctly diff basic objects",() => { + var oldObject = { + a: 1, + b: 'test', + c: 2, + d: 'constant', + e: 'old' + }; + + var newObject = { + a: 3, + b: 'tested', + c: 2, + d: 'constant', + f: 'new' + }; + + var expectedDiff = { + $set: { a: 3, b: 'tested', f: 'new' }, + $unset: { e: 1 } + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + + it('should correctly diff complex objects', function () { + var oldObject = { + a: { value: 1 }, + b: { value1: 1, value2: 1 }, + c: { value: 2 }, + d: { value: {} }, + e: { value: true } + }; + + var newObject = { + a: { value: 3 }, + b: { value1: 'tested', value2: 2 }, + c: { value: 2 }, + d: { value: {} }, + e: { value2: false } + }; + + var expectedDiff = { + $set: { 'a.value': 3, 'b.value1': 'tested', 'b.value2': 2, 'e.value2': false }, + $unset: { 'e.value': 1 } + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + + it('should correctly diff ObjectIDs', function () { + var oldID = new MongoDB.ObjectID(); + var newID = MongoDB.ObjectID.createFromHexString(oldID.toHexString()); + + var oldObject = { _id: oldID }; + var newObject = { _id: newID }; + var expectedDiff = { + + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + + newID = new MongoDB.ObjectID(); + + oldObject = { _id: oldID }; + newObject = { _id: newID }; + expectedDiff = { + $set: { _id: newID } + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + + describe('arrays', function () { + it('should correctly handle arrays which can be pulled', function () { + var oldObject = { a: [1, 2, 3, 4], b: [1, 2, 3, 4] }; + var newObject = { a: [1, 3, 4], b: [1, 3] }; + var expectedDiff = { + $pull: { a: 2 }, + $pullAll: { b: [2, 4] } + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + + it('should correctly handle arrays which can be pushed', function () { + var oldObject = { a: [1, 2, 3, 4], b: [1, 2, 3, 4] }; + var newObject = { a: [1, 2, 3, 4, 5], b: [1, 2, 3, 4, 5, 6] }; + var expectedDiff = { + $push: { a: 5, b: { $each: [5, 6] } } + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + + it('should correctly handle arrays which should be replaced', function () { + var oldObject = { a: [1, 2], b: [1, 2, 3] }; + var newObject = { a: [5, 4, 3], b: [5, 4, 3, 2] }; + var expectedDiff = { + $set: { + a: [5, 4, 3], + b: [5, 4, 3, 2] + } + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + + it("should correctly handle arrays which can be partially modified", function () { + var oldObject = { a: [1, 2, 3, 4], b: [1, 2, 3, 4] }; + var newObject = { a: [1, 2, 5, 4, 5], b: [1, 2, 5, 4, 5, 6] }; + var expectedDiff = { + $set: { + 'a.2': 5, + 'a.4': 5, + 'b.2': 5, + 'b.4': 5, + 'b.5': 6 + } + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + + it("should correctly diff array elements as objects", function () { + var postDate = new Date(); + var oldObject = { + comments: [ + { id: 1, title: 'Title 1', text: 'test text 1', posted: postDate }, + { id: 2, title: 'Title 2', text: 'test text 2', posted: postDate }, + { id: 3, title: 'Title 3', text: 'test text 3', posted: postDate } + ] + }; + + var newDate = new Date(postDate.getTime() + 50); + var newObject = { + comments: [ + { id: 1, title: 'Title 1', text: 'tested text 1', posted: postDate }, + { id: 2, title: 'Title 2', text: 'tested text 2', posted: postDate }, + { id: 3, title: 'Title 3', text: 'test text 3', posted: newDate } + ] + }; + + var expectedDiff = { + $set: { + 'comments.0.text': 'tested text 1', + 'comments.1.text': 'tested text 2', + 'comments.2.posted': newDate + } + }; + + chai.expect(Omnom.diff(oldObject, newObject)).to.exist.and.be.eql(expectedDiff); + }); + }); +}); \ No newline at end of file