LimitDocumentSource.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. var assert = require("assert"),
  3. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  4. LimitDocumentSource = require("../../../../lib/pipeline/documentSources/LimitDocumentSource");
  5. module.exports = {
  6. "LimitDocumentSource": {
  7. "constructor()": {
  8. "should not throw Error when constructing without args": function testConstructor(){
  9. assert.doesNotThrow(function(){
  10. new LimitDocumentSource();
  11. });
  12. }
  13. },
  14. "#getDependencies": {
  15. "limits do not create dependencies": function() {
  16. var lds = LimitDocumentSource.createFromJson(1),
  17. deps = {};
  18. assert.equal(DocumentSource.GetDepsReturn.SEE_NEXT, lds.getDependencies(deps));
  19. assert.equal(0, Object.keys(deps).length);
  20. }
  21. },
  22. "#getSourceName()": {
  23. "should return the correct source name; $limit": function testSourceName(){
  24. var lds = new LimitDocumentSource();
  25. assert.strictEqual(lds.getSourceName(), "$limit");
  26. }
  27. },
  28. "#getFactory()": {
  29. "should return the constructor for this class": function factoryIsConstructor(){
  30. assert.strictEqual(new LimitDocumentSource().getFactory(), LimitDocumentSource);
  31. }
  32. },
  33. "#coalesce()": {
  34. "should return false if nextSource is not $limit": function dontSkip(){
  35. var lds = new LimitDocumentSource();
  36. assert.equal(lds.coalesce({}), false);
  37. },
  38. "should return true if nextSource is $limit": function changeLimit(){
  39. var lds = new LimitDocumentSource();
  40. assert.equal(lds.coalesce(new LimitDocumentSource()), true);
  41. }
  42. },
  43. "#getNext()": {
  44. "should throw an error if no callback is given": function() {
  45. var lds = new LimitDocumentSource();
  46. assert.throws(lds.getNext.bind(lds));
  47. },
  48. "should return the current document source": function currSource(next){
  49. var lds = new LimitDocumentSource();
  50. lds.limit = 1;
  51. lds.source = {getNext:function(cb){cb(null,{ item:1 });}};
  52. lds.getNext(function(err,val) {
  53. assert.deepEqual(val, { item:1 });
  54. next();
  55. });
  56. },
  57. "should return EOF for no sources remaining": function noMoar(next){
  58. var lds = new LimitDocumentSource();
  59. lds.limit = 10;
  60. lds.source = {
  61. calls: 0,
  62. getNext:function(cb) {
  63. if (lds.source.calls)
  64. return cb(null,DocumentSource.EOF);
  65. lds.source.calls++;
  66. return cb(null,{item:1});
  67. },
  68. dispose:function() { return true; }
  69. };
  70. lds.getNext(function(){});
  71. lds.getNext(function(err,val) {
  72. assert.strictEqual(val, DocumentSource.EOF);
  73. next();
  74. });
  75. },
  76. "should return EOF if we hit our limit": function noMoar(next){
  77. var lds = new LimitDocumentSource();
  78. lds.limit = 1;
  79. lds.source = {
  80. calls: 0,
  81. getNext:function(cb) {
  82. if (lds.source.calls)
  83. return cb(null,DocumentSource.EOF);
  84. return cb(null,{item:1});
  85. },
  86. dispose:function() { return true; }
  87. };
  88. lds.getNext(function(){});
  89. lds.getNext(function (err,val) {
  90. assert.strictEqual(val, DocumentSource.EOF);
  91. next();
  92. });
  93. }
  94. },
  95. "#serialize()": {
  96. "should create an object with a key $limit and the value equal to the limit": function sourceToJsonTest(){
  97. var lds = new LimitDocumentSource();
  98. lds.limit = 9;
  99. var actual = lds.serialize(false);
  100. assert.deepEqual(actual, { "$limit": 9 });
  101. }
  102. },
  103. "#createFromJson()": {
  104. "should return a new LimitDocumentSource object from an input number": function createTest(){
  105. var t = LimitDocumentSource.createFromJson(5);
  106. assert.strictEqual(t.constructor, LimitDocumentSource);
  107. assert.strictEqual(t.limit, 5);
  108. }
  109. }
  110. }
  111. };
  112. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);