Pipeline.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }, TestDocumentSource = klass, 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. klass.createFromJson = function(options){
  34. return new TestDocumentSource(options);
  35. };
  36. return klass;
  37. })().createFromJson;
  38. //TODO:remove this once Sort is implemented!!!
  39. Pipeline.SortDocumentSource = (function(){
  40. var klass = function SortDocumentSource(){
  41. }, SortDocumentSource = klass, base = require('../../../lib/pipeline/documentSources/DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  42. klass.sortName = "$sort";
  43. klass.createFromJson = function(options){
  44. return new SortDocumentSource(options);
  45. };
  46. return klass;
  47. })();
  48. Pipeline.StageDesc.$sort = Pipeline.SortDocumentSource.createFromJson;
  49. },
  50. "parseCommand": {
  51. "should fail if given non-objects in the array":function(){
  52. assert.throws(function(){
  53. Pipeline.parseCommand([5]);
  54. });
  55. },
  56. "should fail if given objects with more/less than one field":function(){
  57. assert.throws(function(){
  58. Pipeline.parseCommand([{}]);
  59. });
  60. },
  61. "should fail if given objects that dont match any known document sources":function(){
  62. assert.throws(function(){
  63. Pipeline.parseCommand([{$foo:"$sdfdf"}]);
  64. });
  65. },
  66. "should swap $match and $sort if the $match immediately follows the $sort":function(){
  67. var p = Pipeline.parseCommand([{$sort:{"xyz":1}}, {$match:{}}]);
  68. assert.equal(p.sourceVector[0].constructor.matchName, "$match");
  69. assert.equal(p.sourceVector[1].constructor.sortName, "$sort");
  70. },
  71. "should attempt to coalesce all sources":function(){
  72. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:true}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]);
  73. assert.equal(p.sourceVector.length, 3);
  74. p.sourceVector.slice(0,-1).forEach(function(source){
  75. assert.equal(source.coalesceWasCalled, true);
  76. });
  77. assert.equal(p.sourceVector[p.sourceVector.length -1].coalesceWasCalled, false);
  78. },
  79. "should optimize all sources":function(){
  80. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}]);
  81. p.sourceVector.forEach(function(source){
  82. assert.equal(source.optimizeWasCalled, true);
  83. });
  84. }
  85. },
  86. "#run": {
  87. "should set the parent source for all sources in the pipeline except the first one":function(){
  88. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]);
  89. p.run({}, []);
  90. assert.equal(p.sourceVector[1].pSource, p.sourceVector[0]);
  91. assert.equal(p.sourceVector[2].pSource, p.sourceVector[1]);
  92. },
  93. "should iterate through sources and return resultant array":function(){
  94. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]),
  95. result = {};
  96. p.run(result, []);
  97. assert.deepEqual(result.result, [5,4,3,2,1,0]);//see the test source for why this should be so
  98. }
  99. }
  100. }
  101. };
  102. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();