Browse Source

EAGLESIX-3083: All test cases pass

David Aebersold 11 years ago
parent
commit
cbef96748b

+ 5 - 4
lib/pipeline/documentSources/SortDocumentSource.js

@@ -136,10 +136,12 @@ proto.serializeToArray = function serializeToArray(array, explain) {
 		doc.limit = this.limitSrc ? this.limitSrc.getLimit() : undefined;
 		array.push(doc);
 	} else { // one Value for $sort and maybe a Value for $limit
-		var inner = this.serializeSortKey(explain);
+		var inner = {};
+		inner = this.serializeSortKey(explain);
 		if (this._mergePresorted)
 			inner.$mergePresorted = true;
 		doc[this.getSourceName()] = inner;
+
 		array.push(doc);
 
 		if (this.limitSrc)
@@ -374,13 +376,12 @@ proto.serializeSortKey = function serializeSortKey(explain) {
 	var n = this.vSortKey.length;
 	for (var i = 0; i < n; i++) {
 		if ( this.vSortKey[i] instanceof FieldPathExpression ) {
-			var fieldPath = this.vSortKey[i].getFieldPath();
-
+			var fieldPath = this.vSortKey[i].getFieldPath(false).fieldNames.slice(1).join('.');
 			// append a named integer based on the sort order
 			keyObj[fieldPath] = this.vAscending[i] ? 1 : -1;
 		} else {
 			// other expressions use a made-up field name
-			keyObj[{"$computed":i}] = this.vSortKey[i].serializeToArray(explain);
+			keyObj[{"$computed":i}] = this.vSortKey[i].serialize(explain);
 		}
 	}
 	return keyObj;

+ 29 - 28
test/lib/pipeline/documentSources/SortDocumentSource.js

@@ -13,32 +13,32 @@ module.exports = {
 
 	"SortDocumentSource": {
 
-		// "constructor()": {
+		"constructor()": {
 
-		// 	"should not throw Error when constructing without args": function testConstructor(){
-		// 		assert.doesNotThrow(function(){
-		// 			new SortDocumentSource();
-		// 		});
-		// 	}
+			"should not throw Error when constructing without args": function testConstructor(){
+				assert.doesNotThrow(function(){
+					new SortDocumentSource();
+				});
+			}
 
-		// },
+		},
 
-		// "#getSourceName()": {
+		"#getSourceName()": {
 
-		// 	"should return the correct source name; $sort": function testSourceName(){
-		// 		var sds = new SortDocumentSource();
-		// 		assert.strictEqual(sds.getSourceName(), "$sort");
-		// 	}
+			"should return the correct source name; $sort": function testSourceName(){
+				var sds = new SortDocumentSource();
+				assert.strictEqual(sds.getSourceName(), "$sort");
+			}
 
-		// },
+		},
 
-		// "#getFactory()": {
+		"#getFactory()": {
 
-		// 	"should return the constructor for this class": function factoryIsConstructor(){
-		// 		assert.strictEqual(new SortDocumentSource().getFactory(), SortDocumentSource);
-		// 	}
+			"should return the constructor for this class": function factoryIsConstructor(){
+				assert.strictEqual(new SortDocumentSource().getFactory(), SortDocumentSource);
+			}
 
-		// },
+		},
 
 		"#getNext()": {
 			/** Assert that iterator state accessors consistently report the source is exhausted. */
@@ -85,10 +85,10 @@ module.exports = {
 				var cwc = new CursorDocumentSource.CursorWithContext();
 				cwc._cursor = new Cursor( [{a: 1}, {b:2}] );
 				var cds = new CursorDocumentSource(cwc);
-				var sds = SortDocumentSource.createFromJson({"sort":-1});
+				var sds = SortDocumentSource.createFromJson({"sort":1});
 				sds.setSource(cds);
 				sds.getNext(function(err, doc) {
-					assert.deepEqual(doc, {b:2});
+					assert.deepEqual(doc, {a: 1});
 					return next();
 				});
 			},
@@ -97,11 +97,11 @@ module.exports = {
 				var cwc = new CursorDocumentSource.CursorWithContext();
 				cwc._cursor = new Cursor( [{a: 1}, {b:2}] );
 				var cds = new CursorDocumentSource(cwc);
-				var sds = SortDocumentSource.createFromJson({"sort":-1});
+				var sds = SortDocumentSource.createFromJson({"sort":1});
 				sds.setSource(cds);
 				sds.getNext(function(err, doc) {
 					sds.getNext(function(err, doc) {
-						assert.deepEqual(doc, {a:1});
+						assert.deepEqual(doc, {b:2});
 						next();
 					});
 				});
@@ -130,7 +130,7 @@ module.exports = {
 				var array = [];
 				sds.serializeToArray(array, false);
 
-				assert.deepEqual(array, [ { '$sort': { '[object Object]': 1 } } ]);
+				assert.deepEqual(array, [{"$sort":{"sort":1}}]);
 			},
 
 			"should create an object representation of the SortDocumentSource": function serializeToArrayTest(){
@@ -139,7 +139,7 @@ module.exports = {
 				sds.vSortKey.push(new FieldPathExpression("b", fieldPathVar) );
 				var array = [];
 				sds.serializeToArray(array, false);
-				assert.deepEqual(array,  [{"$sort":{"[object Object]":-1}}] );
+				assert.deepEqual(array, [{"$sort":{"":-1}}] );
 			}
 
 		},
@@ -151,7 +151,7 @@ module.exports = {
 				assert.strictEqual(sds.constructor, SortDocumentSource);
 				var t = [];
 				sds.serializeToArray(t, false);
-				assert.deepEqual(t, [{"$sort":{"[object Object]":1}}]);
+				assert.deepEqual(t, [{"$sort":{"a":1}}] );
 			},
 
 			"should return a new SortDocumentSource object from an input JSON object with a descending field": function createTest(){
@@ -159,7 +159,7 @@ module.exports = {
 				assert.strictEqual(sds.constructor, SortDocumentSource);
 				var t = [];
 				sds.serializeToArray(t, false);
-				assert.deepEqual(t, [{ "$sort": { "[object Object]": -1 } }]);
+				assert.deepEqual(t,  [{"$sort":{"a":-1}}]);
 			},
 
 			"should return a new SortDocumentSource object from an input JSON object with dotted paths": function createTest(){
@@ -167,7 +167,7 @@ module.exports = {
 				assert.strictEqual(sds.constructor, SortDocumentSource);
 				var t = [];
 				sds.serializeToArray(t, false);
-				assert.deepEqual(t, [{ "$sort": { "[object Object]" : 1  } }]);
+				assert.deepEqual(t, [{"$sort":{"a.b":1}}]);
 			},
 
 			"should throw an exception when not passed an object": function createTest(){
@@ -507,7 +507,8 @@ module.exports = {
 				var deps = {fields: {}, needWholeDocument: false, needTextScore: false};
 
 				assert.equal('SEE_NEXT', sds.getDependencies(deps));
-				assert.equal(2, Object.keys(deps.fields).length);
+				// Sort keys are now part of deps fields.
+				assert.equal(3, Object.keys(deps.fields).length);
 			 	assert.equal(1, deps.fields.a);
 				assert.equal(1, deps.fields['b.c']);
 				assert.equal(false, deps.needWholeDocument);