PipelineD.js 3.0 KB

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