LimitDocumentSource.js 3.8 KB

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