PipelineD_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
  3. var assert = require("assert"),
  4. Pipeline = require("../../../lib/pipeline/Pipeline"),
  5. PipelineD = require("../../../lib/pipeline/PipelineD"),
  6. CursorDocumentSource = require("../../../lib/pipeline/documentSources/CursorDocumentSource");
  7. module.exports = {
  8. "PipelineD": {
  9. "prepareCursorSource": {
  10. "should place a CursorDocumentSource in pipeline": function () {
  11. var p = Pipeline.parseCommand({pipeline:[{$match:{a:true}}], aggregate:[]});
  12. PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  13. assert.equal(p.sources[0].constructor, CursorDocumentSource);
  14. },
  15. "should get projection from all sources": function () {
  16. var p = Pipeline.parseCommand({pipeline:[{$project:{a:"$x"}}], aggregate:[]});
  17. PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  18. assert.deepEqual(p.sources[0]._projection, {x:1, _id:1});
  19. assert.deepEqual(p.sources[0]._dependencies, {_fields:{_id:true, x:true}});
  20. },
  21. "should get projection's deps": function () {
  22. var cmdObj = {
  23. aggregate: [],
  24. pipeline: [
  25. {$match:{
  26. x:{$exists:true},
  27. y:{$exists:false}
  28. }},
  29. {$project:{
  30. a:"$a.b.c",
  31. b:"$d",
  32. c:"$e.f.g"
  33. }},
  34. {$group:{
  35. _id:"$a",
  36. x:{$push:"b"}
  37. }}
  38. ]
  39. };
  40. var p = Pipeline.parseCommand(cmdObj);
  41. PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  42. assert.deepEqual(p.sources[0]._projection, {"a.b.c": 1, d: 1, "e.f.g": 1, _id: 1});
  43. assert.deepEqual(p.sources[0]._dependencies, {"_fields":{"_id":true,"a":{"b":{"c":true}},"d":true,"e":{"f":{"g":true}}}});
  44. },
  45. "should get group's deps": function(){
  46. var cmdObj = {
  47. aggregate: [],
  48. pipeline: [
  49. {$match:{
  50. x:{$exists:true},
  51. y:{$exists:false}
  52. }},
  53. {$group:{
  54. _id:"$a",
  55. x:{$push:"$b"},
  56. y:{$addToSet:"$x.y.z"},
  57. z:{$sum:"$x.y.z.w"}
  58. }},
  59. {$project:{
  60. a:"$a.b.c",
  61. b:"$d",
  62. c:"$e.f.g"
  63. }}
  64. ]
  65. };
  66. var p = Pipeline.parseCommand(cmdObj);
  67. PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  68. assert.equal(JSON.stringify(p.sources[0]._projection), JSON.stringify({ a: 1, b: 1, "x.y.z": 1, _id: 0 }));
  69. assert.deepEqual(p.sources[0]._dependencies, {"_fields":{"a":true,"b":true,"x":{"y":{"z":true}}}});
  70. },
  71. "should set the queryObj on the Cursor": function(){
  72. var cmdObj = {
  73. aggregate: [],
  74. pipeline: [
  75. {$match:{
  76. x:{$exists:true},
  77. y:{$exists:false}
  78. }}
  79. ]
  80. };
  81. var p = Pipeline.parseCommand(cmdObj);
  82. PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  83. assert.deepEqual(p.sources[0]._query, {x:{$exists: true}, y:{$exists:false}});
  84. },
  85. "should set the sort on the Cursor": function(){
  86. var cmdObj = {
  87. aggregate: [],
  88. pipeline: [
  89. {$sort:{
  90. x:1,
  91. y:-1
  92. }}
  93. ]
  94. };
  95. var p = Pipeline.parseCommand(cmdObj);
  96. PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  97. assert.deepEqual(p.sources[0]._sort, {x:1, y:-1});
  98. },
  99. "should set the sort on the Cursor if there is a match first": function(){
  100. var cmdObj = {
  101. aggregate: [],
  102. pipeline: [
  103. {$match:{
  104. x:{$exists:true},
  105. y:{$exists:false}
  106. }},
  107. {$sort:{
  108. x:1,
  109. y:-1
  110. }}
  111. ]
  112. };
  113. var p = Pipeline.parseCommand(cmdObj);
  114. PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  115. assert.deepEqual(p.sources[0]._sort, {x:1, y:-1});
  116. },
  117. "should coalesce the Cursor with the rest of the pipeline": function(){
  118. var cmdObj = {
  119. aggregate: [],
  120. pipeline: [
  121. {$limit:1}
  122. ]
  123. };
  124. var p = Pipeline.parseCommand(cmdObj);
  125. PipelineD.prepareCursorSource(p, {ns:[1,2,3,4,5]});
  126. assert.equal(p.sources[0].getLimit(), 1);
  127. assert.equal(p.sources.length, 1);
  128. },
  129. }
  130. }
  131. };