Pipeline.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";
  2. var assert = require("assert"),
  3. Pipeline = require("../../../lib/pipeline/Pipeline"),
  4. FieldPath = require("../../../lib/pipeline/FieldPath"),
  5. DocumentSource = require('../../../lib/pipeline/documentSources/DocumentSource');
  6. module.exports = {
  7. "Pipeline": {
  8. before: function(){
  9. Pipeline.stageDesc.$test = (function(){
  10. var klass = function TestDocumentSource(options, ctx){
  11. base.call(this, ctx);
  12. this.shouldCoalesce = options.coalesce;
  13. this.coalesceWasCalled = false;
  14. this.optimizeWasCalled = false;
  15. this.current = 5;
  16. }, TestDocumentSource = klass, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  17. proto.coalesce = function(){
  18. this.coalesceWasCalled = true;
  19. var c = this.shouldCoalesce;//only coalesce with the first thing we find
  20. this.shouldCoalesce = false;
  21. return c;
  22. };
  23. proto.optimize = function(){
  24. this.optimizeWasCalled = true;
  25. };
  26. proto.getNext = function(callback){
  27. var answer = this.current > 0 ? {val:this.current--} : null;
  28. if(callback) {
  29. return callback(null, answer);
  30. } else {
  31. return answer;
  32. }
  33. };
  34. klass.createFromJson = function(options, ctx){
  35. return new TestDocumentSource(options, ctx);
  36. };
  37. return klass;
  38. })().createFromJson;
  39. },
  40. "parseCommand": {
  41. "should throw Error if given non-objects in the array": function () {
  42. assert.throws(function(){
  43. Pipeline.parseCommand({pipeline:[5]});
  44. });
  45. },
  46. "should throw Error if given objects with more / less than one field": function () {
  47. assert.throws(function(){
  48. Pipeline.parseCommand({pipeline:[{}]});
  49. Pipeline.parseCommand({pipeline:[{a:1,b:2}]});
  50. });
  51. },
  52. "should throw Error on unknown document sources": function () {
  53. assert.throws(function(){
  54. Pipeline.parseCommand({pipeline:[{$foo:"$sdfdf"}]});
  55. });
  56. },
  57. "should swap $match and $sort if the $match immediately follows the $sort": function () {
  58. var p = Pipeline.parseCommand({pipeline:[{$sort:{"xyz":1}}, {$match:{}}]});
  59. assert.equal(p.sources[0].constructor.matchName, "$match");
  60. assert.equal(p.sources[1].constructor.sortName, "$sort");
  61. },
  62. "should attempt to coalesce all sources": function () {
  63. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:true}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]});
  64. assert.equal(p.sources.length, 3);
  65. p.sources.slice(0,-1).forEach(function(source){
  66. assert.equal(source.coalesceWasCalled, true);
  67. });
  68. assert.equal(p.sources[p.sources.length -1].coalesceWasCalled, false);
  69. },
  70. "should optimize all sources": function () {
  71. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}]});
  72. p.sources.forEach(function(source){
  73. assert.equal(source.optimizeWasCalled, true);
  74. });
  75. }
  76. },
  77. "#stitch": {
  78. "should set the parent source for all sources in the pipeline except the first one": function () {
  79. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]});
  80. p.stitch();
  81. assert.equal(p.sources[1].source, p.sources[0]);
  82. }
  83. },
  84. "#_runSync": {
  85. "should iterate through sources and return resultant array": function () {
  86. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]}),
  87. results = p.run();
  88. assert.deepEqual(results.result, [ { val: 5 }, { val: 4 }, { val: 3 }, { val: 2 }, { val: 1 } ]);
  89. },
  90. "should call callback with errors from pipeline components": function (next) {
  91. var p = Pipeline.parseCommand({pipeline:[{$limit:1},{$project:{a:new Date()}}, {$project:{a:{$add:["$a", 1]}}}]}); //Can't add date
  92. p.run(function(err, results){
  93. assert(err instanceof Error);
  94. return next();
  95. });
  96. }
  97. },
  98. "#_runAsync": {
  99. "should iterate through sources and return resultant array asynchronously": function () {
  100. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]}),
  101. results = p.run(function(err, results) {
  102. assert.deepEqual(results.result, [ { val: 5 }, { val: 4 }, { val: 3 }, { val: 2 }, { val: 1 } ]);
  103. });
  104. }
  105. },
  106. "#addInitialSource": {
  107. "should put the given source at the beginning of the pipeline": function () {
  108. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]}),
  109. initialSource = Pipeline.stageDesc.$test({coalesce:false});
  110. p.addInitialSource(initialSource);
  111. assert.equal(initialSource, p.sources[0]);
  112. },
  113. "should be able to addInitialSource then stitch": function () {
  114. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]}),
  115. initialSource = Pipeline.stageDesc.$test({coalesce:false});
  116. p.addInitialSource(initialSource);
  117. p.stitch();
  118. assert.equal(p.sources[1].source, p.sources[0]);
  119. }
  120. }
  121. }
  122. };
  123. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);