Pipeline.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var assert = require("assert"),
  2. Pipeline = require("../../../lib/pipeline/Pipeline");
  3. module.exports = {
  4. "Pipeline": {
  5. before: function(){
  6. Pipeline.StageDesc.$test = (function(){
  7. var klass = function TestDocumentSource(options){
  8. base.call(this);
  9. this.shouldCoalesce = options.coalesce;
  10. this.coalesceWasCalled = false;
  11. this.optimizeWasCalled = false;
  12. this.current = 5;
  13. }, base = require('../../../lib/pipeline/documentSources/DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. proto.coalesce = function(){
  15. this.coalesceWasCalled = true;
  16. var c = this.shouldCoalesce;//only coalesce with the first thing we find
  17. this.shouldCoalesce = false;
  18. return c;
  19. };
  20. proto.optimize = function(){
  21. this.optimizeWasCalled = true;
  22. };
  23. proto.eof = function(){
  24. return this.current < 0;
  25. };
  26. proto.advance = function(){
  27. this.current = this.current - 1;
  28. return !this.eof();
  29. };
  30. proto.getCurrent = function(){
  31. return this.current;
  32. };
  33. return klass;
  34. })();
  35. //TODO:remove this once Sort is implemented!!!
  36. Pipeline.SortDocumentSource = (function(){
  37. var klass = function SortDocumentSource(){
  38. }, base = require('../../../lib/pipeline/documentSources/DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  39. klass.sortName = "$sort";
  40. return klass;
  41. })();
  42. Pipeline.StageDesc.$sort = Pipeline.SortDocumentSource;
  43. },
  44. "parseCommand": {
  45. "should fail if given non-objects in the array":function(){
  46. assert.throws(function(){
  47. Pipeline.parseCommand([5]);
  48. });
  49. },
  50. "should fail if given objects with more/less than one field":function(){
  51. assert.throws(function(){
  52. Pipeline.parseCommand([{}]);
  53. });
  54. },
  55. "should fail if given objects that dont match any known document sources":function(){
  56. assert.throws(function(){
  57. Pipeline.parseCommand([{$foo:"$sdfdf"}]);
  58. });
  59. },
  60. "should swap $match and $sort if the $match immediately follows the $sort":function(){
  61. var p = Pipeline.parseCommand([{$sort:{"xyz":1}}, {$match:{}}]);
  62. assert.equal(p.sourceVector[0].constructor.matchName, "$match");
  63. assert.equal(p.sourceVector[1].constructor.sortName, "$sort");
  64. },
  65. "should attempt to coalesce all sources":function(){
  66. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:true}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]);
  67. assert.equal(p.sourceVector.length, 3);
  68. p.sourceVector.slice(0,-1).forEach(function(source){
  69. assert.equal(source.coalesceWasCalled, true);
  70. });
  71. assert.equal(p.sourceVector[p.sourceVector.length -1].coalesceWasCalled, false);
  72. },
  73. "should optimize all sources":function(){
  74. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}]);
  75. p.sourceVector.forEach(function(source){
  76. assert.equal(source.optimizeWasCalled, true);
  77. });
  78. }
  79. },
  80. "#run": {
  81. "should set the parent source for all sources in the pipeline except the first one":function(){
  82. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]);
  83. p.run({}, []);
  84. assert.equal(p.sourceVector[1].pSource, p.sourceVector[0]);
  85. assert.equal(p.sourceVector[2].pSource, p.sourceVector[1]);
  86. },
  87. "should iterate through sources and return resultant array":function(){
  88. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]),
  89. result = {};
  90. p.run(result, []);
  91. assert.deepEqual(result.result, [5,4,3,2,1,0]);//see the test source for why this should be so
  92. }
  93. }
  94. }
  95. };
  96. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();