PipelineD.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "should get projection's deps": function(){
  84. var cmdObj = [
  85. {$match:{
  86. x:{$exists:true},
  87. y:{$exists:false}
  88. }},
  89. {$project:{
  90. a:"$a.b.c",
  91. b:"$d",
  92. c:"$e.f.g"
  93. }},
  94. {$group:{
  95. _id:"$a",
  96. x:{$push:"b"}
  97. }}
  98. ];
  99. var p = Pipeline.parseCommand(cmdObj),
  100. cs = PipelineD.prepareCursorSource(p, [1,2,3,4,5]);
  101. assert.equal(JSON.stringify(cs._projection), JSON.stringify({_id: 1, 'a.b.c': 1, d: 1, 'e.f.g': 1}));
  102. },
  103. "should get group's deps": function(){
  104. var cmdObj = [
  105. {$match:{
  106. x:{$exists:true},
  107. y:{$exists:false}
  108. }},
  109. {$group:{
  110. _id:"$a",
  111. x:{$push:"$b"},
  112. y:{$addToSet:"$x.y.z"},
  113. z:{$sum:"$x.y.z.w"}
  114. }},
  115. {$project:{
  116. a:"$a.b.c",
  117. b:"$d",
  118. c:"$e.f.g"
  119. }}
  120. ];
  121. var p = Pipeline.parseCommand(cmdObj),
  122. cs = PipelineD.prepareCursorSource(p, [1,2,3,4,5]);
  123. assert.equal(JSON.stringify(cs._projection), JSON.stringify({ _id: 0, a: 1, b: 1, x: 1, y: 1 }));
  124. }
  125. }
  126. }
  127. };
  128. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);