Explorar o código

REFS DEVOPS-261 Created new class for diff expression. v2.6 compatibility

David Aebersold %!s(int64=11) %!d(string=hai) anos
pai
achega
40838c2f17

+ 53 - 0
lib/pipeline/expressions/SetDifferenceExpression.js

@@ -0,0 +1,53 @@
+"use strict";
+
+/**
+ * A $setdifference pipeline expression.
+ * @see evaluateInternal
+ * @class SubstrExpression
+ * @namespace mungedb-aggregate.pipeline.expressions
+ * @module mungedb-aggregate
+ * @constructor
+ **/
+var SetDifferenceExpression = module.exports = function SetDifferenceExpression() {
+		this.fixedAirty(2);
+		if (arguments.length !== 2) throw new Error("two args expected");
+		base.call(this);
+}, klass = SetDifferenceExpression,
+		base = require("./NaryExpression"),
+		proto = klass.prototype = Object.create(base.prototype, {
+				constructor: {
+						value: klass
+				}
+		});
+
+// DEPENDENCIES
+var Value = require("../Value"),
+		Expression = require("./Expression");
+
+// PROTOTYPE MEMBERS
+proto.getOpName = function getOpName() {
+		return "$setdifference";
+};
+
+/**
+ * Takes 2 arrays. Assigns the second array to the first array.
+ * @method evaluateInternal
+ **/
+proto.evaluateInternal = function evaluateInternal(doc) {
+		var array1 = this.operands[0].evaluateInternal(doc),
+				array2 = this.operands[1].evaluateInternal(doc);
+		if (array1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an array");
+		if (array2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an array");
+
+		var returnVec = [];
+
+		array1.forEach(function(key) {
+				if (-1 === array2.indexOf(key)) {
+						returnVec.push(key);
+				}
+		}, this);
+		return returnVec;
+};
+
+/** Register Expression */
+Expression.registerExpression("$setdifference", SetDifferenceExpression.parse);

+ 87 - 0
test/lib/pipeline/expressions/SetDifferenceExpression.js

@@ -0,0 +1,87 @@
+"use strict";
+var assert = require("assert"),
+		SetDifferenceExpression = require("../../../../lib/pipeline/expressions/SetDifferenceExpression"),
+		Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+
+module.exports = {
+
+		"SetDifferenceExpression": {
+
+				"constructor()": {
+
+						"should not throw Error when constructing without args": function testConstructor() {
+								assert.doesNotThrow(function() {
+										new SetDifferenceExpression();
+								});
+						}
+
+				},
+
+				"#getOpName()": {
+
+						"should return the correct op name; $setdifference": function testOpName() {
+								assert.equal(new SetDifferenceExpression().getOpName(), "$setdifference");
+						}
+
+				},
+
+				"#evaluateInternal()": {
+
+						"Should fail if array1 is not an array": function testArg1() {
+								var array1 = "not an array",
+										array2 = [6, 7, 8, 9];
+								assert.throws(function() {
+										Expression.parseOperand({
+												$setdifference: ["$array1", "$array2"]
+										}).evaluateInternal({
+												array1: array1,
+												array2: array2
+										});
+								});
+						},
+
+						"Should fail if array2 is not an array": function testArg2() {
+								var array1 = [1, 2, 3, 4],
+										array2 = "not an array";
+								assert.throws(function() {
+										Expression.parseOperand({
+												$setdifference: ["$array1", "$array2"]
+										}).evaluateInternal({
+												array1: array1,
+												array2: array2
+										});
+								});
+						},
+
+						"Should fail if both are not an array": function testArg1andArg2() {
+								var array1 = "not an array",
+										array2 = "not an array";
+								assert.throws(function() {
+										Expression.parseOperand({
+												$setdifference: ["$array1", "$array2"]
+										}).evaluateInternal({
+												array1: array1,
+												array2: array2
+										});
+								});
+						},
+
+						"Should pass and return a difference between the arrays": function testBasicAssignment() {
+								var array1 = [1, 9, 2, 3, 4, 5],
+										array2 = [5, 6, 7, 2, 8, 9];
+								assert.strictEqual(Expression.parseOperand({
+										$setdifference: ["$array1", "$array2"]
+								}).evaluateInternal({
+										array1: array1,
+										array2: array2
+								}), [1, 3, 4, 6, 7 , 8]);
+						},
+
+				}
+
+		}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);