Procházet zdrojové kódy

Fixes #994 Added PushAccumulator and fixed a minor issue with the other accumulator test cases where the testcase output text didn't exactly match what it was doing

http://source.rd.rcg.local/trac/eagle6/changeset/1314/Eagle6_SVN
Adam Bell před 12 roky
rodič
revize
6cf82ea7d3

+ 30 - 0
lib/pipeline/accumulators/PushAccumulator.js

@@ -0,0 +1,30 @@
+var PushAccumulator = module.exports = (function(){
+
+	// Constructor
+	var klass = module.exports = function PushAccumulator(){
+		this.values = [];
+		base.call(this);
+	}, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+
+	proto.evaluate = function evaluate(doc){
+		if(this.operands.length != 1) throw new Error("this should never happen");
+		var v = this.operands[0].evaluate(doc);
+		if(v)
+			this.values.push(v);
+	
+		return null;
+	};
+
+	proto.getValue = function getValue(){
+		return this.values;
+	};
+
+	proto.getOpName = function getOpName(){
+		return "$push";
+	};
+
+	return klass;
+
+})();
+

+ 1 - 1
test/lib/pipeline/accumulators/LastAccumulator.js

@@ -26,7 +26,7 @@ module.exports = {
 
 		"#getOpName()": {
 
-			"should return the correct op name; $mod": function testOpName(){
+			"should return the correct op name; $last": function testOpName(){
 				assert.strictEqual(new LastAccumulator().getOpName(), "$last");
 			}
 

+ 73 - 0
test/lib/pipeline/accumulators/PushAccumulator.js

@@ -0,0 +1,73 @@
+var assert = require("assert"),
+	PushAccumulator = require("../../../../lib/pipeline/accumulators/PushAccumulator"),
+	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
+
+
+function createAccumulator(){
+	var accumulator = new PushAccumulator();
+	accumulator.addOperand(new FieldPathExpression("b") );
+	return accumulator;
+}
+
+
+module.exports = {
+
+	"PushAccumulator": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args": function testConstructor(){
+				assert.doesNotThrow(function(){
+					new PushAccumulator();
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $push": function testOpName(){
+				assert.strictEqual(new PushAccumulator().getOpName(), "$push");
+			}
+
+		},
+
+		"#evaluate()": {
+
+			"should evaluate no documents": function testStuff(){
+				var accumulator = createAccumulator();
+				accumulator.evaluate();
+				assert.deepEqual(accumulator.getValue(), []);
+			},
+
+
+			"should evaluate one document and return an array of 1": function testStuff(){
+				var accumulator = createAccumulator();
+				accumulator.evaluate({b:1});
+				assert.deepEqual(accumulator.getValue(), [1]);
+			},
+
+			"should evaluate two documents and return an array of 2": function testStuff(){
+
+				var accumulator = createAccumulator();
+				accumulator.evaluate({b:1});
+				accumulator.evaluate({b:2});
+				assert.deepEqual(accumulator.getValue(), [1,2]);
+			},
+
+			"should evaluate one document with null and one document with a value and return an array of 1": function testStuff(){
+				var accumulator = createAccumulator();
+				accumulator.evaluate({b:1});
+				accumulator.evaluate({b:null});
+				assert.deepEqual(accumulator.getValue(), [1]);
+			}
+
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
+
+

+ 1 - 1
test/lib/pipeline/accumulators/SumAccumulator.js

@@ -26,7 +26,7 @@ module.exports = {
 
 		"#getOpName()": {
 
-			"should return the correct op name; $mod": function testOpName(){
+			"should return the correct op name; $sum": function testOpName(){
 				assert.strictEqual(new SumAccumulator().getOpName(), "$sum");
 			}