SkipDocumentSource.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. var assert = require("assert"),
  3. async = require("async"),
  4. Cursor = require("../../../../lib/Cursor"),
  5. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  6. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  7. SkipDocumentSource = require("../../../../lib/pipeline/documentSources/SkipDocumentSource");
  8. module.exports = {
  9. "SkipDocumentSource": {
  10. "constructor()": {
  11. "should not throw Error when constructing without args": function testConstructor(){
  12. assert.doesNotThrow(function(){
  13. new SkipDocumentSource();
  14. });
  15. }
  16. },
  17. '#create()': {
  18. 'should create a direct copy of a SkipDocumentSource created through the constructor': function () {
  19. var sds1 = new SkipDocumentSource(),
  20. sds2 = SkipDocumentSource.create();
  21. assert.strictEqual(JSON.stringify(sds1), JSON.stringify(sds2));
  22. }
  23. },
  24. "#getSourceName()": {
  25. "should return the correct source name; $skip": function testSourceName(){
  26. var sds = new SkipDocumentSource();
  27. assert.strictEqual(sds.getSourceName(), "$skip");
  28. }
  29. },
  30. '#getSkip()': {
  31. 'should return the skips': function () {
  32. var sds = new SkipDocumentSource();
  33. assert.strictEqual(sds.getSkip(), 0);
  34. }
  35. },
  36. '#setSkip()': {
  37. 'should return the skips': function () {
  38. var sds = new SkipDocumentSource();
  39. sds.setSkip(10);
  40. assert.strictEqual(sds.getSkip(), 10);
  41. }
  42. },
  43. "#coalesce()": {
  44. "should return false if nextSource is not $skip": function dontSkip(){
  45. var sds = new SkipDocumentSource();
  46. assert.equal(sds.coalesce({}), false);
  47. },
  48. "should return true if nextSource is $skip": function changeSkip(){
  49. var sds = new SkipDocumentSource();
  50. assert.equal(sds.coalesce(new SkipDocumentSource()), true);
  51. }
  52. },
  53. "#getNext()": {
  54. "should throw an error if no callback is given": function() {
  55. var sds = new SkipDocumentSource();
  56. assert.throws(sds.getNext.bind(sds));
  57. },
  58. "should return EOF if there are no more sources": function noSources(next){
  59. var sds = new SkipDocumentSource();
  60. sds.skip = 3;
  61. sds.count = 0;
  62. var expected = [
  63. {val:4},
  64. DocumentSource.EOF
  65. ];
  66. var cwc = new CursorDocumentSource.CursorWithContext();
  67. var input = [
  68. {val:1},
  69. {val:2},
  70. {val:3},
  71. {val:4},
  72. ];
  73. cwc._cursor = new Cursor( input );
  74. var cds = new CursorDocumentSource(cwc);
  75. sds.setSource(cds);
  76. async.series([
  77. sds.getNext.bind(sds),
  78. sds.getNext.bind(sds),
  79. ],
  80. function(err,res) {
  81. assert.deepEqual(expected, res);
  82. next();
  83. }
  84. );
  85. sds.getNext(function(err, actual) {
  86. assert.equal(actual, DocumentSource.EOF);
  87. });
  88. },
  89. "should return documents if skip count is not hit and there are more documents": function hitSkip(next){
  90. var sds = SkipDocumentSource.createFromJson(1);
  91. var cwc = new CursorDocumentSource.CursorWithContext();
  92. var input = [{val:1},{val:2},{val:3}];
  93. cwc._cursor = new Cursor( input );
  94. var cds = new CursorDocumentSource(cwc);
  95. sds.setSource(cds);
  96. sds.getNext(function(err,actual) {
  97. assert.notEqual(actual, DocumentSource.EOF);
  98. assert.deepEqual(actual, {val:2});
  99. next();
  100. });
  101. },
  102. "should return the current document source": function currSource(){
  103. var sds = SkipDocumentSource.createFromJson(1);
  104. var cwc = new CursorDocumentSource.CursorWithContext();
  105. var input = [{val:1},{val:2},{val:3}];
  106. cwc._cursor = new Cursor( input );
  107. var cds = new CursorDocumentSource(cwc);
  108. sds.setSource(cds);
  109. sds.getNext(function(err, actual) {
  110. assert.deepEqual(actual, { val:2 });
  111. });
  112. },
  113. "should return false if we hit our limit": function noMoar(next){
  114. var sds = new SkipDocumentSource();
  115. sds.skip = 3;
  116. var expected = [
  117. {item:4},
  118. DocumentSource.EOF
  119. ];
  120. var i = 1;
  121. sds.source = {
  122. getNext:function(cb){
  123. if (i>=5)
  124. return cb(null,DocumentSource.EOF);
  125. return cb(null, { item:i++ });
  126. }
  127. };
  128. async.series([
  129. sds.getNext.bind(sds),
  130. sds.getNext.bind(sds),
  131. ],
  132. function(err,res) {
  133. assert.deepEqual(expected, res);
  134. next();
  135. }
  136. );
  137. }
  138. },
  139. "#serialize()": {
  140. "should create an object with a key $skip and the value equal to the skip": function sourceToJsonTest(){
  141. var sds = new SkipDocumentSource();
  142. sds.skip = 9;
  143. var t = sds.serialize(false);
  144. assert.deepEqual(t, { "$skip": 9 });
  145. }
  146. },
  147. "#createFromJson()": {
  148. "should return a new SkipDocumentSource object from an input number": function createTest(){
  149. var t = SkipDocumentSource.createFromJson(5);
  150. assert.strictEqual(t.constructor, SkipDocumentSource);
  151. assert.strictEqual(t.skip, 5);
  152. }
  153. },
  154. '#getDependencies()': {
  155. 'should return 1 (GET_NEXT)': function () {
  156. var sds = new SkipDocumentSource();
  157. assert.strictEqual(sds.getDependencies(), DocumentSource.GetDepsReturn.SEE_NEXT); // Hackish. We may be getting an enum in somewhere.
  158. }
  159. },
  160. '#getShardSource()': {
  161. 'should return the instance of the SkipDocumentSource': function () {
  162. var sds = new SkipDocumentSource();
  163. assert.strictEqual(sds.getShardSource(), null);
  164. }
  165. },
  166. '#getRouterSource()': {
  167. 'should return null': function () {
  168. var sds = new SkipDocumentSource();
  169. assert.strictEqual(sds.getRouterSource(), sds);
  170. }
  171. }
  172. }
  173. };
  174. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);