CursorDocumentSource.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. var assert = require("assert"),
  3. async = require("async"),
  4. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  5. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  6. LimitDocumentSource = require("../../../../lib/pipeline/documentSources/LimitDocumentSource"),
  7. SkipDocumentSource = require("../../../../lib/pipeline/documentSources/SkipDocumentSource"),
  8. Cursor = require("../../../../lib/Cursor");
  9. module.exports = {
  10. "CursorDocumentSource": {
  11. "constructor(data)": {
  12. "should fail if CursorWithContext is not provided": function(){
  13. assert.throws(function(){
  14. var cds = new CursorDocumentSource();
  15. });
  16. },
  17. "should get a accept a CursorWithContext and set it internally": function(){
  18. var cwc = new CursorDocumentSource.CursorWithContext();
  19. cwc._cursor = new Cursor( [] );
  20. var cds = new CursorDocumentSource(cwc);
  21. assert.ok(cds._cursorWithContext);
  22. }
  23. },
  24. "#coalesce": {
  25. "should be able to coalesce a limit into itself": function (){
  26. var cwc = new CursorDocumentSource.CursorWithContext();
  27. cwc._cursor = new Cursor( [] );
  28. var lds = new LimitDocumentSource();
  29. lds.limit = 1;
  30. var cds = new CursorDocumentSource(cwc);
  31. assert.equal(cds.coalesce(lds) instanceof LimitDocumentSource, true);
  32. },
  33. "should leave non-limit alone": function () {
  34. var cwc = new CursorDocumentSource.CursorWithContext();
  35. cwc._cursor = new Cursor( [] );
  36. var sds = new SkipDocumentSource(),
  37. cds = new CursorDocumentSource(cwc);
  38. assert.equal(cds.coalesce(sds), false);
  39. }
  40. },
  41. "#getNext": {
  42. "should return the current cursor value sync": function(){
  43. var cwc = new CursorDocumentSource.CursorWithContext();
  44. cwc._cursor = new Cursor( [1,2,3,4] );
  45. var cds = new CursorDocumentSource(cwc);
  46. assert.equal(cds.getNext(), 1);
  47. assert.equal(cds.getNext(), 2);
  48. assert.equal(cds.getNext(), 3);
  49. assert.equal(cds.getNext(), 4);
  50. assert.equal(cds.getNext(), DocumentSource.EOF);
  51. },
  52. "should return the current cursor value async": function(next){
  53. var expected = JSON.stringify([1,2]);
  54. var cwc = new CursorDocumentSource.CursorWithContext();
  55. cwc._cursor = new Cursor( [1,2,3,4] );
  56. var cds = new CursorDocumentSource(cwc);
  57. async.series([
  58. cds.getNext.bind(cds),
  59. cds.getNext.bind(cds),
  60. cds.getNext.bind(cds),
  61. cds.getNext.bind(cds),
  62. cds.getNext.bind(cds),
  63. ],
  64. function(err,res) {
  65. assert.deepEqual([1,2,3,4,DocumentSource.EOF], res);
  66. next();
  67. }
  68. );
  69. },
  70. "should return values past the batch limit": function(){
  71. var cwc = new CursorDocumentSource.CursorWithContext(),
  72. n = 0,
  73. arr = Array.apply(0, new Array(200)).map(function() { return n++; });
  74. cwc._cursor = new Cursor( arr );
  75. var cds = new CursorDocumentSource(cwc);
  76. arr.forEach(function(v) {
  77. assert.equal(cds.getNext(), v);
  78. });
  79. assert.equal(cds.getNext(), DocumentSource.EOF);
  80. },
  81. },
  82. "#dispose": {
  83. "should empty the current cursor": function(){
  84. var cwc = new CursorDocumentSource.CursorWithContext();
  85. cwc._cursor = new Cursor( [1,2,3] );
  86. var cds = new CursorDocumentSource(cwc);
  87. assert.equal(cds.getNext(), 1);
  88. assert.equal(cds.getNext(), 2);
  89. cds.dispose();
  90. assert.equal(cds.getNext(), DocumentSource.EOF);
  91. }
  92. }
  93. }
  94. };
  95. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();