SkipDocumentSource_test.js 5.1 KB

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