PipelineD.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. var assert = require("assert"),
  3. Pipeline = require("../../../lib/pipeline/Pipeline"),
  4. PipelineD = require("../../../lib/pipeline/PipelineD"),
  5. DocumentSource = require('../../../lib/pipeline/documentSources/DocumentSource'),
  6. CursorDocumentSource = require('../../../lib/pipeline/documentSources/CursorDocumentSource');
  7. module.exports = {
  8. "Pipeline": {
  9. before: function(){
  10. Pipeline.StageDesc.$test = (function(){
  11. var klass = function TestDocumentSource(options){
  12. base.call(this);
  13. this.shouldCoalesce = options.coalesce;
  14. this.coalesceWasCalled = false;
  15. this.optimizeWasCalled = false;
  16. this.resetWasCalled = false;
  17. this.current = 5;
  18. }, TestDocumentSource = klass, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  19. proto.coalesce = function(){
  20. this.coalesceWasCalled = true;
  21. var c = this.shouldCoalesce;//only coalesce with the first thing we find
  22. this.shouldCoalesce = false;
  23. return c;
  24. };
  25. proto.optimize = function(){
  26. this.optimizeWasCalled = true;
  27. };
  28. proto.eof = function(){
  29. return this.current < 0;
  30. };
  31. proto.advance = function(){
  32. this.current = this.current - 1;
  33. return !this.eof();
  34. };
  35. proto.getCurrent = function(){
  36. return this.current;
  37. };
  38. proto.reset = function(){
  39. this.resetWasCalled = true;
  40. };
  41. proto.getDependencies = function(deps){
  42. if (!deps.testDep){
  43. deps.testDep = 1;
  44. return DocumentSource.GetDepsReturn.SEE_NEXT;
  45. }
  46. return DocumentSource.GetDepsReturn.EXHAUSTIVE;
  47. };
  48. klass.createFromJson = function(options){
  49. return new TestDocumentSource(options);
  50. };
  51. return klass;
  52. })().createFromJson;
  53. //TODO:remove this once Sort is implemented!!!
  54. Pipeline.SortDocumentSource = (function(){
  55. var klass = function SortDocumentSource(){
  56. }, SortDocumentSource = klass, base = require('../../../lib/pipeline/documentSources/DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  57. klass.sortName = "$sort";
  58. klass.createFromJson = function(options){
  59. return new SortDocumentSource(options);
  60. };
  61. return klass;
  62. })();
  63. Pipeline.StageDesc.$sort = Pipeline.SortDocumentSource.createFromJson;
  64. },
  65. "prepareCursorSource": {
  66. "should call reset on all sources":function(){
  67. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}]);
  68. PipelineD.prepareCursorSource(p, [1,2,3,4,5]);
  69. p.sourceVector.forEach(function(source){
  70. assert.equal(source.resetWasCalled, true);
  71. });
  72. },
  73. "should return a CursorDocumentSource":function(){
  74. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}]),
  75. cs = PipelineD.prepareCursorSource(p, [1,2,3,4,5]);
  76. assert.equal(cs.constructor, CursorDocumentSource);
  77. },
  78. "should get projection from all sources":function(){
  79. var p = Pipeline.parseCommand([{$test:{coalesce:false}}, {$test:{coalesce:false}}]),
  80. cs = PipelineD.prepareCursorSource(p, [1,2,3,4,5]);
  81. assert.deepEqual(cs._projection, {"_id":0,"testDep":1});
  82. }
  83. }
  84. }
  85. };
  86. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();