CursorDocumentSource.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. Cursor = require("../../../../lib/Cursor");
  9. var getCursor = function(values) {
  10. if (!values)
  11. values = [1,2,3,4,5];
  12. var cwc = new CursorDocumentSource.CursorWithContext();
  13. cwc._cursor = new Cursor( values );
  14. return new CursorDocumentSource(cwc);
  15. };
  16. module.exports = {
  17. "CursorDocumentSource": {
  18. "constructor(data)": {
  19. "should fail if CursorWithContext is not provided": function(){
  20. assert.throws(function(){
  21. var cds = new CursorDocumentSource();
  22. });
  23. },
  24. "should get a accept a CursorWithContext and set it internally": function(){
  25. var cwc = new CursorDocumentSource.CursorWithContext();
  26. cwc._cursor = new Cursor( [] );
  27. var cds = new CursorDocumentSource(cwc);
  28. assert.ok(cds._cursorWithContext);
  29. }
  30. },
  31. "#coalesce": {
  32. "should be able to coalesce a limit into itself": function (){
  33. var cds = getCursor(),
  34. lds = LimitDocumentSource.createFromJson(2);
  35. assert.equal(cds.coalesce(lds) instanceof LimitDocumentSource, true);
  36. assert.equal(cds.getLimit(), 2);
  37. },
  38. "should keep original limit if coalesced to a larger limit": function() {
  39. var cds = getCursor();
  40. cds.coalesce(LimitDocumentSource.createFromJson(2));
  41. cds.coalesce(LimitDocumentSource.createFromJson(3));
  42. assert.equal(cds.getLimit(), 2);
  43. },
  44. "cursor only returns $limit number when coalesced": function(next) {
  45. var cds = getCursor(),
  46. lds = LimitDocumentSource.createFromJson(2);
  47. cds.coalesce(lds);
  48. var docs = [], i = 0;
  49. async.doWhilst(
  50. function(cb) {
  51. cds.getNext(function(err, val) {
  52. docs[i] = val;
  53. return cb(err);
  54. });
  55. },
  56. function() {
  57. return docs[i++] !== DocumentSource.EOF;
  58. },
  59. function(err) {
  60. assert.deepEqual([1, 2, DocumentSource.EOF], docs);
  61. next();
  62. }
  63. );
  64. },
  65. "should leave non-limit alone": function () {
  66. var cwc = new CursorDocumentSource.CursorWithContext();
  67. cwc._cursor = new Cursor( [] );
  68. var sds = new SkipDocumentSource(),
  69. cds = new CursorDocumentSource(cwc);
  70. assert.equal(cds.coalesce(sds), false);
  71. }
  72. },
  73. "#getNext": {
  74. "should throw an error if no callback is given": function() {
  75. var cwc = new CursorDocumentSource.CursorWithContext();
  76. cwc._cursor = new Cursor( [1,2,3,4] );
  77. var cds = new CursorDocumentSource(cwc);
  78. assert.throws(cds.getNext.bind(cds));
  79. },
  80. "should return the current cursor value async": function(next){
  81. var expected = JSON.stringify([1,2]);
  82. var cwc = new CursorDocumentSource.CursorWithContext();
  83. cwc._cursor = new Cursor( [1,2,3,4] );
  84. var cds = new CursorDocumentSource(cwc);
  85. async.series([
  86. cds.getNext.bind(cds),
  87. cds.getNext.bind(cds),
  88. cds.getNext.bind(cds),
  89. cds.getNext.bind(cds),
  90. cds.getNext.bind(cds),
  91. ],
  92. function(err,res) {
  93. assert.deepEqual([1,2,3,4,DocumentSource.EOF], res);
  94. next();
  95. }
  96. );
  97. },
  98. "should return values past the batch limit": function(next){
  99. var cwc = new CursorDocumentSource.CursorWithContext(),
  100. n = 0,
  101. arr = Array.apply(0, new Array(200)).map(function() { return n++; });
  102. cwc._cursor = new Cursor( arr );
  103. var cds = new CursorDocumentSource(cwc);
  104. async.each(arr,
  105. function(a,next) {
  106. cds.getNext(function(err,val) {
  107. assert.equal(val,a);
  108. next(err);
  109. });
  110. },
  111. function(err) {
  112. assert.equal(err, null);
  113. }
  114. );
  115. cds.getNext(function(err,val) {
  116. assert.equal(val, DocumentSource.EOF);
  117. next();
  118. });
  119. },
  120. },
  121. "#dispose": {
  122. "should empty the current cursor": function(next){
  123. var cwc = new CursorDocumentSource.CursorWithContext();
  124. cwc._cursor = new Cursor( [1,2,3] );
  125. var cds = new CursorDocumentSource(cwc);
  126. async.series([
  127. cds.getNext.bind(cds),
  128. cds.getNext.bind(cds),
  129. cds.getNext.bind(cds),
  130. cds.getNext.bind(cds),
  131. ],
  132. function(err,res) {
  133. assert.deepEqual([1,2,3,DocumentSource.EOF], res);
  134. next();
  135. }
  136. );
  137. }
  138. },
  139. "#setProjection": {
  140. "should set a projection": function() {
  141. var cwc = new CursorDocumentSource.CursorWithContext();
  142. cwc._cursor = new Cursor( [1,2,3] );
  143. var cds = new CursorDocumentSource(cwc);
  144. cds.setProjection({a:1}, {a:true});
  145. assert.deepEqual(cds._projection, {a:1});
  146. assert.deepEqual(cds._dependencies, {a:true});
  147. },
  148. "should throw an error if projection is already set": function (){
  149. var cwc = new CursorDocumentSource.CursorWithContext();
  150. cwc._cursor = new Cursor( [1,2,3] );
  151. var cds = new CursorDocumentSource(cwc);
  152. cds.setProjection({a:1}, {});
  153. assert.throws(function() {
  154. cds.setProjection({a:1}, {});
  155. });
  156. },
  157. "should project properly": function(next) {
  158. var cwc = new CursorDocumentSource.CursorWithContext();
  159. cwc._cursor = new Cursor( [{a:1},{a:2,b:3},{c:4,d:5}] );
  160. var cds = new CursorDocumentSource(cwc);
  161. cds.setProjection({a:1}, {a:true});
  162. assert.deepEqual(cds._projection, {a:1});
  163. assert.deepEqual(cds._dependencies, {a:true});
  164. async.series([
  165. cds.getNext.bind(cds),
  166. cds.getNext.bind(cds),
  167. cds.getNext.bind(cds),
  168. cds.getNext.bind(cds),
  169. ],
  170. function(err,res) {
  171. assert.deepEqual([{a:1},{a:2},{},DocumentSource.EOF], res);
  172. next();
  173. }
  174. );
  175. }
  176. }
  177. }
  178. };
  179. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();