瀏覽代碼

Added some more $limit test cases and fixed some issues with $match, $limit, and $project. refs #1002 #1004 #1006

http://source.rd.rcg.local/trac/eagle6/changeset/1333/Eagle6_SVN
Philip Murray 12 年之前
父節點
當前提交
cfc877200b

+ 13 - 13
lib/pipeline/Pipeline.js

@@ -6,22 +6,22 @@ var Pipeline = module.exports = (function(){
 		
 	}, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 	
-//	var GroupDocumentSource = require('./documentSources/GroupDocumentSource'),
-//		LimitDocumentSource = require('./documentSources/LimitDocumentSource'),
-//		MatchDocumentSource = require('./documentSources/MatchDocumentSource'),
-//		ProjectDocumentSource = require('./documentSources/ProjectDocumentSource'),
+	var LimitDocumentSource = require('./documentSources/LimitDocumentSource'),
+		MatchDocumentSource = require('./documentSources/MatchDocumentSource'),
+		ProjectDocumentSource = require('./documentSources/ProjectDocumentSource');
+//		GroupDocumentSource = require('./documentSources/GroupDocumentSource'),
 //		SkipDocumentSource = require('./documentSources/SkipDocumentSource'),
 //		SortDocumentSource = require('./documentSources/SortDocumentSource'),
 //		UnwindDocumentSource = require('./documentSources/UnwindDocumentSource');
 	
 	klass.StageDesc = {};//attaching this to the class for test cases
-//	StageDesc[GroupDocumentSource.groupName] = GroupDocumentSource;
-//	StageDesc[LimitDocumentSource.limitName] = LimitDocumentSource;
-//	StageDesc[MatchDocumentSource.matchName] = MatchDocumentSource;
-//	StageDesc[ProjectDocumentSource.projectName] = ProjectDocumentSource;
-//	StageDesc[SkipDocumentSource.skipName] = SkipDocumentSource;
-//	StageDesc[SortDocumentSource.sortName] = SortDocumentSource;
-//	StageDesc[UnwindDocumentSource.unwindName] = UnwindDocumentSource;
+	klass.StageDesc[LimitDocumentSource.limitName] = LimitDocumentSource;
+	klass.StageDesc[MatchDocumentSource.matchName] = MatchDocumentSource;
+	klass.StageDesc[ProjectDocumentSource.projectName] = ProjectDocumentSource;
+//	klass.StageDesc[GroupDocumentSource.groupName] = GroupDocumentSource;
+//	klass.StageDesc[SkipDocumentSource.skipName] = SkipDocumentSource;
+//	klass.StageDesc[SortDocumentSource.sortName] = SortDocumentSource;
+//	klass.StageDesc[UnwindDocumentSource.unwindName] = UnwindDocumentSource;
 	
     /**
      * Create a pipeline from the command.
@@ -84,9 +84,9 @@ var Pipeline = module.exports = (function(){
          */
         for(var srcn = sourceVector.length, srci = 1; srci < srcn; ++srci) {
             var source = sourceVector[srci];
-            if (source.constructor === klass.MatchDocumentSource) { //TODO: remove 'klass.' once match is implemented!!!
+            if (source.constructor === MatchDocumentSource) {
                 var previous = sourceVector[srci - 1];
-                if (previous.constructor === klass.SortDocumentSource) { //TODO: remove 'sort.' once match is implemented!!!
+                if (previous.constructor === klass.SortDocumentSource) { //TODO: remove 'sort.' once sort is implemented!!!
                     /* swap this item with the previous */
                     sourceVector[srci - 1] = source;
                     sourceVector[srci] = previous;

+ 4 - 3
lib/pipeline/documentSources/LimitDocumentSource.js

@@ -16,8 +16,9 @@ var LimitDocumentSource = module.exports = (function(){
 		this.count = 0;
 	}, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
+	klass.limitName = "$limit";
 	proto.getSourceName = function getSourceName(){
-		return "$limit";
+		return klass.limitName;
 	};
 	
 	proto.getFactory = function getFactory(){
@@ -35,9 +36,9 @@ var LimitDocumentSource = module.exports = (function(){
 	 * @return {bool} return whether we can coalese together
 	**/
 	proto.coalesce = function coalesce(nextSource) {
-		nextLimit =	nextSource.get();
+		var nextLimit =	nextSource.constructor === LimitDocumentSource?nextSource:null;
 
-		/* if it's not another $skip, we can't coalesce */
+		/* if it's not another $limit, we can't coalesce */
 		if (!nextLimit)
 			return false;
 		/* we need to limit by the minimum of the two limits */

+ 3 - 3
lib/pipeline/documentSources/MatchDocumentSource.js

@@ -14,7 +14,6 @@ var MatchDocumentSource = module.exports = (function(){
 		if(arguments.length !== 1) throw new Error("one arg expected");
 		base.call(this);
 		this.matcher = sift(query);
-		this.matchName = "$match";
 	}, base = require('./FilterBaseDocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 	proto.extend = function extend(obj, withObj){
@@ -38,12 +37,13 @@ var MatchDocumentSource = module.exports = (function(){
 	// DEPENDENCIES
 	var sift = require("sift");
 
+	klass.matchName = "$match";
 	proto.getSourceName = function getSourceName(){
-		return this.matchName;
+		return klass.matchName;
 	};
 
     /**
-     * Create an object that represents the document source.  The object
+	 * Create an object that represents the document source.  The object
      * will have a single field whose name is the source's name.  This
      * will be used by the default implementation of addToJsonArray()
      * to add this object to a pipeline being represented in JSON.

+ 3 - 3
lib/pipeline/documentSources/ProjectDocumentSource.js

@@ -13,7 +13,6 @@ var ProjectDocumentSource = module.exports = (function(){
 		if(arguments.length !== 0) throw new Error("zero args expected");
 		base.call(this);
 		this.EO = new ObjectExpression();
-		this.projectName = "$project";
 	}, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 	// DEPENDENCIES
@@ -27,8 +26,9 @@ var ProjectDocumentSource = module.exports = (function(){
 	 *	
 	 *	@return {string} the name of project
 	**/
+	klass.projectName = "$project";
 	proto.getSourceName = function getSourceName() {
-        return this.projectName;
+        return klass.projectName;
     };
 
 	/**
@@ -56,7 +56,7 @@ var ProjectDocumentSource = module.exports = (function(){
 	*	@return A document that represents this base document
 	**/
 	proto.getCurrent = function getCurrent() {
-		inDocument = base.pSource.getCurrent();
+		var inDocument = base.pSource.getCurrent();
 		if (!inDocument) {
 			throw new Error('inDocument must be an object');
 		}

+ 0 - 10
test/lib/pipeline/Pipeline.js

@@ -45,16 +45,6 @@ module.exports = {
 			return klass;
 		})();
 		
-		//TODO:remove this once Match is implemented!!!
-		Pipeline.MatchDocumentSource = (function(){
-			var klass = function MatchDocumentSource(){
-				
-			}, base = require('../../../lib/pipeline/documentSources/DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
-			klass.matchName = "$match";
-			return klass;
-		})();
-		Pipeline.StageDesc.$match = Pipeline.MatchDocumentSource;
-		
 		//TODO:remove this once Sort is implemented!!!
 		Pipeline.SortDocumentSource = (function(){
 			var klass = function SortDocumentSource(){

+ 0 - 10
test/lib/pipeline/PipelineD.js

@@ -59,16 +59,6 @@ module.exports = {
 			return klass;
 		})();
 		
-		//TODO:remove this once Match is implemented!!!
-		Pipeline.MatchDocumentSource = (function(){
-			var klass = function MatchDocumentSource(){
-				
-			}, base = require('../../../lib/pipeline/documentSources/DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
-			klass.matchName = "$match";
-			return klass;
-		})();
-		Pipeline.StageDesc.$match = Pipeline.MatchDocumentSource;
-		
 		//TODO:remove this once Sort is implemented!!!
 		Pipeline.SortDocumentSource = (function(){
 			var klass = function SortDocumentSource(){

+ 62 - 6
test/lib/pipeline/documentSources/LimitDocumentSource.js

@@ -32,12 +32,15 @@ module.exports = {
 
 		},
 
-		/*
 		"#coalesce()": {
 
-			"should return false if nextSource is not $skip": function dontSkip(){
+			"should return false if nextSource is not $limit": function dontSkip(){
+				var lds = new LimitDocumentSource();
+				assert.equal(lds.coalesce({}), false);
 			},
-			"should return true if nextSource is $skip": function changeLimit(){
+			"should return true if nextSource is $limit": function changeLimit(){
+				var lds = new LimitDocumentSource();
+				assert.equal(lds.coalesce(new LimitDocumentSource()), true);
 			}
 
 		},
@@ -45,8 +48,37 @@ module.exports = {
 		"#eof()": {
 
 			"should return true if there are no more sources": function noSources(){
+				var lds = new LimitDocumentSource();
+				lds.limit = 9;
+				lds.count = 0;
+				lds.pSource = {
+					eof: function(){
+						return true;
+					}
+				};
+				assert.equal(lds.eof(), true);
 			},
 			"should return true if limit is hit": function hitLimit(){
+				var lds = new LimitDocumentSource();
+				lds.limit = 9;
+				lds.count = 9;
+				lds.pSource = {
+					eof: function(){
+						return false;
+					}
+				};
+				assert.equal(lds.eof(), true);
+			},
+			"should return false if limit is not hit and there are more documents": function hitLimit(){
+				var lds = new LimitDocumentSource();
+				lds.limit = 10;
+				lds.count = 9;
+				lds.pSource = {
+					eof: function(){
+						return false;
+					}
+				};
+				assert.equal(lds.eof(), false);
 			}
 
 		},
@@ -56,8 +88,8 @@ module.exports = {
 			"should return the current document source": function currSource(){
 				var lds = new LimitDocumentSource();
 				lds.limit = 1;
-				lds.pSource = { item:1 };
-				assert.strictEqual(lds.getCurrent(), { item:1 }); 
+				lds.pSource = {getCurrent:function(){return { item:1 };}};
+				assert.deepEqual(lds.getCurrent(), { item:1 }); 
 			}
 
 		},
@@ -65,13 +97,37 @@ module.exports = {
 		"#advance()": {
 
 			"should return true for moving to the next source": function nextSource(){
+				var lds = new LimitDocumentSource();
+				lds.count = 0;
+				lds.limit = 2;
+				lds.pSource = {
+					getCurrent:function(){return { item:1 };},
+					advance:function(){return true;}
+				};
+				assert.strictEqual(lds.advance(), true); 
 			},
 
 			"should return false for no sources remaining": function noMoar(){
+				var lds = new LimitDocumentSource();
+				lds.limit = 1;
+				lds.pSource = {
+					getCurrent:function(){return { item:1 };},
+					advance:function(){return false;}
+				};
+				assert.strictEqual(lds.advance(), false); 
+			},
+
+			"should return false if we hit our limit": function noMoar(){
+				var lds = new LimitDocumentSource();
+				lds.limit = 1;
+				lds.pSource = {
+					getCurrent:function(){return { item:1 };},
+					advance:function(){return true;}
+				};
+				assert.strictEqual(lds.advance(), false); 
 			}
 
 		},
-		*/
 
 		"#sourceToJson()": {