LimitDocumentSource.js 4.0 KB

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