DocumentSourceRunner.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. var assert = require("assert"),
  3. Runner = require("../../../lib/query/Runner"),
  4. CursorDocumentSource = require("../../../lib/pipeline/documentSources/CursorDocumentSource"),
  5. LimitDocumentSource = require("../../../lib/pipeline/documentSources/LimitDocumentSource"),
  6. MatchDocumentSource = require("../../../lib/pipeline/documentSources/MatchDocumentSource"),
  7. ArrayRunner = require("../../../lib/query/ArrayRunner"),
  8. DocumentSourceRunner = require("../../../lib/query/DocumentSourceRunner");
  9. module.exports = {
  10. "ArrayRunner": {
  11. "#constructor": {
  12. "should accept an array of data": function(){
  13. var cds = new CursorDocumentSource(null, new ArrayRunner([]), null),
  14. pipeline = [];
  15. assert.doesNotThrow(function(){
  16. var ar = new DocumentSourceRunner(cds, pipeline);
  17. });
  18. },
  19. "should fail if not given a document source or pipeline": function(){
  20. var cds = new CursorDocumentSource(null, new ArrayRunner([]), null);
  21. assert.throws(function(){
  22. var ar = new DocumentSourceRunner();
  23. });
  24. assert.throws(function(){
  25. var ar = new DocumentSourceRunner(123);
  26. });
  27. assert.throws(function(){
  28. var ar = new DocumentSourceRunner(cds, 123);
  29. });
  30. },
  31. "should coalesce the pipeline into the given documentsource": function(){
  32. var cds = new CursorDocumentSource(null, new ArrayRunner([]), null),
  33. pipeline = [new LimitDocumentSource(3), new MatchDocumentSource({"a":true})],
  34. expected = [{$match:{a:true}}];
  35. var ds = new DocumentSourceRunner(cds, pipeline);
  36. var actual = pipeline.map(function(d){return d.serialize();});
  37. assert.deepEqual(expected, actual);
  38. }
  39. },
  40. "#getNext": {
  41. "should return the next item in the given documentsource": function(done){
  42. var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
  43. pipeline = [new LimitDocumentSource(3)];
  44. var ds = new DocumentSourceRunner(cds, pipeline);
  45. ds.getNext(function(err, out, state){
  46. assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
  47. assert.strictEqual(out, 1);
  48. ds.getNext(function(err, out, state){
  49. assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
  50. assert.strictEqual(out, 2);
  51. ds.getNext(function(err, out, state){
  52. assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
  53. assert.strictEqual(out, 3);
  54. done();
  55. });
  56. });
  57. });
  58. },
  59. "should return EOF if there is nothing left in the given documentsource": function(done){
  60. var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
  61. pipeline = [new LimitDocumentSource({}, 1)];
  62. var ds = new DocumentSourceRunner(cds, pipeline);
  63. ds.getNext(function(err, out, state){
  64. assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
  65. assert.strictEqual(out, 1);
  66. ds.getNext(function(err, out, state){
  67. assert.strictEqual(state, Runner.RunnerState.RUNNER_EOF);
  68. assert.strictEqual(out, null);
  69. done();
  70. });
  71. });
  72. }
  73. },
  74. "#getInfo": {
  75. "should return nothing if explain flag is not set": function(){
  76. var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
  77. pipeline = [new LimitDocumentSource({}, 1)];
  78. var ds = new DocumentSourceRunner(cds, pipeline);
  79. assert.strictEqual(ds.getInfo(), undefined);
  80. },
  81. "should return information about the runner if explain flag is set": function(){
  82. var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
  83. pipeline = [new LimitDocumentSource({}, 1)];
  84. var ds = new DocumentSourceRunner(cds, pipeline);
  85. assert.deepEqual(ds.getInfo(true), {
  86. "type": "DocumentSourceRunner",
  87. "docSrc": {
  88. "$cursor": {
  89. "query": undefined,
  90. "sort": null,
  91. "limit": 1,
  92. "fields": null,
  93. "plan": {
  94. "type": "ArrayRunner",
  95. "nDocs": 3,
  96. "position": 0,
  97. "state": "RUNNER_ADVANCED"
  98. }
  99. }
  100. },
  101. "state": "RUNNER_ADVANCED"
  102. });
  103. }
  104. },
  105. "#reset": {
  106. "should dispose of the documentSource": function(){
  107. var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
  108. pipeline = [new LimitDocumentSource({}, 1)];
  109. var ds = new DocumentSourceRunner(cds, pipeline);
  110. ds.reset();
  111. assert.deepEqual(ds.getInfo(true), {
  112. "type": "DocumentSourceRunner",
  113. "docSrc": {
  114. "$cursor": {
  115. "query": undefined,
  116. "sort": null,
  117. "limit": 1,
  118. "fields": null,
  119. "plan": {
  120. "type": "ArrayRunner",
  121. "nDocs": 0,
  122. "position": 0,
  123. "state": "RUNNER_DEAD"
  124. }
  125. }
  126. },
  127. "state": "RUNNER_DEAD"
  128. });
  129. }
  130. }
  131. }
  132. };
  133. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();