LimitDocumentSource.js 3.9 KB

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