Ver Fonte

Refs #5300: Add missing test cases to assorted documentsources

Chris Sexton há 12 anos atrás
pai
commit
66c998ea2b

+ 1 - 1
lib/pipeline/documentSources/DocumentSource.js

@@ -232,7 +232,7 @@ proto._serialize = function _serialize(explain) {
 
 proto.serializeToArray = function serializeToArray(array, explain) {
 	var entry = this.serialize(explain);
-	if (!entry) {
+	if (entry) {
 		array.push(entry);
 	}
 };

+ 15 - 0
test/lib/aggregate.js

@@ -151,6 +151,21 @@ module.exports = {
 			});
 		},
 
+		"should be able to project out a whole document and leave an empty": function(next) {
+			testAggregate({
+				inputs: [{_id:0, a:1}, {_id:1, a:2, b:1}, {_id:2, b:2, c:1}],
+				pipeline: [
+					{$project:{
+						_id:0,
+						a:1
+						//TODO: high level test of all other expression operators
+					}}
+				],
+				expected: [{a:1}, {a:2}, {}],
+				next: next
+			});
+		},
+
 		"should be able to construct an instance with sort operators properly (ascending)": function(next){
 			testAggregate({
 				inputs: [

+ 45 - 6
test/lib/pipeline/documentSources/CursorDocumentSource.js

@@ -7,6 +7,14 @@ var assert = require("assert"),
 	SkipDocumentSource = require("../../../../lib/pipeline/documentSources/SkipDocumentSource"),
 	Cursor = require("../../../../lib/Cursor");
 
+var getCursor = function(values) {
+	if (!values)
+		values = [1,2,3,4,5];
+	var cwc = new CursorDocumentSource.CursorWithContext();
+	cwc._cursor = new Cursor( values );
+	return new CursorDocumentSource(cwc);
+};
+
 
 module.exports = {
 
@@ -30,15 +38,46 @@ module.exports = {
 
 		"#coalesce": {
 			"should be able to coalesce a limit into itself": function (){
-				var cwc = new CursorDocumentSource.CursorWithContext();
-				cwc._cursor = new Cursor( [] );
-
-				var lds = new LimitDocumentSource();
-				lds.limit = 1;
+				var cds = getCursor(),
+					lds = LimitDocumentSource.createFromJson(2);
 
-				var cds = new CursorDocumentSource(cwc);
 				assert.equal(cds.coalesce(lds) instanceof LimitDocumentSource, true);
+				assert.equal(cds.getLimit(), 2);
+			},
+
+			"should keep original limit if coalesced to a larger limit": function() {
+				var cds = getCursor();
+				cds.coalesce(LimitDocumentSource.createFromJson(2));
+				cds.coalesce(LimitDocumentSource.createFromJson(3));
+				assert.equal(cds.getLimit(), 2);
+			},
+
+
+			"cursor only returns $limit number when coalesced": function(next) {
+				var cds = getCursor(),
+					lds = LimitDocumentSource.createFromJson(2);
+
+
+				cds.coalesce(lds);
+
+				var docs = [], i = 0;
+				async.doWhilst(
+					function(cb) {
+						cds.getNext(function(err, val) {
+							docs[i] = val;
+							return cb(err);
+						});
+					},
+					function() {
+						return docs[i++] !== DocumentSource.EOF;
+					},
+					function(err) {
+						assert.deepEqual([1, 2, DocumentSource.EOF], docs);
+						next();
+					}
+				);
 			},
+
 			"should leave non-limit alone": function () {
 				var cwc = new CursorDocumentSource.CursorWithContext();
 				cwc._cursor = new Cursor( [] );

+ 10 - 0
test/lib/pipeline/documentSources/LimitDocumentSource.js

@@ -18,6 +18,16 @@ module.exports = {
 
 		},
 
+		"#getDependencies": {
+			"limits do not create dependencies": function() {
+				var lds = LimitDocumentSource.createFromJson(1),
+					deps = {};
+
+				assert.equal(DocumentSource.GetDepsReturn.SEE_NEXT, lds.getDependencies(deps));
+				assert.equal(0, Object.keys(deps).length);
+			}
+		},
+
 		"#getSourceName()": {
 
 			"should return the correct source name; $limit": function testSourceName(){

+ 37 - 0
test/lib/pipeline/documentSources/MatchDocumentSource.js

@@ -161,6 +161,43 @@ module.exports = {
 				var actual = mds.coalesce(mds2);
 				assert.equal(actual, true);
 				assert.deepEqual(mds.getQuery(), expected);
+			},
+
+			"should merge two MatchDocumentSources together": function() {
+				var match1 = MatchDocumentSource.createFromJson({ a: 1 }),
+					match2 = MatchDocumentSource.createFromJson({ b: 1 }),
+					match3 = MatchDocumentSource.createFromJson({ c: 1 });
+
+				// check initial state
+				assert.deepEqual(match1.getQuery(), {a:1});
+				assert.deepEqual(match2.getQuery(), {b:1});
+				assert.deepEqual(match3.getQuery(), {c:1});
+
+				assert.doesNotThrow(function() {
+					match1.coalesce(match2);
+				});
+				assert.deepEqual(match1.getQuery(), {$and: [{a:1}, {b:1}]});
+			},
+
+			"should merge three MatchDocumentSources together": function() {
+				var match1 = MatchDocumentSource.createFromJson({ a: 1 }),
+					match2 = MatchDocumentSource.createFromJson({ b: 1 }),
+					match3 = MatchDocumentSource.createFromJson({ c: 1 });
+
+				// check initial state
+				assert.deepEqual(match1.getQuery(), {a:1});
+				assert.deepEqual(match2.getQuery(), {b:1});
+				assert.deepEqual(match3.getQuery(), {c:1});
+
+				assert.doesNotThrow(function() {
+					match1.coalesce(match2);
+				});
+				assert.deepEqual(match1.getQuery(), {$and: [{a:1}, {b:1}]});
+
+				assert.doesNotThrow(function() {
+					match1.coalesce(match3);
+				});
+				assert.deepEqual(match1.getQuery(), {$and: [{$and: [{a:1}, {b:1}]}, {c:1}]});
 			}
 
 		},

+ 11 - 4
test/lib/pipeline/documentSources/SortDocumentSource.js

@@ -453,11 +453,18 @@ module.exports = {
 				var sds = SortDocumentSource.createFromJson({a:1}),
 					lds = LimitDocumentSource.createFromJson(1);
 
-				var newSrc = sds.coalesce(lds);
+				var newSrc = sds.coalesce(LimitDocumentSource.createFromJson(10));
 				assert.ok(newSrc instanceof LimitDocumentSource);
-				assert.equal(newSrc.limit, 1);
-				assert.equal(sds.getLimit(), 1);
-			}
+				assert.equal(sds.getLimit(), 10);
+				assert.equal(newSrc.limit, 10);
+
+				sds.coalesce(LimitDocumentSource.createFromJson(5));
+				assert.equal(sds.getLimit(), 5);
+
+				var arr = [];
+				sds.serializeToArray(arr);
+				assert.deepEqual(arr, [{$sort: {a:1}}, {$limit: 5}]);
+			},
 		},
 
 		"#dependencies": {