LimitDocumentSource_test.js 4.0 KB

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