SkipDocumentSource_test.js 5.1 KB

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