CursorDocumentSource.js 4.9 KB

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