| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 | 
							- "use strict";
 
- var assert = require("assert"),
 
- 	async = require("async"),
 
- 	DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
 
- 	SortDocumentSource = require("../../../../lib/pipeline/documentSources/SortDocumentSource"),
 
- 	LimitDocumentSource = require("../../../../lib/pipeline/documentSources/LimitDocumentSource"),
 
- 	CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
 
- 	Cursor = require("../../../../lib/Cursor"),
 
- 	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
 
- module.exports = {
 
- 	"SortDocumentSource": {
 
- 		"constructor()": {
 
- 			"should not throw Error when constructing without args": function testConstructor(){
 
- 				assert.doesNotThrow(function(){
 
- 					new SortDocumentSource();
 
- 				});
 
- 			}
 
- 		},
 
- 		"#getSourceName()": {
 
- 			"should return the correct source name; $sort": function testSourceName(){
 
- 				var sds = new SortDocumentSource();
 
- 				assert.strictEqual(sds.getSourceName(), "$sort");
 
- 			}
 
- 		},
 
- 		"#getFactory()": {
 
- 			"should return the constructor for this class": function factoryIsConstructor(){
 
- 				assert.strictEqual(new SortDocumentSource().getFactory(), SortDocumentSource);
 
- 			}
 
- 		},
 
- 		"#getNext()": {
 
- 			"should return EOF if there are no more sources": function noSources(next){
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				cwc._cursor = new Cursor( [{a: 1}] );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = SortDocumentSource.createFromJson({a:1});
 
- 				sds.setSource(cds);
 
- 				sds.getNext(function(err, val) {
 
- 					assert.deepEqual(val, {a:1});
 
- 					sds.getNext(function(err, val) {
 
- 						assert.equal(val, DocumentSource.EOF);
 
- 						next();
 
- 					});
 
- 				});
 
- 			},
 
- 			"should return EOF if there are more documents": function hitSort(next){
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				cwc._cursor = new Cursor( [{a: 1}] );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = SortDocumentSource.createFromJson({a:1});
 
- 				sds.setSource(cds);
 
- 				sds.getNext(function(err, doc) {
 
- 					assert.notEqual(doc, DocumentSource.EOF);
 
- 					next();
 
- 				});
 
- 			},
 
- 			"should return the current document source": function currSource(next){
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				cwc._cursor = new Cursor( [{a: 1}] );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = SortDocumentSource.createFromJson({a:1});
 
- 				sds.setSource(cds);
 
- 				sds.getNext(function(err, doc) {
 
- 					assert.deepEqual(doc, { a:1 });
 
- 					next();
 
- 				});
 
- 			},
 
- 			"should return next document when moving to the next source": function nextSource(next){
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				cwc._cursor = new Cursor( [{a: 1}, {b:2}] );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = SortDocumentSource.createFromJson({a:1});
 
- 				sds.setSource(cds);
 
- 				sds.getNext(function(err, doc) {
 
- 					assert.deepEqual(doc, {b:2});
 
- 					next();
 
- 				});
 
- 			},
 
- 			"should return false for no sources remaining": function noMoar(next){
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				cwc._cursor = new Cursor( [{a: 1}, {b:2}] );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = SortDocumentSource.createFromJson({a:1});
 
- 				sds.setSource(cds);
 
- 				sds.getNext(function(err, doc) {
 
- 					sds.getNext(function(err, doc) {
 
- 						assert.deepEqual(doc, {a:1});
 
- 						next();
 
- 					});
 
- 				});
 
- 			}
 
- 		},
 
- 		"#serialize()": {
 
- 			"should throw an error when trying to serialize": function serialize() {
 
- 				var sds = new SortDocumentSource();
 
- 				assert.throws(sds.serialize.bind(sds));
 
- 			}
 
- 		},
 
- 		"#serializeToArray()": {
 
- 			"should create an object representation of the SortDocumentSource": function serializeToArrayTest(){
 
- 				var sds = new SortDocumentSource();
 
- 				sds.vSortKey.push(new FieldPathExpression("b") );
 
- 				var t = [];
 
- 				sds.serializeToArray(t, false);
 
- 				assert.deepEqual(t, [{ "$sort": { "b": -1 } }]);
 
- 			}
 
- 		},
 
- 		"#createFromJson()": {
 
- 			"should return a new SortDocumentSource object from an input JSON object": function createTest(){
 
- 				var sds = SortDocumentSource.createFromJson({a:1});
 
- 				assert.strictEqual(sds.constructor, SortDocumentSource);
 
- 				var t = [];
 
- 				sds.serializeToArray(t, false);
 
- 				assert.deepEqual(t, [{ "$sort": { "a": 1 } }]);
 
- 			},
 
- 			"should return a new SortDocumentSource object from an input JSON object with a descending field": function createTest(){
 
- 				var sds = SortDocumentSource.createFromJson({a:-1});
 
- 				assert.strictEqual(sds.constructor, SortDocumentSource);
 
- 				var t = [];
 
- 				sds.serializeToArray(t, false);
 
- 				assert.deepEqual(t, [{ "$sort": { "a": -1 } }]);
 
- 			},
 
- 			"should return a new SortDocumentSource object from an input JSON object with dotted paths": function createTest(){
 
- 				var sds = SortDocumentSource.createFromJson({ "a.b":1 });
 
- 				assert.strictEqual(sds.constructor, SortDocumentSource);
 
- 				var t = [];
 
- 				sds.serializeToArray(t, false);
 
- 				assert.deepEqual(t, [{ "$sort": { "a.b" : 1  } }]);
 
- 			},
 
- 			"should throw an exception when not passed an object": function createTest(){
 
- 				assert.throws(function() {
 
- 					var sds = SortDocumentSource.createFromJson(7);
 
- 				});
 
- 			},
 
- 			"should throw an exception when passed an empty object": function createTest(){
 
- 				assert.throws(function() {
 
- 					var sds = SortDocumentSource.createFromJson({});
 
- 				});
 
- 			},
 
- 			"should throw an exception when passed an object with a non number value": function createTest(){
 
- 				assert.throws(function() {
 
- 					var sds = SortDocumentSource.createFromJson({a:"b"});
 
- 				});
 
- 			},
 
- 			"should throw an exception when passed an object with a non valid number value": function createTest(){
 
- 				assert.throws(function() {
 
- 					var sds = SortDocumentSource.createFromJson({a:14});
 
- 				});
 
- 			}
 
- 		},
 
- 		"#sort": {
 
- 			"should sort a single document": function singleValue(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				cwc._cursor = new Cursor( [{_id:0, a: 1}] );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("_id", false);
 
- 				sds.setSource(cds);
 
- 				sds.getNext(function(err, actual) {
 
- 					assert.deepEqual(actual, {_id:0, a:1});
 
- 					next();
 
- 				});
 
- 			},
 
- 			"should sort two documents": function twoValue(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: 1}, {_id:1, a:0}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("_id", false);
 
- 				sds.setSource(cds);
 
- 				async.series([
 
- 						sds.getNext.bind(sds),
 
- 						sds.getNext.bind(sds),
 
- 					],
 
- 					function(err,res) {
 
- 						assert.deepEqual([{_id:1, a: 0}, {_id:0, a:1}], res);
 
- 						next();
 
- 					}
 
- 				);
 
- 			},
 
- 			"should sort two documents in ascending order": function ascendingValue(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: 1}, {_id:5, a:12}, {_id:1, a:0}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("_id", true);
 
- 				sds.setSource(cds);
 
- 				var docs = [], i = 0;
 
- 				async.doWhilst(
 
- 					function(cb) {
 
- 						sds.getNext(function(err, val) {
 
- 							docs[i] = val;
 
- 							return cb(err);
 
- 						});
 
- 					},
 
- 					function() {
 
- 						return docs[i++] !== DocumentSource.EOF;
 
- 					},
 
- 					function(err) {
 
- 						assert.deepEqual([{_id:0, a: 1}, {_id:1, a:0}, {_id:5, a:12}, DocumentSource.EOF], docs);
 
- 						next();
 
- 					}
 
- 				);
 
- 			},
 
- 			"should sort documents with a compound key": function compoundKeySort(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: 1, b:3}, {_id:5, a:12, b:7}, {_id:1, a:0, b:2}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("a", false);
 
- 				sds.addKey("b", false);
 
- 				sds.setSource(cds);
 
- 				var docs = [], i = 0;
 
- 				async.doWhilst(
 
- 					function(cb) {
 
- 						sds.getNext(function(err, val) {
 
- 							docs[i] = val;
 
- 							return cb(err);
 
- 						});
 
- 					},
 
- 					function() {
 
- 						return docs[i++] !== DocumentSource.EOF;
 
- 					},
 
- 					function(err) {
 
- 						assert.deepEqual([{_id:5, a:12, b:7}, {_id:0, a:1, b:3}, {_id:1, a:0, b:2}, DocumentSource.EOF], docs);
 
- 						next();
 
- 					}
 
- 				);
 
- 			},
 
- 			"should sort documents with a compound key in ascending order": function compoundAscendingKeySort(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: 1, b:3}, {_id:5, a:12, b:7}, {_id:1, a:0, b:2}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("a", true);
 
- 				sds.addKey("b", true);
 
- 				sds.setSource(cds);
 
- 				var docs = [], i = 0;
 
- 				async.doWhilst(
 
- 					function(cb) {
 
- 						sds.getNext(function(err, val) {
 
- 							docs[i] = val;
 
- 							return cb(err);
 
- 						});
 
- 					},
 
- 					function() {
 
- 						return docs[i++] !== DocumentSource.EOF;
 
- 					},
 
- 					function(err) {
 
- 						assert.deepEqual([{_id:1, a:0, b:2}, {_id:0, a:1, b:3}, {_id:5, a:12, b:7}, DocumentSource.EOF], docs);
 
- 						next();
 
- 					}
 
- 				);
 
- 			},
 
- 			"should sort documents with a compound key in mixed order": function compoundMixedKeySort(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: 1, b:3}, {_id:5, a:12, b:7}, {_id:1, a:0, b:2}, {_id:8, a:7, b:42}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("a", true);
 
- 				sds.addKey("b", false);
 
- 				sds.setSource(cds);
 
- 				var docs = [], i = 0;
 
- 				async.doWhilst(
 
- 					function(cb) {
 
- 						sds.getNext(function(err, val) {
 
- 							docs[i] = val;
 
- 							return cb(err);
 
- 						});
 
- 					},
 
- 					function() {
 
- 						return docs[i++] !== DocumentSource.EOF;
 
- 					},
 
- 					function(err) {
 
- 						assert.deepEqual([{_id:1, a:0, b:2}, {_id:0, a:1, b:3}, {_id:8, a:7, b:42}, {_id:5, a:12, b:7}, DocumentSource.EOF], docs);
 
- 						next();
 
- 					}
 
- 				);
 
- 			},
 
- 			"should not sort different types": function diffTypesSort() {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: 1}, {_id:1, a:"foo"}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("a", false);
 
- 				assert.throws(sds.setSource(cds));
 
- 			},
 
- 			"should sort docs with missing fields": function missingFields(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: 1}, {_id:1}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("a", true);
 
- 				sds.setSource(cds);
 
- 				var docs = [], i = 0;
 
- 				async.doWhilst(
 
- 					function(cb) {
 
- 						sds.getNext(function(err, val) {
 
- 							docs[i] = val;
 
- 							return cb(err);
 
- 						});
 
- 					},
 
- 					function() {
 
- 						return docs[i++] !== DocumentSource.EOF;
 
- 					},
 
- 					function(err) {
 
- 						assert.deepEqual([{_id:1}, {_id:0, a:1}, DocumentSource.EOF], docs);
 
- 						next();
 
- 					}
 
- 				);
 
- 			},
 
- 			"should sort docs with null fields": function nullFields(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: 1}, {_id:1, a: null}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("a", true);
 
- 				sds.setSource(cds);
 
- 				var docs = [], i = 0;
 
- 				async.doWhilst(
 
- 					function(cb) {
 
- 						sds.getNext(function(err, val) {
 
- 							docs[i] = val;
 
- 							return cb(err);
 
- 						});
 
- 					},
 
- 					function() {
 
- 						return docs[i++] !== DocumentSource.EOF;
 
- 					},
 
- 					function(err) {
 
- 						assert.deepEqual([{_id:1, a:null}, {_id:0, a:1}, DocumentSource.EOF], docs);
 
- 						next();
 
- 					}
 
- 				);
 
- 			},
 
- 			"should not support a missing object nested in an array": function missingObjectWithinArray() {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0, a: [1]}, {_id:1, a:[0]}];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				assert.throws(function() {
 
- 					sds.addKey("a.b", false);
 
- 					sds.setSource(cds);
 
- 					var c = [];
 
- 					while (!sds.eof()) {
 
- 						c.push(sds.getCurrent());
 
- 						sds.advance();
 
- 					}
 
- 				});
 
- 			},
 
- 			"should compare nested values from within an array": function extractArrayValues(next) {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0,a:[{b:1},{b:2}]}, {_id:1,a:[{b:1},{b:1}]} ];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc);
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("a.b", true);
 
- 				sds.setSource(cds);
 
- 				var docs = [], i = 0;
 
- 				async.doWhilst(
 
- 					function(cb) {
 
- 						sds.getNext(function(err, val) {
 
- 							docs[i] = val;
 
- 							return cb(err);
 
- 						});
 
- 					},
 
- 					function() {
 
- 						return docs[i++] !== DocumentSource.EOF;
 
- 					},
 
- 					function(err) {
 
- 						assert.deepEqual([{_id:1,a:[{b:1},{b:1}]},{_id:0,a:[{b:1},{b:2}]}, DocumentSource.EOF], docs);
 
- 						next();
 
- 					}
 
- 				);
 
- 			}
 
- 		},
 
- 		"#coalesce()": {
 
- 			"should return false when coalescing a non-limit source": function nonLimitSource() {
 
- 				var cwc = new CursorDocumentSource.CursorWithContext();
 
- 				var l = [{_id:0,a:[{b:1},{b:2}]}, {_id:1,a:[{b:1},{b:1}]} ];
 
- 				cwc._cursor = new Cursor( l );
 
- 				var cds = new CursorDocumentSource(cwc),
 
- 					sds = SortDocumentSource.createFromJson({a:1});
 
- 				var newSrc = sds.coalesce(cds);
 
- 				assert.equal(newSrc, false);
 
- 			},
 
- 			"should return limit source when coalescing a limit source": function limitSource() {
 
- 				var sds = SortDocumentSource.createFromJson({a:1}),
 
- 					lds = LimitDocumentSource.createFromJson(1);
 
- 				var newSrc = sds.coalesce(LimitDocumentSource.createFromJson(10));
 
- 				assert.ok(newSrc instanceof LimitDocumentSource);
 
- 				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": {
 
- 			"should have Dependant field paths": function dependencies() {
 
- 				var sds = new SortDocumentSource();
 
- 				sds.addKey("a", true);
 
- 				sds.addKey("b.c", false);
 
- 				var deps = {};
 
- 				assert.equal("SEE_NEXT", sds.getDependencies(deps));
 
- 				assert.equal(2, Object.keys(deps).length);
 
- 				assert.ok(deps.a);
 
- 				assert.ok(deps["b.c"]);
 
- 			}
 
- 		}
 
- 	}
 
- };
 
- if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);
 
 
  |