CursorDocumentSource.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 cwc = new CursorDocumentSource.CursorWithContext();
  54. cwc._cursor = new Cursor( [1,2,3,4] );
  55. var cds = new CursorDocumentSource(cwc);
  56. cds.getNext(function(val) {
  57. assert.equal(val, 1);
  58. cds.getNext(function(val) {
  59. assert.equal(val, 2);
  60. cds.getNext(function(val) {
  61. assert.equal(val, 3);
  62. cds.getNext(function(val) {
  63. assert.equal(val, 4);
  64. cds.getNext(function(val) {
  65. assert.equal(val, DocumentSource.EOF);
  66. return next();
  67. });
  68. });
  69. });
  70. });
  71. });
  72. },
  73. "should return values past the batch limit": function(){
  74. var cwc = new CursorDocumentSource.CursorWithContext(),
  75. n = 0,
  76. arr = Array.apply(0, new Array(200)).map(function() { return n++; });
  77. cwc._cursor = new Cursor( arr );
  78. var cds = new CursorDocumentSource(cwc);
  79. arr.forEach(function(v) {
  80. assert.equal(cds.getNext(), v);
  81. });
  82. assert.equal(cds.getNext(), DocumentSource.EOF);
  83. },
  84. },
  85. "#dispose": {
  86. "should empty the current cursor": function(){
  87. var cwc = new CursorDocumentSource.CursorWithContext();
  88. cwc._cursor = new Cursor( [1,2,3] );
  89. var cds = new CursorDocumentSource(cwc);
  90. assert.equal(cds.getNext(), 1);
  91. assert.equal(cds.getNext(), 2);
  92. cds.dispose();
  93. assert.equal(cds.getNext(), DocumentSource.EOF);
  94. }
  95. }
  96. }
  97. };
  98. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();