Pipeline.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. var assert = require("assert"),
  3. Pipeline = require("../../../lib/pipeline/Pipeline"),
  4. DocumentSource = require('../../../lib/pipeline/documentSources/DocumentSource');
  5. module.exports = {
  6. "Pipeline": {
  7. before: function(){
  8. Pipeline.stageDesc.$test = (function(){
  9. var klass = function TestDocumentSource(options, ctx){
  10. base.call(this, ctx);
  11. this.shouldCoalesce = options.coalesce;
  12. this.coalesceWasCalled = false;
  13. this.optimizeWasCalled = false;
  14. this.current = 5;
  15. }, TestDocumentSource = klass, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  16. proto.coalesce = function(){
  17. this.coalesceWasCalled = true;
  18. var c = this.shouldCoalesce;//only coalesce with the first thing we find
  19. this.shouldCoalesce = false;
  20. return c;
  21. };
  22. proto.optimize = function(){
  23. this.optimizeWasCalled = true;
  24. };
  25. proto.eof = function(){
  26. return this.current < 0;
  27. };
  28. proto.advance = function(){
  29. this.current = this.current - 1;
  30. return !this.eof();
  31. };
  32. proto.getCurrent = function(){
  33. return this.current;
  34. };
  35. klass.createFromJson = function(options, ctx){
  36. return new TestDocumentSource(options, ctx);
  37. };
  38. return klass;
  39. })().createFromJson;
  40. },
  41. "parseCommand": {
  42. "should throw Error if given non-objects in the array": function () {
  43. assert.throws(function(){
  44. Pipeline.parseCommand({pipeline:[5]});
  45. });
  46. },
  47. "should throw Error if given objects with more / less than one field": function () {
  48. assert.throws(function(){
  49. Pipeline.parseCommand({pipeline:[{}]});
  50. Pipeline.parseCommand({pipeline:[{a:1,b:2}]});
  51. });
  52. },
  53. "should throw Error on unknown document sources": function () {
  54. assert.throws(function(){
  55. Pipeline.parseCommand({pipeline:[{$foo:"$sdfdf"}]});
  56. });
  57. },
  58. "should swap $match and $sort if the $match immediately follows the $sort": function () {
  59. var p = Pipeline.parseCommand({pipeline:[{$sort:{"xyz":1}}, {$match:{}}]});
  60. assert.equal(p.sourceVector[0].constructor.matchName, "$match");
  61. assert.equal(p.sourceVector[1].constructor.sortName, "$sort");
  62. },
  63. "should attempt to coalesce all sources": function () {
  64. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:true}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]});
  65. assert.equal(p.sourceVector.length, 3);
  66. p.sourceVector.slice(0,-1).forEach(function(source){
  67. assert.equal(source.coalesceWasCalled, true);
  68. });
  69. assert.equal(p.sourceVector[p.sourceVector.length -1].coalesceWasCalled, false);
  70. },
  71. "should optimize all sources": function () {
  72. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}]});
  73. p.sourceVector.forEach(function(source){
  74. assert.equal(source.optimizeWasCalled, true);
  75. });
  76. }
  77. },
  78. "#run": {
  79. "should set the parent source for all sources in the pipeline except the first one": function (next) {
  80. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]});
  81. p.run(new DocumentSource({}), function(err, results){
  82. assert.equal(p.sourceVector[1].source, p.sourceVector[0]);
  83. assert.equal(p.sourceVector[2].source, p.sourceVector[1]);
  84. return next();
  85. });
  86. },
  87. "should iterate through sources and return resultant array": function (next) {
  88. var p = Pipeline.parseCommand({pipeline:[{$test:{coalesce:false}}, {$test:{coalesce:false}}, {$test:{coalesce:false}}]});
  89. p.run(new DocumentSource({}), function(err, results){
  90. assert.deepEqual(results.result, [5,4,3,2,1,0]); //see the test source for why this should be so
  91. return next();
  92. });
  93. },
  94. "should call callback with errors from pipeline components": function (next) {
  95. // The $foo part is invalid and causes a throw.
  96. assert.throws(function(){
  97. Pipeline.parseCommand({pipeline:[{$match:{$foo:{bar:"baz"}}}]});
  98. });
  99. }
  100. }
  101. }
  102. };
  103. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);