Преглед изворни кода

Refs #5127: Fix async issues, migrate test cases

Chris Sexton пре 12 година
родитељ
комит
9750e40fe6

+ 19 - 14
lib/pipeline/documentSources/ProjectDocumentSource.js

@@ -33,25 +33,30 @@ proto.getSourceName = function getSourceName() {
 };
 };
 
 
 proto.getNext = function getNext(callback) {
 proto.getNext = function getNext(callback) {
-	var input = this.source.getNext();
+	if (!callback) throw new Error(this.getSourceName() + ' #getNext() requires callback');
 
 
-	if (input === DocumentSource.EOF)
-		return callback(null, DocumentSource.EOF);
+	var self = this;
 
 
-	/* create the result document */
-	var out = {};
+	this.source.getNext(function(err, input) {
+		if (err)
+		return callback(null, err);
+
+		if (input === DocumentSource.EOF)
+			return callback(null, DocumentSource.EOF);
 
 
-	/*
-	* Use the ExpressionObject to create the base result.
-	*
-	* If we're excluding fields at the top level, leave out the _id if
-	* it is found, because we took care of it above.
-	*/
-	this.OE.addToDocument(out, input); // TODO: 'Variables' stuffs
+		/* create the result document */
+		var out = {};
 
 
-	if (!callback) return new Error('ProjectDocumentSource #getNext() requires callback');
+		/*
+		* Use the ExpressionObject to create the base result.
+		*
+		* If we're excluding fields at the top level, leave out the _id if
+		* it is found, because we took care of it above.
+		*/
+		self.OE.addToDocument(out, input); // TODO: 'Variables' stuffs
 
 
-	return callback(null, out);
+		return callback(null, out);
+	});
 };
 };
 
 
 /**
 /**

+ 75 - 91
test/lib/pipeline/documentSources/ProjectDocumentSource.js

@@ -1,17 +1,12 @@
 "use strict";
 "use strict";
 var assert = require("assert"),
 var assert = require("assert"),
+	async = require("async"),
 	DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
 	DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
 	ProjectDocumentSource = require("../../../../lib/pipeline/documentSources/ProjectDocumentSource"),
 	ProjectDocumentSource = require("../../../../lib/pipeline/documentSources/ProjectDocumentSource"),
 	CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
 	CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
 	Cursor = require("../../../../lib/Cursor");
 	Cursor = require("../../../../lib/Cursor");
 
 
 
 
-//HELPERS
-var assertExhausted = function assertExhausted(pds) {
-	assert.ok(pds.eof());
-	assert.ok(!pds.advance());
-};
-
 /**
 /**
  *   Tests if the given rep is the same as what the pds resolves to as JSON.
  *   Tests if the given rep is the same as what the pds resolves to as JSON.
  *   MUST CALL WITH A PDS AS THIS (e.g. checkJsonRepresentation.call(this, rep) where this is a PDS)
  *   MUST CALL WITH A PDS AS THIS (e.g. checkJsonRepresentation.call(this, rep) where this is a PDS)
@@ -63,85 +58,66 @@ module.exports = {
 
 
 		},
 		},
 
 
-		"#returnNext()": {
+		"#getNext()": {
 
 
-			"should return EOF": function testEOF() {
+			"should return EOF": function testEOF(next) {
 				var pds = createProject();
 				var pds = createProject();
 				pds.setSource({
 				pds.setSource({
-					getNext: function getNext() {
-						return DocumentSource.EOF;
+					getNext: function getNext(cb) {
+						return cb(null, DocumentSource.EOF);
 					}
 					}
 				});
 				});
 				pds.getNext(function(err, doc) {
 				pds.getNext(function(err, doc) {
 					assert.equal(DocumentSource.EOF, doc);
 					assert.equal(DocumentSource.EOF, doc);
+					next();
 				});
 				});
 			},
 			},
 
 
 			"callback is required": function requireCallback() {
 			"callback is required": function requireCallback() {
 				var pds = createProject();
 				var pds = createProject();
-				pds.setSource({
-					getNext: function getNext() {
-						return false;
-					}
+				assert.throws(pds.getNext.bind(pds));
+			},
+
+			"should not return EOF when a document is still in cursor": function testNotEOFTrueIfDocPresent() {
+				var cwc = new CursorDocumentSource.CursorWithContext();
+				cwc._cursor = new Cursor( [{a: 1}] );
+				var cds = new CursorDocumentSource(cwc);
+				var pds = new ProjectDocumentSource();
+				pds.setSource(cds);
+				pds.getNext(function(err,actual) {
+					assert.notEqual(actual, DocumentSource.EOF);
 				});
 				});
-				assert.deepEqual(pds.getNext(/*no callback */), new Error('ProjectDocumentSource #getNext() requires callback'));
-			}
+			},
 
 
-			// "should return !eof == true when a document is still in cursor": function testNotEOFTrueIfDocPresent() {
-			//     var cwc = new CursorDocumentSource.CursorWithContext();
-			//     cwc._cursor = new Cursor( [{a: 1}] );
-			//     var cds = new CursorDocumentSource(cwc);
-			//     var pds = new ProjectDocumentSource();
-			//     pds.setSource(cds);
-			//     assert.ok(!pds.eof());
-			// }
+			"can retrieve second document from source": function testAdvanceFirst() {
+				var cwc = new CursorDocumentSource.CursorWithContext();
+				var input = [{_id: 0, a: 1}, {_id: 1, a: 2}];
+				cwc._cursor = new Cursor( input );
+				var cds = new CursorDocumentSource(cwc);
+				var pds = createProject();
+				pds.setSource(cds);
 
 
-		},
+				pds.getNext(function(err,val) {
+					// eh, ignored
+					pds.getNext(function(err,val) {
+						assert.equal(2, val.a);
+					});
+				});
+			},
+
+			"should get the first document out of a cursor": function getCurrentCalledFirst() {
+				var cwc = new CursorDocumentSource.CursorWithContext();
+				var input = [{_id: 0, a: 1}];
+				cwc._cursor = new Cursor( input );
+				var cds = new CursorDocumentSource(cwc);
+				var pds = createProject();
+				pds.setSource(cds);
+				pds.getNext(function(err, actual) {
+					assert.equal(1, actual.a);
+				});
+			},
 
 
-		// "#advance()": {
-
-		//     "should return same as this.psource.advance": function testCallsThisPSourceAdvance() {
-		//         var pds = new ProjectDocumentSource();
-		//         pds.setSource({
-		//             advance: function advance() {
-		//                 return true;
-		//             }
-		//         });
-		//         assert.ok(pds.advance());
-		//     },
-
-		//     "can retrieve values from the project when advance is first function call": function testAdvanceFirst() {
-		//         var cwc = new CursorDocumentSource.CursorWithContext();
-		//         var input = [{_id: 0, a: 1}, {_id: 1, a: 2}];
-		//         cwc._cursor = new Cursor( input );
-		//         var cds = new CursorDocumentSource(cwc);
-		//         var pds = createProject();
-		//         pds.setSource(cds);
-		//         assert.ok(pds.advance()); 
-		//         assert.equal(2, pds.getCurrent().a);
-		//     }
-
-		// },
-
-		/* TODO : copy mongo tests, they will probably be more involved */
-		// "#getCurrent()": {
-
-		//     "should work when getCurrent is the first function call": function getCurrentCalledFirst() {
-		//         var cwc = new CursorDocumentSource.CursorWithContext();
-		//         var input = [{_id: 0, a: 1}];
-		//         cwc._cursor = new Cursor( input );
-		//         var cds = new CursorDocumentSource(cwc);
-		//         var pds = createProject();
-		//         pds.setSource(cds);
-		//         assert.ok(pds.getCurrent()); 
-		//         assert.equal(1, pds.getCurrent().a);                
-		//     }
-
-		// },
-
-		"combined": {
-
-			"The a and c.d fields are included but the b field is not": function testFullProject1() {
+			"The a and c.d fields are included but the b field is not": function testFullProject1(next) {
 				var cwc = new CursorDocumentSource.CursorWithContext();
 				var cwc = new CursorDocumentSource.CursorWithContext();
 				var input = [{
 				var input = [{
 					_id: 0,
 					_id: 0,
@@ -154,20 +130,21 @@ module.exports = {
 				cwc._cursor = new Cursor(input);
 				cwc._cursor = new Cursor(input);
 				var cds = new CursorDocumentSource(cwc);
 				var cds = new CursorDocumentSource(cwc);
 				var pds = createProject({
 				var pds = createProject({
-					a: true,
-					c: {
-						d: true
-					}
-				});
+						a: true,
+						c: {
+							d: true
+						}
+					}),
+					expected = {a:1, c:{ d: 1 }};
 				pds.setSource(cds);
 				pds.setSource(cds);
-				assert.ok(pds.getNext());
-				assert.equal(1, pds.getCurrent().a);
-				assert.ok(!pds.getCurrent().b);
-				assert.equal(0, pds.getCurrent()._id);
-				assert.equal(1, pds.getCurrent().c.d);
+
+				pds.getNext(function(err,val) {
+					assert.deepEqual(expected, val);
+					next();
+				});
 			},
 			},
 
 
-			"Two documents": function testTwoDocumentsProject() {
+			"Two documents": function testTwoDocumentsProject(next) {
 				var cwc = new CursorDocumentSource.CursorWithContext();
 				var cwc = new CursorDocumentSource.CursorWithContext();
 				var input = [{
 				var input = [{
 					a: 1,
 					a: 1,
@@ -175,7 +152,12 @@ module.exports = {
 				}, {
 				}, {
 					a: 3,
 					a: 3,
 					b: 4
 					b: 4
-				}];
+				}],
+				expected = [
+					{a:1},
+					{a:3},
+					DocumentSource.EOF
+				];
 				cwc._cursor = new Cursor(input);
 				cwc._cursor = new Cursor(input);
 				var cds = new CursorDocumentSource(cwc);
 				var cds = new CursorDocumentSource(cwc);
 				var pds = createProject({
 				var pds = createProject({
@@ -185,15 +167,17 @@ module.exports = {
 					}
 					}
 				});
 				});
 				pds.setSource(cds);
 				pds.setSource(cds);
-				assert.ok(!pds.eof());
-				assert.equal(1, pds.getCurrent().a);
-				assert.ok(!pds.getCurrent().b);
-				assert.ok(pds.advance());
-				assert.ok(!pds.eof());
-				assert.equal(3, pds.getCurrent().a);
-				assert.ok(!pds.getCurrent().b);
-				assert.ok(!pds.advance());
-				assertExhausted(pds);
+
+				async.series([
+						pds.getNext.bind(pds),
+						pds.getNext.bind(pds),
+						pds.getNext.bind(pds),
+					],
+					function(err,res) {
+						assert.deepEqual(expected, res);
+						next();
+					}
+				);
 			}
 			}
 		},
 		},
 
 
@@ -283,4 +267,4 @@ module.exports = {
 
 
 };
 };
 
 
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);