Browse Source

EAGLESIX-1581: Add context to the aggregate function.

We were not passing context down from extensions that call aggregate.
Chris Sexton 11 years ago
parent
commit
16ff98b4af
2 changed files with 39 additions and 14 deletions
  1. 6 5
      lib/index.js
  2. 33 9
      test/lib/aggregate.js

+ 6 - 5
lib/index.js

@@ -17,12 +17,13 @@
  * @param   callback.err           {Error}                                    The Error if one occurred
  * @param   callback.docs          {Array}                                    The resulting documents
  **/
-exports = module.exports = function aggregate(pipeline, inputs, callback) {	// function-style interface; i.e., return the utility function directly as the require
-	var ctx = {}, //not used yet
-		pipelineInst = exports.pipeline.Pipeline.parseCommand({
+exports = module.exports = function aggregate(pipeline, ctx, inputs, callback) {	// function-style interface; i.e., return the utility function directly as the require
+	if (ctx instanceof Array) callback = inputs, inputs = ctx, ctx = {};
+	var pipelineInst = exports.pipeline.Pipeline.parseCommand({
 			pipeline: pipeline
 		}, ctx),
-		aggregator = function aggregator(inputs, callback) {
+		aggregator = function aggregator(ctx, inputs, callback) {
+			if (ctx instanceof Array) callback = inputs,  inputs = ctx, ctx = {};
 			if (!callback) callback = exports.SYNC_CALLBACK;
 			if (!inputs) return callback("arg `inputs` is required");
 
@@ -54,7 +55,7 @@ exports = module.exports = function aggregate(pipeline, inputs, callback) {	// f
 			pipelineInst = null; // unset so that subsequent calls can rebuild the pipeline
 			return results;
 		};
-	if(inputs) return aggregator(inputs, callback);
+	if(inputs) return aggregator(ctx, inputs, callback);
 	return aggregator;
 };
 

+ 33 - 9
test/lib/aggregate.js

@@ -10,8 +10,17 @@ function testAggregate(opts){
 	var results = aggregate(opts.pipeline, opts.inputs);
 	assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
 
+	// SYNC: test one-off usage with context
+	results = aggregate(opts.pipeline, {hi: "there"}, opts.inputs);
+	assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
+
+	// SYNC: test use with context
+	var aggregator = aggregate(opts.pipeline, {hi: "there"});
+	results = aggregator(opts.inputs);
+	assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
+
 	// SYNC: test reusable aggregator functionality
-	var aggregator = aggregate(opts.pipeline);
+	aggregator = aggregate(opts.pipeline);
 	results = aggregator(opts.inputs);
 	assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
 
@@ -24,19 +33,34 @@ function testAggregate(opts){
 		assert.ifError(err);
 		assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
 
-		// ASYNC: test reusable aggregator functionality
-		var aggregator = aggregate(opts.pipeline);
-		aggregator(opts.inputs, function(err, results){
+		// ASYNC: test one-off usage with context
+		aggregate(opts.pipeline, {hi: "there"}, opts.inputs, function(err, results){
 			assert.ifError(err);
 			assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
 
-			// ASYNC: test that it is actually reusable
-			aggregator(opts.inputs, function(err, results){
+			// ASYNC: test reusable aggregator functionality with context
+			var aggregator = aggregate(opts.pipeline);
+			aggregator({hi: "there"}, opts.inputs, function(err, results){
 				assert.ifError(err);
-				assert.equal(JSON.stringify(results), JSON.stringify(opts.expected), "Reuse of aggregator should yield the same results!");
+				assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
+
+				// ASYNC: test reusable aggregator functionality
+				var aggregator = aggregate(opts.pipeline);
+				aggregator(opts.inputs, function(err, results){
+					assert.ifError(err);
+					assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
+
+					// ASYNC: test that it is actually reusable
+					aggregator(opts.inputs, function(err, results){
+						assert.ifError(err);
+						assert.equal(JSON.stringify(results), JSON.stringify(opts.expected), "Reuse of aggregator should yield the same results!");
+
+						// success!
+						return opts.next();
+					});
+
+				});
 
-				// success!
-				return opts.next();
 			});
 
 		});