OutDocumentSource.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. var assert = require("assert"),
  3. async = require("async"),
  4. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  5. OutDocumentSource = require("../../../../lib/pipeline/documentSources/OutDocumentSource"),
  6. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  7. Cursor = require("../../../../lib/Cursor");
  8. var createOut = function(ctx) {
  9. var ds = new OutDocumentSource(ctx);
  10. return ds;
  11. };
  12. module.exports = {
  13. "OutDocumentSource": {
  14. "constructor()":{
  15. "should not throw when constructing without args":function() {
  16. assert.doesNotThrow(function(){
  17. var ods = createOut();
  18. });
  19. }
  20. },
  21. "#getSourceName()":{
  22. "should return the correct source name; $out": function() {
  23. var ods = createOut();
  24. assert.strictEqual(ods.getSourceName(), "$out");
  25. }
  26. },
  27. "#getNext()":{
  28. "callback is required":function() {
  29. var ods = createOut();
  30. assert.throws(ods.getNext.bind(ods));
  31. },
  32. "should act ass passthrough (for now)": function(next) {
  33. var ods = OutDocumentSource.createFromJson("test"),
  34. cwc = new CursorDocumentSource.CursorWithContext(),
  35. l = [{_id:0,a:[{b:1},{b:2}]}, {_id:1,a:[{b:1},{b:1}]} ];
  36. cwc._cursor = new Cursor( l );
  37. var cds = new CursorDocumentSource(cwc);
  38. ods.setSource(cds);
  39. var docs = [], i = 0;
  40. async.doWhilst(
  41. function(cb) {
  42. ods.getNext(function(err, val) {
  43. docs[i] = val;
  44. return cb(err);
  45. });
  46. },
  47. function() {
  48. return docs[i++] !== DocumentSource.EOF;
  49. },
  50. function(err) {
  51. assert.deepEqual([{_id:0,a:[{b:1},{b:2}]}, {_id:1,a:[{b:1},{b:1}]}, DocumentSource.EOF], docs);
  52. next();
  53. }
  54. );
  55. }
  56. },
  57. "#createFromJson()":{
  58. "method creates OutDocumentSource with given title":function() {
  59. var title = "CognitiveScientists",
  60. ods = OutDocumentSource.createFromJson(title);
  61. assert.strictEqual(title, ods._collectionName);
  62. }
  63. },
  64. "#serialize()":{
  65. "serialize":function() {
  66. var cwc = new CursorDocumentSource.CursorWithContext();
  67. var input = [{_id: 0, a: 1}, {_id: 1, a: 2}];
  68. cwc._cursor = new Cursor( input );
  69. var cds = new CursorDocumentSource(cwc);
  70. var title = "CognitiveScientists";
  71. var ods = OutDocumentSource.createFromJson(title);
  72. ods.setSource(cds);
  73. var srcNm = ods.getSourceName();
  74. var serialize = {};
  75. serialize[srcNm] = title;
  76. assert.deepEqual(ods.serialize(), serialize);
  77. }
  78. }
  79. }
  80. };
  81. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);