RedactDocumentSource.js 2.9 KB

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