Browse Source

Merge pull request #12 in EAGLE6/mungedb-aggregate from feature/EAGLESIX-2353-url-facilities-in-pipelines to master

* commit 'b975d40692743f9f7b8b177b3bde07dcc935daf2':
  EAGLESIX-2353 Darn lint errors!
  EAGLESIX-2353 Split into two tests a single test case that had been broken by a silt upgrade.
  EAGLESIX-2353 - Moving code to check that there is a valid range of operands from NaryExpression to a new extended version that will live in mungedb-aggregate-common.
  Fixed a test case that broke when we upgraded sift.
  EAGLESIX-2353 added a test case to show that the minimum number of arguments can be 0.
  EAGLESIX-2353 added to NaryExpression a new utility function that ensures the number of operands falls within an allowable range
Tony Ennis 11 năm trước cách đây
mục cha
commit
91d0b67b80

+ 1 - 1
lib/pipeline/expressions/NaryExpression.js

@@ -135,4 +135,4 @@ proto.checkArgLimit = function checkArgLimit(maxArgs) {
  **/
 proto.checkArgCount = function checkArgCount(reqArgs) {
 	if (this.operands.length !== reqArgs) throw new Error(this.getOpName() + ":  insufficient operands; " + reqArgs + " required, only got " + this.operands.length + "; code 15997");
-};
+};

+ 73 - 40
test/lib/pipeline/Pipeline.js

@@ -8,49 +8,49 @@ module.exports = {
 
 	"Pipeline": {
 
-		before: function(){
+		before: function () {
 
-			Pipeline.stageDesc.$test = (function(){
-				var klass = function TestDocumentSource(options, ctx){
+			Pipeline.stageDesc.$test = (function () {
+				var klass = function TestDocumentSource(options, ctx) {
 					base.call(this, ctx);
-					
+
 					this.shouldCoalesce = options.coalesce;
 					this.coalesceWasCalled = false;
 					this.optimizeWasCalled = false;
-					
+
 					this.current = 5;
-					
-				}, TestDocumentSource = klass, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
-				
-				
-				proto.coalesce = function(){
+
+				}, TestDocumentSource = klass, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
+
+
+				proto.coalesce = function () {
 					this.coalesceWasCalled = true;
 					var c = this.shouldCoalesce;//only coalesce with the first thing we find
 					this.shouldCoalesce = false;
 					return c;
 				};
 
-				proto.optimize = function(){
+				proto.optimize = function () {
 					this.optimizeWasCalled = true;
 				};
-				
-				proto.eof = function(){
+
+				proto.eof = function () {
 					return this.current < 0;
 				};
 
-				proto.advance = function(){
+				proto.advance = function () {
 					this.current = this.current - 1;
 					return !this.eof();
 				};
 
-				proto.getCurrent = function(){
+				proto.getCurrent = function () {
 					return this.current;
 				};
 
-				klass.createFromJson = function(options, ctx){
+				klass.createFromJson = function (options, ctx) {
 					return new TestDocumentSource(options, ctx);
 				};
-				
+
 				return klass;
 			})().createFromJson;
 
@@ -59,42 +59,59 @@ module.exports = {
 		"parseCommand": {
 
 			"should throw Error if given non-objects in the array": function () {
-				assert.throws(function(){
-					Pipeline.parseCommand({pipeline:[5]});
+				assert.throws(function () {
+					Pipeline.parseCommand({pipeline: [5]});
 				});
 			},
 
 			"should throw Error if given objects with more / less than one field": function () {
-				assert.throws(function(){
-					Pipeline.parseCommand({pipeline:[{}]});
-					Pipeline.parseCommand({pipeline:[{a:1,b:2}]});
+				assert.throws(function () {
+					Pipeline.parseCommand({pipeline: [
+						{}
+					]});
+					Pipeline.parseCommand({pipeline: [
+						{a: 1, b: 2}
+					]});
 				});
 			},
 
 			"should throw Error on unknown document sources": function () {
-				assert.throws(function(){
-					Pipeline.parseCommand({pipeline:[{$foo:"$sdfdf"}]});
+				assert.throws(function () {
+					Pipeline.parseCommand({pipeline: [
+						{$foo: "$sdfdf"}
+					]});
 				});
 			},
 
 			"should swap $match and $sort if the $match immediately follows the $sort": function () {
-				var p = Pipeline.parseCommand({pipeline:[{$sort:{"xyz":1}}, {$match:{}}]});
+				var p = Pipeline.parseCommand({pipeline: [
+					{$sort: {"xyz": 1}},
+					{$match: {}}
+				]});
 				assert.equal(p.sourceVector[0].constructor.matchName, "$match");
 				assert.equal(p.sourceVector[1].constructor.sortName, "$sort");
 			},
 
 			"should attempt to coalesce all sources": function () {
-				var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:true}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]});
+				var p = Pipeline.parseCommand({pipeline: [
+					{$test: {coalesce: false}},
+					{$test: {coalesce: true}},
+					{$test: {coalesce: false}},
+					{$test: {coalesce: false}}
+				]});
 				assert.equal(p.sourceVector.length, 3);
-				p.sourceVector.slice(0,-1).forEach(function(source){
+				p.sourceVector.slice(0, -1).forEach(function (source) {
 					assert.equal(source.coalesceWasCalled, true);
 				});
-				assert.equal(p.sourceVector[p.sourceVector.length -1].coalesceWasCalled, false);
+				assert.equal(p.sourceVector[p.sourceVector.length - 1].coalesceWasCalled, false);
 			},
 
 			"should optimize all sources": function () {
-				var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}]});
-				p.sourceVector.forEach(function(source){
+				var p = Pipeline.parseCommand({pipeline: [
+					{$test: {coalesce: false}},
+					{$test: {coalesce: false}}
+				]});
+				p.sourceVector.forEach(function (source) {
 					assert.equal(source.optimizeWasCalled, true);
 				});
 			}
@@ -104,8 +121,12 @@ module.exports = {
 		"#run": {
 
 			"should set the parent source for all sources in the pipeline except the first one": function (next) {
-				var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]});
-				p.run(new DocumentSource({}), function(err, results){
+				var p = Pipeline.parseCommand({pipeline: [
+					{$test: {coalesce: false}},
+					{$test: {coalesce: false}},
+					{$test: {coalesce: false}}
+				]});
+				p.run(new DocumentSource({}), function (err, results) {
 					assert.equal(p.sourceVector[1].source, p.sourceVector[0]);
 					assert.equal(p.sourceVector[2].source, p.sourceVector[1]);
 					return next();
@@ -113,25 +134,37 @@ module.exports = {
 			},
 
 			"should iterate through sources and return resultant array": function (next) {
-				var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]});
-				p.run(new DocumentSource({}), function(err, results){
-					assert.deepEqual(results.result, [5,4,3,2,1,0]); //see the test source for why this should be so
+				var p = Pipeline.parseCommand({pipeline: [
+					{$test: {coalesce: false}},
+					{$test: {coalesce: false}},
+					{$test: {coalesce: false}}
+				]});
+				p.run(new DocumentSource({}), function (err, results) {
+					assert.deepEqual(results.result, [5, 4, 3, 2, 1, 0]); //see the test source for why this should be so
 					return next();
 				});
 			},
 
+			"should catch parse errors": function () {
+				// The $foo part is invalid and causes a throw.
+				assert.throws(function () {
+					Pipeline.parseCommand({pipeline: [
+						{$match: {$foo: {bar: "baz"}}}
+					]});
+				});
+			},
+
 			"should call callback with errors from pipeline components": function (next) {
-				var p = Pipeline.parseCommand({pipeline:[{$match:{$foo:{bar:"baz"}}}]});
-				p.run(new DocumentSource({}), function(err, results){
+				var p = Pipeline.parseCommand({pipeline: [
+					{$match: {foo: {bar: "baz"}}}
+				]});
+				p.run(new DocumentSource({}), function (err, results) {
 					assert(err instanceof Error);
 					return next();
 				});
 			}
-
 		}
-
 	}
-
 };
 
 if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);

+ 1 - 1
test/lib/pipeline/expressions/NaryExpression.js

@@ -123,7 +123,7 @@ module.exports = {
 				testableExpr.checkArgCount(3);
 			});
 		},
-		
+
 		//the following test case is eagerly awaiting ObjectExpression
 		"#addDependencies()": function testDependencies(){
 			var testableExpr = new TestableExpression();