SkipDocumentSource.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. var assert = require("assert"),
  3. async = require("async"),
  4. Cursor = require("../../../../lib/Cursor"),
  5. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  6. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  7. SkipDocumentSource = require("../../../../lib/pipeline/documentSources/SkipDocumentSource");
  8. module.exports = {
  9. "SkipDocumentSource": {
  10. "constructor()": {
  11. "should not throw Error when constructing without args": function testConstructor(){
  12. assert.doesNotThrow(function(){
  13. new SkipDocumentSource();
  14. });
  15. }
  16. },
  17. "#getSourceName()": {
  18. "should return the correct source name; $skip": function testSourceName(){
  19. var sds = new SkipDocumentSource();
  20. assert.strictEqual(sds.getSourceName(), "$skip");
  21. }
  22. },
  23. "#coalesce()": {
  24. "should return false if nextSource is not $skip": function dontSkip(){
  25. var sds = new SkipDocumentSource();
  26. assert.equal(sds.coalesce({}), false);
  27. },
  28. "should return true if nextSource is $skip": function changeSkip(){
  29. var sds = new SkipDocumentSource();
  30. assert.equal(sds.coalesce(new SkipDocumentSource()), true);
  31. }
  32. },
  33. "#getNext()": {
  34. "should throw an error if no callback is given": function() {
  35. var sds = new SkipDocumentSource();
  36. assert.throws(sds.getNext.bind(sds));
  37. },
  38. "should return EOF if there are no more sources": function noSources(next){
  39. var sds = new SkipDocumentSource();
  40. sds.skip = 3;
  41. sds.count = 0;
  42. var expected = [
  43. {val:4},
  44. DocumentSource.EOF
  45. ];
  46. var cwc = new CursorDocumentSource.CursorWithContext();
  47. var input = [
  48. {val:1},
  49. {val:2},
  50. {val:3},
  51. {val:4},
  52. ];
  53. cwc._cursor = new Cursor( input );
  54. var cds = new CursorDocumentSource(cwc);
  55. sds.setSource(cds);
  56. async.series([
  57. sds.getNext.bind(sds),
  58. sds.getNext.bind(sds),
  59. ],
  60. function(err,res) {
  61. assert.deepEqual(expected, res);
  62. next();
  63. }
  64. );
  65. sds.getNext(function(err, actual) {
  66. assert.equal(actual, DocumentSource.EOF);
  67. });
  68. },
  69. "should return documents if skip count is not hit and there are more documents": function hitSkip(next){
  70. var sds = SkipDocumentSource.createFromJson(1);
  71. var cwc = new CursorDocumentSource.CursorWithContext();
  72. var input = [{val:1},{val:2},{val:3}];
  73. cwc._cursor = new Cursor( input );
  74. var cds = new CursorDocumentSource(cwc);
  75. sds.setSource(cds);
  76. sds.getNext(function(err,actual) {
  77. assert.notEqual(actual, DocumentSource.EOF);
  78. assert.deepEqual(actual, {val:2});
  79. next();
  80. });
  81. },
  82. "should return the current document source": function currSource(){
  83. var sds = SkipDocumentSource.createFromJson(1);
  84. var cwc = new CursorDocumentSource.CursorWithContext();
  85. var input = [{val:1},{val:2},{val:3}];
  86. cwc._cursor = new Cursor( input );
  87. var cds = new CursorDocumentSource(cwc);
  88. sds.setSource(cds);
  89. sds.getNext(function(err, actual) {
  90. assert.deepEqual(actual, { val:2 });
  91. });
  92. },
  93. "should return false if we hit our limit": function noMoar(next){
  94. var sds = new SkipDocumentSource();
  95. sds.skip = 3;
  96. var expected = [
  97. {item:4},
  98. DocumentSource.EOF
  99. ];
  100. var i = 1;
  101. sds.source = {
  102. getNext:function(cb){
  103. if (i>=5)
  104. return cb(null,DocumentSource.EOF);
  105. return cb(null, { item:i++ });
  106. }
  107. };
  108. async.series([
  109. sds.getNext.bind(sds),
  110. sds.getNext.bind(sds),
  111. ],
  112. function(err,res) {
  113. assert.deepEqual(expected, res);
  114. next();
  115. }
  116. );
  117. }
  118. },
  119. "#serialize()": {
  120. "should create an object with a key $skip and the value equal to the skip": function sourceToJsonTest(){
  121. var sds = new SkipDocumentSource();
  122. sds.skip = 9;
  123. var t = sds.serialize(false);
  124. assert.deepEqual(t, { "$skip": 9 });
  125. }
  126. },
  127. "#createFromJson()": {
  128. "should return a new SkipDocumentSource object from an input number": function createTest(){
  129. var t = SkipDocumentSource.createFromJson(5);
  130. assert.strictEqual(t.constructor, SkipDocumentSource);
  131. assert.strictEqual(t.skip, 5);
  132. }
  133. }
  134. }
  135. };
  136. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);