MatchDocumentSource.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. "use strict";
  2. var assert = require("assert"),
  3. async = require("async"),
  4. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  5. MatchDocumentSource = require("../../../../lib/pipeline/documentSources/MatchDocumentSource");
  6. module.exports = {
  7. "MatchDocumentSource": {
  8. "constructor()": {
  9. "should throw Error when constructing without args": function testConstructor(){
  10. assert.throws(function(){
  11. new MatchDocumentSource();
  12. });
  13. }
  14. },
  15. "#getSourceName()": {
  16. "should return the correct source name; $match": function testSourceName(){
  17. var mds = new MatchDocumentSource({ packet :{ $exists : false } });
  18. assert.strictEqual(mds.getSourceName(), "$match");
  19. }
  20. },
  21. "#serialize()": {
  22. "should append the match query to the input builder": function sourceToJsonTest(){
  23. var mds = new MatchDocumentSource({ location : { $in : ['Kentucky'] } });
  24. var t = mds.serialize(false);
  25. assert.deepEqual(t, { "$match" : { location : { $in : ['Kentucky'] } }});
  26. }
  27. },
  28. "#createFromJson()": {
  29. "should return a new MatchDocumentSource object from an input object": function createTest(){
  30. var t = MatchDocumentSource.createFromJson({ someval:{$exists:true} });
  31. assert.strictEqual(t instanceof MatchDocumentSource, true);
  32. }
  33. },
  34. "#getNext()": {
  35. "should throw an error if no callback is given": function() {
  36. var mds = new MatchDocumentSource({item:1});
  37. assert.throws(mds.getNext.bind(mds));
  38. },
  39. "should return the current document source": function currSource(next){
  40. var mds = new MatchDocumentSource({item: 1});
  41. mds.source = {getNext:function(cb){cb(null,{ item:1 });}};
  42. mds.getNext(function(err,val) {
  43. assert.deepEqual(val, { item:1 });
  44. next();
  45. });
  46. },
  47. "should return matched sources remaining": function (next){
  48. var mds = new MatchDocumentSource({ item: {$lt: 5} }),
  49. items = [ 1,2,3,4,5,6,7,8,9 ];
  50. mds.source = {
  51. calls: 0,
  52. getNext:function(cb) {
  53. if (this.calls >= items.length)
  54. return cb(null,DocumentSource.EOF);
  55. return cb(null,{item: items[this.calls++]});
  56. },
  57. dispose:function() { return true; }
  58. };
  59. async.series([
  60. mds.getNext.bind(mds),
  61. mds.getNext.bind(mds),
  62. mds.getNext.bind(mds),
  63. mds.getNext.bind(mds),
  64. mds.getNext.bind(mds),
  65. ],
  66. function(err,res) {
  67. assert.deepEqual([{item:1},{item:2},{item:3},{item:4},DocumentSource.EOF], res);
  68. next();
  69. }
  70. );
  71. },
  72. "should not return matched out documents for sources remaining": function (next){
  73. var mds = new MatchDocumentSource({ item: {$gt: 5} }),
  74. items = [ 1,2,3,4,5,6,7,8,9 ];
  75. mds.source = {
  76. calls: 0,
  77. getNext:function(cb) {
  78. if (this.calls >= items.length)
  79. return cb(null,DocumentSource.EOF);
  80. return cb(null,{item: items[this.calls++]});
  81. },
  82. dispose:function() { return true; }
  83. };
  84. async.series([
  85. mds.getNext.bind(mds),
  86. mds.getNext.bind(mds),
  87. mds.getNext.bind(mds),
  88. mds.getNext.bind(mds),
  89. mds.getNext.bind(mds),
  90. ],
  91. function(err,res) {
  92. assert.deepEqual([{item:6},{item:7},{item:8},{item:9},DocumentSource.EOF], res);
  93. next();
  94. }
  95. );
  96. },
  97. "should return EOF for no sources remaining": function (next){
  98. var mds = new MatchDocumentSource({ item: {$gt: 5} }),
  99. items = [ ];
  100. mds.source = {
  101. calls: 0,
  102. getNext:function(cb) {
  103. if (this.calls >= items.length)
  104. return cb(null,DocumentSource.EOF);
  105. return cb(null,{item: items[this.calls++]});
  106. },
  107. dispose:function() { return true; }
  108. };
  109. async.series([
  110. mds.getNext.bind(mds),
  111. ],
  112. function(err,res) {
  113. assert.deepEqual([DocumentSource.EOF], res);
  114. next();
  115. }
  116. );
  117. },
  118. },
  119. "#coalesce()": {
  120. "should return false if nextSource is not $match": function dontSkip(){
  121. var mds = new MatchDocumentSource({item: {$lt:3}});
  122. assert.equal(mds.coalesce({}), false);
  123. },
  124. "should return true if nextSource is $limit": function changeLimit(){
  125. var mds = new MatchDocumentSource({item:{$gt:1}}),
  126. mds2 = new MatchDocumentSource({item:{$lt:3}}),
  127. expected = {$and: [{item:{$gt:1}}, {item:{$lt:3}}]};
  128. var actual = mds.coalesce(mds2);
  129. assert.equal(actual, true);
  130. assert.deepEqual(mds.getQuery(), expected);
  131. }
  132. },
  133. "#getQuery()": {
  134. "should return current query": function () {
  135. var mds = new MatchDocumentSource({item: {$gt:1}});
  136. var actual = mds.getQuery();
  137. assert.deepEqual(actual, {item:{$gt:1}});
  138. }
  139. },
  140. "#redactSafePortion()": {
  141. "should throw unimplemented, for now": function() {
  142. var mds = new MatchDocumentSource({$gt:1});
  143. assert.throws(mds.redactSafePortion);
  144. }
  145. }
  146. }
  147. };
  148. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);