Pipeline.js 4.1 KB

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