CursorDocumentSource.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. var assert = require("assert"),
  3. async = require("async"),
  4. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  5. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  6. LimitDocumentSource = require("../../../../lib/pipeline/documentSources/LimitDocumentSource"),
  7. SkipDocumentSource = require("../../../../lib/pipeline/documentSources/SkipDocumentSource"),
  8. ProjectDocumentSource = require("../../../../lib/pipeline/documentSources/ProjectDocumentSource"),
  9. DepsTracker = require("../../../../lib/pipeline/DepsTracker"),
  10. ArrayRunner = require("../../../../lib/query/ArrayRunner");
  11. var getCursorDocumentSource = function(values) {
  12. values = values || [1,2,3,4,5];
  13. return new CursorDocumentSource(null, new ArrayRunner(values), null);
  14. };
  15. module.exports = {
  16. "CursorDocumentSource": {
  17. "constructor(data)": {
  18. "should get a accept a CursorWithContext and set it internally": function(){
  19. var cds = getCursorDocumentSource([]);
  20. assert.ok(cds._runner);
  21. }
  22. },
  23. "#coalesce": {
  24. "should be able to coalesce a limit into itself": function (){
  25. var cds = getCursorDocumentSource(),
  26. lds = LimitDocumentSource.createFromJson(2);
  27. assert.equal(cds.coalesce(lds) instanceof LimitDocumentSource, true);
  28. assert.equal(cds.getLimit(), 2);
  29. },
  30. "should keep original limit if coalesced to a larger limit": function() {
  31. var cds = getCursorDocumentSource();
  32. cds.coalesce(LimitDocumentSource.createFromJson(2));
  33. cds.coalesce(LimitDocumentSource.createFromJson(3));
  34. assert.equal(cds.getLimit(), 2);
  35. },
  36. "cursor only returns $limit number when coalesced": function(next) {
  37. var cds = getCursorDocumentSource(),
  38. lds = LimitDocumentSource.createFromJson(2);
  39. cds.coalesce(lds);
  40. var docs = [], i = 0;
  41. async.doWhilst(
  42. function(cb) {
  43. cds.getNext(function(err, val) {
  44. docs[i] = val;
  45. return cb(err);
  46. });
  47. },
  48. function() {
  49. return docs[i++] !== null;
  50. },
  51. function(err) {
  52. if (err) throw err;
  53. assert.deepEqual([1, 2, null], docs);
  54. next();
  55. }
  56. );
  57. },
  58. "should leave non-limit alone": function () {
  59. var sds = new SkipDocumentSource(),
  60. cds = getCursorDocumentSource([]);
  61. assert.equal(cds.coalesce(sds), false);
  62. }
  63. },
  64. "#getNext": {
  65. "should return the current cursor value async": function(next){
  66. var expected = JSON.stringify([1,2]);
  67. var cds = getCursorDocumentSource([1,2,3,4]);
  68. async.series([
  69. cds.getNext.bind(cds),
  70. cds.getNext.bind(cds),
  71. cds.getNext.bind(cds),
  72. cds.getNext.bind(cds),
  73. cds.getNext.bind(cds),
  74. ],
  75. function(err,res) {
  76. assert.deepEqual([1,2,3,4,null], res);
  77. next();
  78. }
  79. );
  80. },
  81. "should return values past the batch limit": function(next){
  82. var n = 0,
  83. arr = Array.apply(0, new Array(200)).map(function() { return n++; });
  84. var cds = getCursorDocumentSource(arr);
  85. async.each(arr,
  86. function(a,next) {
  87. cds.getNext(function(err,val) {
  88. assert.equal(val,a);
  89. next(err);
  90. });
  91. },
  92. function(err) {
  93. assert.equal(err, null);
  94. }
  95. );
  96. cds.getNext(function(err,val) {
  97. assert.equal(val, null);
  98. next();
  99. });
  100. },
  101. },
  102. "#dispose": {
  103. "should empty the current cursor": function(next){
  104. var cds = getCursorDocumentSource();
  105. async.series([
  106. cds.getNext.bind(cds),
  107. cds.getNext.bind(cds),
  108. function(next){
  109. cds.dispose();
  110. return cds.getNext(next);
  111. }
  112. ],
  113. function(err,res) {
  114. assert.deepEqual([1,2,null], res);
  115. next();
  116. }
  117. );
  118. }
  119. },
  120. "#setProjection": {
  121. "should set a projection": function(next) {
  122. var cds = getCursorDocumentSource([{a:1, b:2},{a:2, b:3}]),
  123. deps = new DepsTracker(),
  124. project = ProjectDocumentSource.createFromJson({"a":1});
  125. project.getDependencies(deps);
  126. cds.setProjection(deps.toProjection(), deps.toParsedDeps());
  127. async.series([
  128. cds.getNext.bind(cds),
  129. cds.getNext.bind(cds),
  130. cds.getNext.bind(cds)
  131. ],
  132. function(err,res) {
  133. assert.deepEqual([{a:1},{a:2},null], res);
  134. next();
  135. }
  136. );
  137. }
  138. }
  139. }
  140. };
  141. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);