SplitDocumentSource.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. var assert = require("assert"),
  3. SplitDocumentSource = require("../../../../lib/pipeline/documentSources/SplitDocumentSource"),
  4. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  5. Cursor = require("../../../../lib/Cursor");
  6. module.exports = {
  7. "SplitDocumentSource": {
  8. "constructor()": {
  9. "should not throw Error when constructing without args": function testConstructor(){
  10. assert.doesNotThrow(function(){
  11. new SplitDocumentSource();
  12. });
  13. },
  14. "should throw an error if called with arguments.length > 0": function throwsWithArgs(){
  15. assert.throws(function(){
  16. new SplitDocumentSource(1);
  17. });
  18. }
  19. },
  20. "#getSourceName()": {
  21. "should return the correct source name; $split": function testSourceName(){
  22. var pds = new SplitDocumentSource();
  23. assert.strictEqual(pds.getSourceName(), SplitDocumentSource.splitName);
  24. }
  25. },
  26. "#eof()": {
  27. "shouldn't be eof after init": function testEOF(){
  28. var cwc = new CursorDocumentSource.CursorWithContext();
  29. cwc._cursor = new Cursor( [{a: 1}] );
  30. var cds = new CursorDocumentSource(cwc);
  31. var split = new SplitDocumentSource();
  32. split.setSource(cds);
  33. assert.ok(!split.eof());
  34. },
  35. "should be eof after one call to get current": function testAdvanceFirst() {
  36. var cwc = new CursorDocumentSource.CursorWithContext();
  37. var input = [{_id: 0, a: 1}, {_id: 1, a: 2}];
  38. cwc._cursor = new Cursor( input );
  39. var cds = new CursorDocumentSource(cwc);
  40. var split = new SplitDocumentSource();
  41. split.setSource(cds);
  42. assert.ok(split.getCurrent());
  43. assert.ok(split.eof);
  44. }
  45. },
  46. "#advance()": {
  47. "can't advance after one call to getCurrent": function testAdvanceFirst() {
  48. var cwc = new CursorDocumentSource.CursorWithContext();
  49. var input = [{_id: 0, a: 1}, {_id: 1, a: 2}];
  50. cwc._cursor = new Cursor( input );
  51. var cds = new CursorDocumentSource(cwc);
  52. var split = new SplitDocumentSource();
  53. split.setSource(cds);
  54. assert.ok(split.getCurrent());
  55. assert.ok(!split.advance());
  56. },
  57. "throws exception if advanced beyond eof": function throwsBeyondEof() {
  58. assert.throws(function() {
  59. var cwc = new CursorDocumentSource.CursorWithContext();
  60. var input = [{_id: 0, a: 1}, {_id: 1, a: 2}];
  61. cwc._cursor = new Cursor( input );
  62. var cds = new CursorDocumentSource(cwc);
  63. var split = new SplitDocumentSource();
  64. split.setSource(cds);
  65. split.getCurrent();
  66. split.advance();
  67. split.advance();
  68. });
  69. }
  70. },
  71. "#populate()": function testPopulate() {
  72. var spec = {
  73. aX2:[{$project:{a:{$multiply:["$a", 2]}}}],
  74. aX3:[{$project:{a:{$multiply:["$a", 3]}}}]
  75. };
  76. var cwc = new CursorDocumentSource.CursorWithContext();
  77. var input = [{a:1}, {a:2}];
  78. cwc._cursor = new Cursor( input );
  79. var cds = new CursorDocumentSource(cwc);
  80. var split = SplitDocumentSource.createFromJson(spec);
  81. split.setSource(cds);
  82. assert.ok(!split.eof());
  83. assert.deepEqual({aX2:[{a:2}, {a:4}], aX3:[{a:3}, {a:6}]}, split.getCurrent());
  84. /*
  85. assert.ok(!split.getCurrent().b);
  86. assert.ok(split.advance());
  87. assert.ok(!split.eof());
  88. assert.equal(3, split.getCurret().a);
  89. assert.ok(!split.getCurrent().b);
  90. assert.ok(!split.advance());
  91. assertExhausted(split);
  92. */
  93. },
  94. "#createFromJson()": {
  95. "should error if called with non-object": function testNonObjectPassed() {
  96. //String as arg
  97. assert.throws(function() {
  98. var split = SplitDocumentSource.createFromJson("not an object");
  99. });
  100. //Date as arg
  101. assert.throws(function() {
  102. var split = SplitDocumentSource.createFromJson(new Date());
  103. });
  104. //Array as arg
  105. assert.throws(function() {
  106. var split = SplitDocumentSource.createFromJson([]);
  107. });
  108. //Empty args
  109. assert.throws(function() {
  110. var split = SplitDocumentSource.createFromJson();
  111. });
  112. },
  113. "should error if spec has no keys": function testNoKeys() {
  114. assert.throws(function() {
  115. var split = SplitDocumentSource.createFromJson({});
  116. });
  117. },
  118. "should error if value of a key in top level is not an array": function testNoKeys() {
  119. assert.throws(function() {
  120. var split = SplitDocumentSource.createFromJson({a: "not an array"});
  121. });
  122. }
  123. },
  124. }
  125. };
  126. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);