LimitDocumentSource.js 4.2 KB

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