Просмотр исходного кода

REFS DEVOPS-265 Added v2.6 compatibility

David Aebersold 12 лет назад
Родитель
Сommit
ae1a10f2b7

+ 54 - 0
lib/pipeline/expressions/SetUnionExpression.js

@@ -0,0 +1,54 @@
+"use strict";
+
+/**
+ * A $setunion pipeline expression.
+ * @see evaluateInternal
+ * @class SetUnionExpression
+ * @namespace mungedb-aggregate.pipeline.expressions
+ * @module mungedb-aggregate
+ * @constructor
+ **/
+var SetUnionExpression = module.exports = function SetUnionExpression() {
+		this.fixedArity(2);
+		if (arguments.length !== 2) throw new Error("two args expected");
+		base.call(this);
+}, klass = SetUnionExpression,
+		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 "$setunion";
+};
+
+/**
+ * Takes 2 objects. Unions the objects
+ * @method evaluateInternal
+ **/
+proto.evaluateInternal = function evaluateInternal(doc) {
+		var object1 = this.operands[0].evaluateInternal(doc),
+				object2 = this.operands[1].evaluateInternal(doc);
+		if (object1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an object");
+		if (object2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an object");
+
+		var object3 = {};
+		for (var attrname1 in object1) {
+				object3[attrname1] = object1[attrname1];
+		}
+		for (var attrname2 in object2) {
+				object3[attrname2] = object2[attrname2];
+		}
+
+		return object3;
+};
+
+/** Register Expression */
+Expression.registerExpression("$setunion", SetUnionExpression.parse);

+ 119 - 0
test/lib/pipeline/expressions/SetUnionExpression.js

@@ -0,0 +1,119 @@
+"use strict";
+var assert = require("assert"),
+		SetUnionExpression = require("../../../../lib/pipeline/expressions/SetUnionExpression"),
+		Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+
+module.exports = {
+
+		"SetUnionExpression": {
+
+				"constructor()": {
+
+						"should throw Error when constructing without args": function testConstructor() {
+								assert.throws(function() {
+										new SetUnionExpression();
+								});
+						}
+
+				},
+
+				"#getOpName()": {
+
+						"should return the correct op name; $setunion": function testOpName() {
+								assert.equal(new SetUnionExpression([1, 2, 3], [4, 5, 6]).getOpName(), "$setunion");
+						}
+
+				},
+
+				"#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({
+												$setunion: ["$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({
+												$setunion: ["$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({
+												$setunion: ["$array1", "$array2"]
+										}).evaluateInternal({
+												array1: array1,
+												array2: array2
+										});
+								});
+						},
+
+						"Should pass and return a unioned set1": function testBasicAssignment() {
+								var array1 = {
+										"a": "3",
+										"c": "4"
+								},
+										array2 = {
+												"a": "3",
+												"b": "3"
+										};
+								assert.strictEqual(Expression.parseOperand({
+										$setunion: ["$array1", "$array2"]
+								}).evaluateInternal({
+										array1: array1,
+										array2: array2
+								}), {
+										"a": "3",
+										"c": "4",
+										"b": "3"
+								});
+						},
+
+						"Should pass and return a unioned set": function testBasicAssignment() {
+								var array1 = [1, 2, 3, 4, 5],
+										array2 = [2, 3, 6, 7, 8];
+								assert.strictEqual(Expression.parseOperand({
+										$setunion: ["$array1", "$array2"]
+								}).evaluateInternal({
+										array1: array1,
+										array2: array2
+								}), [1, 2, 3, 4, 5, 6, 7, 8]);
+						},
+
+						"Should pass and return unioned set": function testBasicAssignment() {
+								var array1 = [1, 2, 3, 4, 5],
+										array2 = [7, 8, 9];
+								assert.strictEqual(Expression.parseOperand({
+										$setunion: ["$array1", "$array2"]
+								}).evaluateInternal({
+										array1: array1,
+										array2: array2
+								}), [1, 2, 3, 4, 5, 7, 8, 9]);
+						},
+
+				}
+
+		}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);