CursorDocumentSource_test.js 4.3 KB

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