CursorDocumentSource_test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. "use strict";
  2. if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
  3. var assert = require("assert"),
  4. async = require("async"),
  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 createSource = function(values) {
  12. values = values || [1,2,3,4,5];
  13. return new CursorDocumentSource(null, new ArrayRunner(values), null);
  14. };
  15. //TODO: port their tests and test classes or document why we chose not to
  16. exports.CursorDocumentSource = {
  17. "constructor(data)": {
  18. "should get a accept a CursorWithContext and set it internally": function() {
  19. var cds = createSource([]);
  20. assert(cds._runner instanceof ArrayRunner);
  21. },
  22. },
  23. "#coalesce": {
  24. "should be able to coalesce a limit into itself": function() {
  25. var cds = createSource(),
  26. lds = LimitDocumentSource.createFromJson(2);
  27. cds.coalesce(lds);
  28. assert.strictEqual(cds.getLimit(), 2);
  29. },
  30. "should keep original limit if coalesced to a larger limit": function() {
  31. var cds = createSource();
  32. cds.coalesce(LimitDocumentSource.createFromJson(2));
  33. cds.coalesce(LimitDocumentSource.createFromJson(3));
  34. assert.strictEqual(cds.getLimit(), 2);
  35. },
  36. "cursor only returns $limit number when coalesced": function(done) {
  37. var cds = createSource(),
  38. lds = LimitDocumentSource.createFromJson(2);
  39. cds.coalesce(lds);
  40. var docs = [],
  41. i = 0;
  42. async.doWhilst(
  43. function iterator(cb) {
  44. cds.getNext(function(err, val) {
  45. docs[i] = val;
  46. return cb(err);
  47. });
  48. },
  49. function test() {
  50. return docs[i++] !== null;
  51. },
  52. function(err) {
  53. assert.ifError(err);
  54. assert.deepEqual([1, 2, null], docs);
  55. return done();
  56. }
  57. );
  58. },
  59. "should leave non-limit alone": function() {
  60. var sds = new SkipDocumentSource(),
  61. cds = createSource([]);
  62. assert.strictEqual(cds.coalesce(sds), false);
  63. },
  64. },
  65. "#getNext": {
  66. "should return the current cursor value async": function(done) {
  67. var cds = createSource([1,2,3,4]);
  68. async.series(
  69. [
  70. cds.getNext.bind(cds),
  71. cds.getNext.bind(cds),
  72. cds.getNext.bind(cds),
  73. cds.getNext.bind(cds),
  74. cds.getNext.bind(cds),
  75. ],
  76. function(err, res) {
  77. assert.ifError(err);
  78. assert.deepEqual([1,2,3,4,null], res);
  79. return done();
  80. }
  81. );
  82. },
  83. "should return values past the batch limit": function(done) {
  84. var arr = Array.apply(0, new Array(100000)).map(function(v, i) { return i; }),
  85. cds = createSource(arr),
  86. docs = [],
  87. doc;
  88. async.doWhilst(
  89. function iterator(next) {
  90. return cds.getNext(function(err, obj) {
  91. if (err) return next(err);
  92. doc = obj;
  93. if (doc !== null) docs.push(doc);
  94. return next();
  95. });
  96. },
  97. function test() {
  98. return doc !== null;
  99. },
  100. function after(err) {
  101. assert.ifError(err);
  102. assert.deepEqual(arr, docs);
  103. return done();
  104. }
  105. );
  106. },
  107. },
  108. "#dispose": {
  109. "should empty the current cursor": function(done) {
  110. var cds = createSource();
  111. async.series(
  112. [
  113. cds.getNext.bind(cds),
  114. cds.getNext.bind(cds),
  115. function(next) {
  116. cds.dispose();
  117. return cds.getNext(next);
  118. }
  119. ],
  120. function(err, res) {
  121. assert.ifError(err);
  122. assert.deepEqual([1,2,null], res);
  123. return done();
  124. }
  125. );
  126. },
  127. },
  128. "#setProjection": {
  129. "should set a projection": function(done) {
  130. var cds = createSource([{a:1, b:2},{a:2, b:3}]),
  131. deps = new DepsTracker(),
  132. project = ProjectDocumentSource.createFromJson({"a":1});
  133. project.getDependencies(deps);
  134. cds.setProjection(deps.toProjection(), deps.toParsedDeps());
  135. async.series(
  136. [
  137. cds.getNext.bind(cds),
  138. cds.getNext.bind(cds),
  139. cds.getNext.bind(cds)
  140. ],
  141. function(err, res) {
  142. assert.ifError(err);
  143. assert.deepEqual([{a:1},{a:2},null], res);
  144. return done();
  145. }
  146. );
  147. },
  148. },
  149. };