LimitDocumentSource.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "#getSourceName()": {
  15. "should return the correct source name; $limit": function testSourceName(){
  16. var lds = new LimitDocumentSource();
  17. assert.strictEqual(lds.getSourceName(), "$limit");
  18. }
  19. },
  20. "#getFactory()": {
  21. "should return the constructor for this class": function factoryIsConstructor(){
  22. assert.strictEqual(new LimitDocumentSource().getFactory(), LimitDocumentSource);
  23. }
  24. },
  25. "#coalesce()": {
  26. "should return false if nextSource is not $limit": function dontSkip(){
  27. var lds = new LimitDocumentSource();
  28. assert.equal(lds.coalesce({}), false);
  29. },
  30. "should return true if nextSource is $limit": function changeLimit(){
  31. var lds = new LimitDocumentSource();
  32. assert.equal(lds.coalesce(new LimitDocumentSource()), true);
  33. }
  34. },
  35. "#getNext()": {
  36. "should return the current document source": function currSource(next){
  37. var lds = new LimitDocumentSource();
  38. lds.limit = 1;
  39. lds.source = {getNext:function(err,cb){cb(null,{ item:1 });}};
  40. lds.getNext(function(err,val) {
  41. assert.deepEqual(val, { item:1 });
  42. next();
  43. });
  44. },
  45. "should return EOF for no sources remaining": function noMoar(next){
  46. var lds = new LimitDocumentSource();
  47. lds.limit = 10;
  48. lds.source = {
  49. calls: 0,
  50. getNext:function(err,cb) {
  51. if (lds.source.calls)
  52. return cb(null,DocumentSource.EOF);
  53. lds.source.calls++;
  54. return cb(null,{item:1});
  55. },
  56. dispose:function() { return true; }
  57. };
  58. lds.getNext(function(){});
  59. lds.getNext(function(err,val) {
  60. assert.strictEqual(val, DocumentSource.EOF);
  61. next();
  62. });
  63. },
  64. "should return false if we hit our limit": function noMoar(next){
  65. var lds = new LimitDocumentSource();
  66. lds.limit = 1;
  67. lds.source = {
  68. calls: 0,
  69. getNext:function(err,cb) {
  70. if (lds.source.calls)
  71. return cb(null,DocumentSource.EOF);
  72. return cb(null,{item:1});
  73. },
  74. dispose:function() { return true; }
  75. };
  76. lds.getNext(function(){});
  77. lds.getNext(function (err,val) {
  78. assert.strictEqual(val, DocumentSource.EOF);
  79. next();
  80. });
  81. }
  82. },
  83. "#serialize()": {
  84. "should create an object with a key $limit and the value equal to the limit": function sourceToJsonTest(){
  85. var lds = new LimitDocumentSource();
  86. lds.limit = 9;
  87. var actual = lds.serialize(false);
  88. assert.deepEqual(actual, { "$limit": 9 });
  89. }
  90. },
  91. "#createFromJson()": {
  92. "should return a new LimitDocumentSource object from an input number": function createTest(){
  93. var t = LimitDocumentSource.createFromJson(5);
  94. assert.strictEqual(t.constructor, LimitDocumentSource);
  95. assert.strictEqual(t.limit, 5);
  96. }
  97. }
  98. }
  99. };
  100. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);