SkipDocumentSource.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. var assert = require("assert"),
  3. SkipDocumentSource = require("../../../../lib/pipeline/documentSources/SkipDocumentSource");
  4. module.exports = {
  5. "SkipDocumentSource": {
  6. "constructor()": {
  7. "should not throw Error when constructing without args": function testConstructor(){
  8. assert.doesNotThrow(function(){
  9. new SkipDocumentSource();
  10. });
  11. }
  12. },
  13. "#getSourceName()": {
  14. "should return the correct source name; $skip": function testSourceName(){
  15. var lds = new SkipDocumentSource();
  16. assert.strictEqual(lds.getSourceName(), "$skip");
  17. }
  18. },
  19. "#coalesce()": {
  20. "should return false if nextSource is not $skip": function dontSkip(){
  21. var lds = new SkipDocumentSource();
  22. assert.equal(lds.coalesce({}), false);
  23. },
  24. "should return true if nextSource is $skip": function changeSkip(){
  25. var lds = new SkipDocumentSource();
  26. assert.equal(lds.coalesce(new SkipDocumentSource()), true);
  27. }
  28. },
  29. "#eof()": {
  30. "should return true if there are no more sources": function noSources(){
  31. var lds = new SkipDocumentSource();
  32. lds.skip = 9;
  33. lds.count = 0;
  34. lds.pSource = {
  35. eof: function(){
  36. return true;
  37. }
  38. };
  39. assert.equal(lds.eof(), true);
  40. },
  41. "should return false if skip count is not hit and there are more documents": function hitSkip(){
  42. var lds = new SkipDocumentSource();
  43. lds.skip = 10;
  44. lds.count = 9;
  45. var i = 1;
  46. lds.pSource = {
  47. getCurrent:function(){return { item:i };},
  48. eof: function(){return false;},
  49. advance: function(){i++; return true;}
  50. };
  51. assert.equal(lds.eof(), false);
  52. }
  53. },
  54. "#getCurrent()": {
  55. "should return the current document source": function currSource(){
  56. var lds = new SkipDocumentSource();
  57. lds.skip = 1;
  58. var i = 0;
  59. lds.pSource = {
  60. getCurrent:function(){return { item:i };},
  61. eof: function(){return false;},
  62. advance: function(){i++; return true;}
  63. };
  64. assert.deepEqual(lds.getCurrent(), { item:1 });
  65. }
  66. },
  67. "#advance()": {
  68. "should return true for moving to the next source": function nextSource(){
  69. var lds = new SkipDocumentSource();
  70. lds.skip = 1;
  71. var i = 0;
  72. lds.pSource = {
  73. getCurrent:function(){return { item:i };},
  74. eof: function(){return false;},
  75. advance: function(){i++; return true;}
  76. };
  77. assert.strictEqual(lds.advance(), true);
  78. },
  79. "should return false for no sources remaining": function noMoar(){
  80. var lds = new SkipDocumentSource();
  81. lds.skip = 1;
  82. var i = 0;
  83. lds.pSource = {
  84. getCurrent:function(){return { item:i };},
  85. eof: function(){return true;},
  86. advance: function(){return false;}
  87. };
  88. assert.strictEqual(lds.advance(), false);
  89. },
  90. "should return false if we hit our limit": function noMoar(){
  91. var lds = new SkipDocumentSource();
  92. lds.skip = 3;
  93. var i = 0;
  94. lds.pSource = {
  95. getCurrent:function(){return { item:i };},
  96. eof: function(){return i>=5;},
  97. advance: function(){i++; return i<5;}
  98. };
  99. assert.strictEqual(lds.advance(), true);
  100. assert.strictEqual(lds.advance(), false);
  101. }
  102. },
  103. "#sourceToJson()": {
  104. "should create an object with a key $skip and the value equal to the skip": function sourceToJsonTest(){
  105. var lds = new SkipDocumentSource();
  106. lds.skip = 9;
  107. var t = {};
  108. lds.sourceToJson(t, false);
  109. assert.deepEqual(t, { "$skip": 9 });
  110. }
  111. },
  112. "#createFromJson()": {
  113. "should return a new SkipDocumentSource object from an input number": function createTest(){
  114. var t = SkipDocumentSource.createFromJson(5);
  115. assert.strictEqual(t.constructor, SkipDocumentSource);
  116. assert.strictEqual(t.skip, 5);
  117. }
  118. }
  119. }
  120. };
  121. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);