CursorDocumentSource.js 3.9 KB

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