PipelineD.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "PipelineD": {
  9. before: function(){
  10. Pipeline.stageDesc.$test = (function(){
  11. var klass = function TestDocumentSource(options, ctx){
  12. base.call(this, ctx);
  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.EXHAUSTIVE;
  45. }
  46. return DocumentSource.GetDepsReturn.SEE_NEXT;
  47. };
  48. klass.createFromJson = function(options, ctx){
  49. return new TestDocumentSource(options, ctx);
  50. };
  51. return klass;
  52. })().createFromJson;
  53. },
  54. "prepareCursorSource": {
  55. "should place a CursorDocumentSource in pipeline": function () {
  56. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}], aggregate:[]}),
  57. cs = PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  58. assert.equal(p.sources[0].constructor, CursorDocumentSource);
  59. },
  60. "should get projection from all sources": function () {
  61. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}], aggregate:[]}),
  62. cs = PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  63. assert.deepEqual(p.sources[0]._projection, {"_id":0,"testDep":1});
  64. },
  65. "should get projection's deps": function () {
  66. var cmdObj = {
  67. aggregate: [],
  68. pipeline: [
  69. {$match:{
  70. x:{$exists:true},
  71. y:{$exists:false}
  72. }},
  73. {$project:{
  74. a:"$a.b.c",
  75. b:"$d",
  76. c:"$e.f.g"
  77. }},
  78. {$group:{
  79. _id:"$a",
  80. x:{$push:"b"}
  81. }}
  82. ]
  83. };
  84. var p = Pipeline.parseCommand(cmdObj),
  85. cs = PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  86. assert.equal(JSON.stringify(p.sources[0]._projection), JSON.stringify({'a.b.c': 1, d: 1, 'e.f.g': 1, _id: 1}));
  87. },
  88. "should get group's deps": function(){
  89. var cmdObj = {
  90. aggregate: [],
  91. pipeline: [
  92. {$match:{
  93. x:{$exists:true},
  94. y:{$exists:false}
  95. }},
  96. {$group:{
  97. _id:"$a",
  98. x:{$push:"$b"},
  99. y:{$addToSet:"$x.y.z"},
  100. z:{$sum:"$x.y.z.w"}
  101. }},
  102. {$project:{
  103. a:"$a.b.c",
  104. b:"$d",
  105. c:"$e.f.g"
  106. }}
  107. ]
  108. };
  109. var p = Pipeline.parseCommand(cmdObj),
  110. cs = PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  111. assert.equal(JSON.stringify(p.sources[0]._projection), JSON.stringify({ _id: 0, a: 1, b: 1, 'x.y.z': 1 }));
  112. }
  113. }
  114. }
  115. };
  116. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);