OutDocumentSource.js 2.5 KB

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