Prechádzať zdrojové kódy

Fixes #1005. Finished hammering out truth logic in compare. All tests ported and working.

http://source.rd.rcg.local/trac/eagle6/changeset/1354/Eagle6_SVN
Spencer Rathbun 12 rokov pred
rodič
commit
2e3e667202

+ 7 - 10
lib/pipeline/documentSources/SortDocumentSource.js

@@ -42,14 +42,14 @@ var SortDocumentSource = module.exports = (function(){
 	};
 
 	klass.GetDepsReturn = {
-		SEE_NEXT:"SEE_NEXT", // Add the next Source's deps to the set
+		SEE_NEXT:"SEE_NEXT" // Add the next Source's deps to the set
 	};
 
-	proto.getDependencies = function getDependencies() {
+	proto.getDependencies = function getDependencies(deps) {
 		for(var i = 0; i < this.vSortKey.length; ++i) {
 			this.vSortKey[i].addDependencies(deps);
 		}
-		return this.GetDepsReturn.SEE_NEXT;
+		return klass.GetDepsReturn.SEE_NEXT;
 	};
 
 	/**
@@ -129,13 +129,10 @@ var SortDocumentSource = module.exports = (function(){
 	proto.addKey = function addKey(fieldPath, ascending) {
 		var pathExpr = new FieldPathExpression(fieldPath);
 		this.vSortKey.push(pathExpr);
-		if (ascending == -1)
-			this.vAscending.push(0);
-		else if (typeof ascending !== "undefined" && typeof ascending !== null)
+		if (ascending === true || ascending === false) 
 			this.vAscending.push(ascending);
 		else
-			this.vAscending.push(0);
-
+			throw new Error("ascending must be true or false");
 	};
 
 	proto.populate = function populate() {
@@ -212,7 +209,7 @@ var SortDocumentSource = module.exports = (function(){
 			/* create the "field name" */
 			var ss = this.vSortKey[i].getFieldPath(usePrefix); // renamed write to get
 			/* push a named integer based on the sort order */
-			builder[ss] = (this.vAscending[i] ? 1 : 0);
+			builder[ss] = ((this.vAscending[i] > 0) ? 1 : -1);
 		}
 	};
 
@@ -235,7 +232,7 @@ var SortDocumentSource = module.exports = (function(){
 			if (typeof JsonElement[key] !== "number") throw new Error("code 15974; " + klass.sortName + " key ordering must be specified using a number");
 
 			sortOrder = JsonElement[key];
-			if ((sortOrder != 1) && (sortOrder !== 0)) throw new Error("code 15975; " + klass.sortName + " key ordering must be 1 (for ascending) or -1 (for descending)");
+			if ((sortOrder != 1) && (sortOrder !== -1)) throw new Error("code 15975; " + klass.sortName + " key ordering must be 1 (for ascending) or 0 (for descending)");
 
 			nextSort.addKey(key, (sortOrder > 0));
 			++sortKeys;

+ 69 - 8
test/lib/pipeline/documentSources/SortDocumentSource.js

@@ -151,7 +151,7 @@ module.exports = {
 
 			"should throw an exception when passed an object with a non valid number value": function createTest(){
 				assert.throws(function() {
-					var sds = SortDocumentSource.createFromJson({a:0});
+					var sds = SortDocumentSource.createFromJson({a:14});
 				});
 			}
 
@@ -164,7 +164,7 @@ module.exports = {
 				cwc._cursor = new Cursor( [{_id:0, a: 1}] );
 				var cds = new CursorDocumentSource(cwc);
 				var sds = new SortDocumentSource();
-				sds.addKey("_id");
+				sds.addKey("_id", false);
 				sds.setSource(cds);
 				assert.deepEqual(sds.getCurrent(), {_id:0, a:1}); 
 			},
@@ -175,7 +175,7 @@ module.exports = {
 				cwc._cursor = new Cursor( l );
 				var cds = new CursorDocumentSource(cwc);
 				var sds = new SortDocumentSource();
-				sds.addKey("_id");
+				sds.addKey("_id", false);
 				sds.setSource(cds);
 				var c = [];
 				while (!sds.eof()) {
@@ -207,8 +207,8 @@ module.exports = {
 				cwc._cursor = new Cursor( l );
 				var cds = new CursorDocumentSource(cwc);
 				var sds = new SortDocumentSource();
-				sds.addKey("a");
-				sds.addKey("b");
+				sds.addKey("a", false);
+				sds.addKey("b", false);
 				sds.setSource(cds);
 				var c = [];
 				while (!sds.eof()) {
@@ -258,7 +258,7 @@ module.exports = {
 				cwc._cursor = new Cursor( l );
 				var cds = new CursorDocumentSource(cwc);
 				var sds = new SortDocumentSource();
-				sds.addKey("a");
+				sds.addKey("a", false);
 				assert.throws(sds.setSource(cds));
 			},
 
@@ -268,7 +268,7 @@ module.exports = {
 				cwc._cursor = new Cursor( l );
 				var cds = new CursorDocumentSource(cwc);
 				var sds = new SortDocumentSource();
-				sds.addKey("a");
+				sds.addKey("a", true);
 				sds.setSource(cds);
 				var c = [];
 				while (!sds.eof()) {
@@ -278,8 +278,69 @@ module.exports = {
 				assert.deepEqual(c, [{_id:1}, {_id:0, a:1}]); 
 			},
 
-		}
+			"should sort docs with null fields": function nullFields() {
+				var cwc = new CursorDocumentSource.CursorWithContext();
+				var l = [{_id:0, a: 1}, {_id:1, a: null}];
+				cwc._cursor = new Cursor( l );
+				var cds = new CursorDocumentSource(cwc);
+				var sds = new SortDocumentSource();
+				sds.addKey("a", true);
+				sds.setSource(cds);
+				var c = [];
+				while (!sds.eof()) {
+					c.push(sds.getCurrent());
+					sds.advance();
+				}
+				assert.deepEqual(c, [{_id:1, a:null}, {_id:0, a:1}]); 
+			},
+
+			"should not support a missing object nested in an array": function missingObjectWithinArray() {
+				var cwc = new CursorDocumentSource.CursorWithContext();
+				var l = [{_id:0, a: [1]}, {_id:1, a:[0]}];
+				cwc._cursor = new Cursor( l );
+				var cds = new CursorDocumentSource(cwc);
+				var sds = new SortDocumentSource();
+				assert.throws(function() {
+					sds.addKey("a.b", false);
+					sds.setSource(cds);
+					var c = [];
+					while (!sds.eof()) {
+						c.push(sds.getCurrent());
+						sds.advance();
+					}
+				});
+			},
 
+			"should compare nested values from within an array": function extractArrayValues() {
+				var cwc = new CursorDocumentSource.CursorWithContext();
+				var l = [{_id:0,a:[{b:1},{b:2}]}, {_id:1,a:[{b:1},{b:1}]} ];
+				cwc._cursor = new Cursor( l );
+				var cds = new CursorDocumentSource(cwc);
+				var sds = new SortDocumentSource();
+				sds.addKey("a.b", true);
+				sds.setSource(cds);
+				var c = [];
+				while (!sds.eof()) {
+					c.push(sds.getCurrent());
+					sds.advance();
+				}
+				assert.deepEqual(c, [{_id:1,a:[{b:1},{b:1}]},{_id:0,a:[{b:1},{b:2}]}]); 
+			}
+
+		},
+
+		"#dependencies": {
+			"should have Dependant field paths": function dependencies() {
+				var sds = new SortDocumentSource();
+				sds.addKey("a", true);
+				sds.addKey("b.c", false);
+				var deps = [];
+				assert.equal("SEE_NEXT", sds.getDependencies(deps));
+				assert.equal(2, deps.length);
+				assert.equal(1, deps.filter(function(val) { return "a" == val; }).length);
+				assert.equal(1, deps.filter(function(val) { return "a" == val; }).length);
+			}
+		}
 
 	}