| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 | 
							- "use strict";
 
- if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
 
- var assert = require("assert"),
 
- 	async = require("async"),
 
- 	ArrayRunner = require("../../../../lib/query/ArrayRunner"),
 
- 	pipeline = require("../../../../lib/pipeline/"),
 
- 	CursorDocumentSource = pipeline.documentSources.CursorDocumentSource,
 
- 	GroupDocumentSource = pipeline.documentSources.GroupDocumentSource;
 
- /// An assertion for `ObjectExpression` instances based on Mongo's `ExpectedResultBase` class
 
- function assertExpectedResult(args) {
 
- 	{// check for required args
 
- 		if (args === undefined) throw new TypeError("missing arg: `args` is required");
 
- 		if (args.spec && args.throw === undefined) args.throw = true; // Assume that spec only tests expect an error to be thrown
 
- 		//if (args.spec === undefined) throw new Error("missing arg: `args.spec` is required");
 
- 		if (args.expected !== undefined && args.docs === undefined) throw new Error("must provide docs with expected value");
 
- 	}// check for required args
 
- 	// run implementation
 
- 	if(args.expected && args.docs){
 
- 		var gds = GroupDocumentSource.createFromJson(args.spec),
 
- 			next,
 
- 			results = [],
 
- 			cds = new CursorDocumentSource(null, new ArrayRunner(args.docs), null);
 
- 		gds.setSource(cds);
 
- 		async.whilst(
 
- 			function() {
 
- 				return next !== null;
 
- 			},
 
- 			function(done) {
 
- 				gds.getNext(function(err, doc) {
 
- 					if(err) return done(err);
 
- 					next = doc;
 
- 					if(next === null) {
 
- 						return done();
 
- 					} else {
 
- 						results.push(next);
 
- 						return done();
 
- 					}
 
- 				});
 
- 			},
 
- 			function(err) {
 
- 				assert.equal(JSON.stringify(results), JSON.stringify(args.expected));
 
- 				if(args.done) {
 
- 					return args.done();
 
- 				}
 
- 			}
 
- 		);
 
- 	}else{
 
- 		if(args.throw) {
 
- 			assert.throws(function(){
 
- 				GroupDocumentSource.createFromJson(args.spec);
 
- 			});
 
- 		} else {
 
- 			assert.doesNotThrow(function(){
 
- 				GroupDocumentSource.createFromJson(args.spec);
 
- 			});
 
- 		}
 
- 	}
 
- }
 
- module.exports = {
 
- 	"GroupDocumentSource": {
 
- 		"constructor()": {
 
- 			// $group spec is not an object
 
- 			"should throw Error when constructing without args": function testConstructor(){
 
- 				assertExpectedResult({"throw":true});
 
- 			},
 
- 			// $group spec is not an object
 
- 			"should throw Error when $group spec is not an object": function testConstructor(){
 
- 				assertExpectedResult({spec:"Foo"});
 
- 			},
 
- 			// $group spec is an empty object
 
- 			"should throw Error when $group spec is an empty object": function testConstructor(){
 
- 				assertExpectedResult({spec:{}});
 
- 			},
 
- 			// $group _id is an empty object
 
- 			"should not throw when _id is an empty object": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:{}}, "throw":false});
 
- 			},
 
- 			// $group _id is specified as an invalid object expression
 
- 			"should throw error when _id is an invalid object expression": function testConstructor(){
 
- 				assertExpectedResult({
 
- 					spec:{_id:{$add:1, $and:1}},
 
- 				});
 
- 			},
 
- 			// $group with two _id specs
 
- 			//NOT Implemented can't do this in Javascript
 
- 			// $group _id is the empty string
 
- 			"should not throw when _id is an empty string": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:""}, "throw":false});
 
- 			},
 
- 			// $group _id is a string constant
 
- 			"should not throw when _id is a string constant": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:"abc"}, "throw":false});
 
- 			},
 
- 			// $group with _id set to an invalid field path
 
- 			"should throw when _id is an invalid field path": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:"$a.."}});
 
- 			},
 
- 			// $group _id is a numeric constant
 
- 			"should not throw when _id is a numeric constant": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:2}, "throw":false});
 
- 			},
 
- 			// $group _id is an array constant
 
- 			"should not throw when _id is an array constant": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:[1,2]}, "throw":false});
 
- 			},
 
- 			// $group _id is a regular expression (not supported)
 
- 			"should not throw when _id is a regex": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:/a/}, "throw":false});
 
- 			},
 
- 			// The name of an aggregate field is specified with a $ prefix
 
- 			"should throw when aggregate field spec is specified with $ prefix": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:1, $foo:{$sum:1}}});
 
- 			},
 
- 			// An aggregate field spec that is not an object
 
- 			"should throw when aggregate field spec is not an object": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:1, a:1}});
 
- 			},
 
- 			// An aggregate field spec that is not an object
 
- 			"should throw when aggregate field spec is an empty object": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:1, a:{}}});
 
- 			},
 
- 			// An aggregate field spec with an invalid accumulator operator
 
- 			"should throw when aggregate field spec is an invalid accumulator": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:1, a:{$bad:1}}});
 
- 			},
 
- 			// An aggregate field spec with an array argument
 
- 			"should throw when aggregate field spec with an array as an argument": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:1, a:{$sum:[]}}});
 
- 			},
 
- 			// Multiple accumulator operators for a field
 
- 			"should throw when aggregate field spec with multiple accumulators": function advanceTest(){
 
- 				assertExpectedResult({spec:{_id:1, a:{$sum:1, $push:1}}});
 
- 			}
 
- 		},
 
- 		"#getSourceName()": {
 
- 			"should return the correct source name; $group": function testSourceName(){
 
- 				var gds = new GroupDocumentSource({_id:{}});
 
- 				assert.strictEqual(gds.getSourceName(), "$group");
 
- 			}
 
- 		},
 
- 		"#getNext, #populate": {
 
- 			// Aggregation using duplicate field names is allowed currently
 
- 			// Note: Can't duplicate fields in javascript objects -- skipped
 
- 			// $group _id is computed from an object expression
 
- 			"should compute _id from an object expression": function ObjectExpression(){
 
- 				assertExpectedResult({
 
- 					docs: [{a:6}],
 
- 					spec: {_id:{z:"$a"}},
 
- 					expected: [{_id:{z:6}}]
 
- 				});
 
- 			},
 
- 			// $group _id is a field path expression
 
- 			"should compute _id from a field path expression": function FieldPathExpression(){
 
- 				assertExpectedResult({
 
- 					docs: [{a:5}],
 
- 					spec: {_id:"$a"},
 
- 					expected: [{_id:5}]
 
- 				});
 
- 			},
 
- 			// $group _id is a field path expression
 
- 			"should compute _id from a Date": function testDate(){
 
- 				var d = new Date();
 
- 				assertExpectedResult({
 
- 					docs: [{a:d}],
 
- 					spec: {_id:"$a"},
 
- 					expected: [{_id:d}]
 
- 				});
 
- 			},
 
- 			// Aggregate the value of an object expression
 
- 			"should aggregate the value of an object expression": function AggregateObjectExpression(){
 
- 				assertExpectedResult({
 
- 					docs: [{a:6}],
 
- 					spec: {_id:0, z:{$first:{x:"$a"}}},
 
- 					expected: [{_id:0, z:{x:6}}]
 
- 				});
 
- 			},
 
- 			// Aggregate the value of an operator expression
 
- 			"should aggregate the value of an operator expression": function AggregateOperatorExpression(){
 
- 				assertExpectedResult({
 
- 					docs: [{a:6}],
 
- 					spec: {_id:0, z:{$first:"$a"}},
 
- 					expected: [{_id:0, z:6}]
 
- 				});
 
- 			},
 
- 			//TODO: verify which tests are mongos vs which are *useful* tests that we've added and delete the rest
 
- 			// // Aggregate the value of an operator expression
 
- 			// "should aggregate the value of an operator expression with a null id": function testAdvance_Null(){
 
- 			// 	assertExpectedResult({
 
- 			// 		docs: [{a:6}],
 
- 			// 		spec: {_id:null, z:{$first:"$a"}},
 
- 			// 		expected: [{_id:null, z:6}]
 
- 			// 	});
 
- 			// },
 
- 			// A $group performed on a single document
 
- 			"should make one group with one values": function SingleDocument() {
 
- 				assertExpectedResult({
 
- 					docs: [{a:1}],
 
- 					spec: {_id:0, a:{$sum:"$a"}},
 
- 					expected: [{_id:0, a:1}]
 
- 				});
 
- 			},
 
- 			// A $group performed on two values for a single key
 
- 			"should make one group with two values": function TwoValuesSingleKey() {
 
- 				assertExpectedResult({
 
- 					docs: [{a:1}, {a:2}],
 
- 					spec: {_id:0, a:{$push:"$a"}},
 
- 					expected: [{_id:0, a:[1,2]}]
 
- 				});
 
- 			},
 
- 			// A $group performed on two values with one key each.
 
- 			"should make two groups with one value": function TwoValuesTwoKeys() {
 
- 				assertExpectedResult({
 
- 					docs: [{_id:0,a:1}, {_id:1,a:2}],
 
- 					spec: {_id:"$_id", a:{$push:"$a"}},
 
- 					expected: [{_id:0, a:[1]}, {_id:1, a:[2]}]
 
- 				});
 
- 			},
 
- 			// A $group performed on two values with two keys each.
 
- 			"should make two groups with two values": function FourValuesTwoKeys() {
 
- 				assertExpectedResult({
 
- 					docs: [{_id:0,a:1}, {_id:1,a:2}, {_id:0,a:3}, {_id:1,a:4}],
 
- 					spec: {_id:"$_id", a:{$push:"$a"}},
 
- 					expected: [{_id:0, a:[1, 3]}, {_id:1, a:[2, 4]}]
 
- 				});
 
- 			},
 
- 			// A $group performed on two values with two keys each and two accumulator operations.
 
- 			"should make two groups with two values with two accumulators": function FourValuesTwoKeysTwoAccumulators() {
 
- 				assertExpectedResult({
 
- 					docs: [{_id:0,a:1}, {_id:1,a:2}, {_id:0,a:3}, {_id:1,a:4}],
 
- 					spec: {_id:"$_id", list:{$push:"$a"}, sum:{$sum:{$divide:["$a", 2]}}},
 
- 					expected: [{_id:0, list:[1, 3], sum:2}, {_id:1, list:[2, 4], sum:3}]
 
- 				});
 
- 			},
 
- 			// Null and undefined _id values are grouped together.
 
- 			"should group null and undefined _id's together": function GroupNullUndefinedIds() {
 
- 				assertExpectedResult({
 
- 					docs: [{a:null, b:100}, {b:10}],
 
- 					spec: {_id:"$a", sum:{$sum:"$b"}},
 
- 					expected: [{_id:null, sum:110}]
 
- 				});
 
- 			},
 
- 			// A complex _id expression.
 
- 			"should group based on a complex id": function ComplexId() {
 
- 				assertExpectedResult({
 
- 					docs: [{a:"de", b:"ad", c:"beef", d:""}, {a:"d", b:"eadbe", c:"", d:"ef"}],
 
- 					spec: {_id:{$concat:["$a", "$b", "$c", "$d"]}},
 
- 					expected: [{_id:"deadbeef"}]
 
- 				});
 
- 			},
 
- 			// An undefined accumulator value is dropped.
 
- 			"should ignore undefined values during accumulation":function UndefinedAccumulatorValue() {
 
- 				assertExpectedResult({
 
- 					docs: [{}],
 
- 					spec: {_id:0, first:{$first:"$missing"}},
 
- 					expected: [{_id:0, first:null}]
 
- 				});
 
- 			},
 
- 			"should return errors in the callback": function(done){
 
- 				var gds = GroupDocumentSource.createFromJson({_id:null, sum: {$sum:"$a"}}),
 
- 					cds = new CursorDocumentSource(null, new ArrayRunner([{"a":"foo"}]), null);
 
- 				gds.setSource(cds);
 
- 				gds.getNext(function(err, doc) {
 
- 					assert(err, "Expected Error");
 
- 					done();
 
- 				});
 
- 			}
 
- 		}
 
- 	}
 
- };
 
 
  |