SkipDocumentSource.js 3.8 KB

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