PipelineD.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "prepareCursorSource": {
  10. "should place a CursorDocumentSource in pipeline": function () {
  11. var p = Pipeline.parseCommand({pipeline:[{$match:{a:true}}], aggregate:[]}),
  12. cs = 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. cs = 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. cs = 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. cs = 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. "should set the sort on the Cursor": function(){},
  73. "should set the sort on the Cursor if there is a match first": function(){},
  74. "should coalesce the Cursor with the rest of the pipeline": function(){},
  75. }
  76. }
  77. };
  78. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);