CursorDocumentSource.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var assert = require("assert"),
  3. async = require("async"),
  4. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  5. Cursor = require("../../../../lib/Cursor");
  6. module.exports = {
  7. "CursorDocumentSource": {
  8. "constructor(data)": {
  9. "should fail if CursorWithContext is not provided": function(){
  10. assert.throws(function(){
  11. var cds = new CursorDocumentSource();
  12. });
  13. },
  14. "should get a accept a CursorWithContext and set it internally": function(){
  15. var cwc = new CursorDocumentSource.CursorWithContext();
  16. cwc._cursor = new Cursor( [] );
  17. var cds = new CursorDocumentSource(cwc);
  18. assert.ok(cds._cursorWithContext);
  19. }
  20. },
  21. "#getNext": {
  22. "should return the current cursor value sync": function(){
  23. var cwc = new CursorDocumentSource.CursorWithContext();
  24. cwc._cursor = new Cursor( [1,2,3,4] );
  25. var cds = new CursorDocumentSource(cwc);
  26. assert.equal(cds.getNext(), 1);
  27. assert.equal(cds.getNext(), 2);
  28. assert.equal(cds.getNext(), 3);
  29. assert.equal(cds.getNext(), 4);
  30. assert.equal(cds.getNext(), undefined);
  31. },
  32. "should return the current cursor value async": function(next){
  33. var cwc = new CursorDocumentSource.CursorWithContext();
  34. cwc._cursor = new Cursor( [1,2,3,4] );
  35. var cds = new CursorDocumentSource(cwc);
  36. cds.getNext(function(val) {
  37. assert.equal(val, 1);
  38. cds.getNext(function(val) {
  39. assert.equal(val, 2);
  40. cds.getNext(function(val) {
  41. assert.equal(val, 3);
  42. cds.getNext(function(val) {
  43. assert.equal(val, 4);
  44. cds.getNext(function(val) {
  45. assert.equal(val, undefined);
  46. return next();
  47. });
  48. });
  49. });
  50. });
  51. });
  52. },
  53. "should return values past the batch limit": function(){
  54. var cwc = new CursorDocumentSource.CursorWithContext(),
  55. n = 0,
  56. arr = Array.apply(0, new Array(200)).map(function() { return n++; });
  57. cwc._cursor = new Cursor( arr );
  58. var cds = new CursorDocumentSource(cwc);
  59. arr.forEach(function(v) {
  60. assert.equal(cds.getNext(), v);
  61. });
  62. assert.equal(cds.getNext(), undefined);
  63. },
  64. },
  65. "#dispose": {
  66. "should empty the current cursor": function(){
  67. var cwc = new CursorDocumentSource.CursorWithContext();
  68. cwc._cursor = new Cursor( [1,2,3] );
  69. var cds = new CursorDocumentSource(cwc);
  70. assert.equal(cds.getNext(), 1);
  71. assert.equal(cds.getNext(), 2);
  72. cds.dispose();
  73. assert.equal(cds.getNext(), undefined);
  74. }
  75. }
  76. }
  77. };
  78. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();