RedactDocumentSource.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. var assert = require("assert"),
  3. async = require("async"),
  4. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  5. RedactDocumentSource = require("../../../../lib/pipeline/documentSources/RedactDocumentSource"),
  6. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  7. Cursor = require("../../../../lib/Cursor");
  8. var exampleRedact = {$cond: [
  9. {$gt:[3, 0]},
  10. "$$DESCEND",
  11. "$$PRUNE"]
  12. };
  13. ////////////////////////////////////////////////////////////////////////////////
  14. ////////////////////////////////////////////////////////////////////////////////
  15. //////////////////////////////////// BUSTED ////////////////////////////////////
  16. // This DocumentSource is busted without new Expressions //
  17. ////////////////////////////////////////////////////////////////////////////////
  18. ////////////////////////////////////////////////////////////////////////////////
  19. ////////////////////////////////////////////////////////////////////////////////
  20. //TESTS
  21. module.exports = {
  22. "RedactDocumentSource": {
  23. "constructor()": {
  24. "should not throw Error when constructing without args": function testConstructor() {
  25. assert.doesNotThrow(function() {
  26. new RedactDocumentSource();
  27. });
  28. }
  29. },
  30. "#getSourceName()": {
  31. "should return the correct source name; $redact": function testSourceName() {
  32. var rds = new RedactDocumentSource();
  33. assert.strictEqual(rds.getSourceName(), "$redact");
  34. }
  35. },
  36. "#getNext()": {
  37. "should return EOF": function testEOF(next) {
  38. var rds = RedactDocumentSource.createFromJson(exampleRedact);
  39. rds.setSource({
  40. getNext: function getNext(cb) {
  41. return cb(null, DocumentSource.EOF);
  42. }
  43. });
  44. rds.getNext(function(err, doc) {
  45. assert.equal(DocumentSource.EOF, doc);
  46. next();
  47. });
  48. },
  49. "iterator state accessors consistently report the source is exhausted": function assertExhausted() {
  50. var cwc = new CursorDocumentSource.CursorWithContext();
  51. var input = [{}];
  52. cwc._cursor = new Cursor( input );
  53. var cds = new CursorDocumentSource(cwc);
  54. var rds = RedactDocumentSource.createFromJson(exampleRedact);
  55. rds.setSource(cds);
  56. rds.getNext(function(err, actual) {
  57. rds.getNext(function(err, actual1) {
  58. assert.equal(DocumentSource.EOF, actual1);
  59. rds.getNext(function(err, actual2) {
  60. assert.equal(DocumentSource.EOF, actual2);
  61. rds.getNext(function(err, actual3) {
  62. assert.equal(DocumentSource.EOF, actual3);
  63. });
  64. });
  65. });
  66. });
  67. },
  68. "callback is required": function requireCallback() {
  69. var rds = new RedactDocumentSource();
  70. assert.throws(rds.getNext.bind(rds));
  71. },
  72. },
  73. "#optimize()": {
  74. "Optimize the expression": function optimizeProject() {
  75. var rds = RedactDocumentSource.createFromJson(exampleRedact);
  76. assert.doesNotThrow(rds.optimize.bind(rds));
  77. }
  78. },
  79. "#createFromJson()": {
  80. "should error if called with non-object": function testNonObjectPassed() {
  81. //Empty args
  82. assert.throws(function() {
  83. var rds = RedactDocumentSource.createFromJson();
  84. });
  85. //Invalid spec
  86. assert.throws(function() {
  87. var rds = RedactDocumentSource.createFromJson({$invalidOperator: 1});
  88. });
  89. }
  90. },
  91. }
  92. };
  93. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);