munge.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. var assert = require("assert"),
  2. munge = require("../../");
  3. module.exports = {
  4. "munge": {
  5. "should be able to use an empty pipeline (no-op)": function(){
  6. var i = [1, 2, 3],
  7. p = [],
  8. e = [1, 2, 3],
  9. munger = munge(p),
  10. a = munger(i);
  11. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  12. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  13. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  14. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  15. },
  16. "should be able to use a $limit operator": function(){
  17. var i = [{_id:0}, {_id:1}, {_id:2}, {_id:3}, {_id:4}, {_id:5}],
  18. p = [{$limit:2}],
  19. e = [{_id:0}, {_id:1}],
  20. munger = munge(p),
  21. a = munger(i);
  22. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  23. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  24. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  25. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  26. },
  27. "should be able to use a $match operator": function(){
  28. var i = [{_id:0, e:1}, {_id:1, e:0}, {_id:2, e:1}, {_id:3, e:0}, {_id:4, e:1}, {_id:5, e:0}],
  29. p = [{$match:{e:1}}],
  30. e = [{_id:0, e:1}, {_id:2, e:1}, {_id:4, e:1}],
  31. munger = munge(p),
  32. a = munger(i);
  33. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  34. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  35. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  36. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  37. },
  38. "should be able to use a $skip operator": function(){
  39. var i = [{_id:0}, {_id:1}, {_id:2}, {_id:3}, {_id:4}, {_id:5}],
  40. p = [{$skip:2}, {$skip:1}], //testing w/ 2 ensures independent state variables
  41. e = [{_id:3}, {_id:4}, {_id:5}],
  42. munger = munge(p),
  43. a = munger(i);
  44. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  45. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  46. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  47. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  48. },
  49. "should be able to use a $skip and then a $limit operator together in the same pipeline": function(){
  50. var i = [{_id:0, e:1}, {_id:1, e:0}, {_id:2, e:1}, {_id:3, e:0}, {_id:4, e:1}, {_id:5, e:0}],
  51. p = [{$skip:2}, {$limit:1}],
  52. e = [{_id:2, e:1}],
  53. munger = munge(p),
  54. a = munger(i);
  55. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  56. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  57. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  58. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  59. },
  60. "should be able to construct an instance with $unwind operators properly": function(){
  61. var i = [
  62. {_id:0, nodes:[
  63. {one:[11], two:[2,2]},
  64. {one:[1,1], two:[22]}
  65. ]},
  66. {_id:1, nodes:[
  67. {two:[22], three:[333]},
  68. {one:[1], three:[3,3,3]}
  69. ]}
  70. ],
  71. p = [{$unwind:"$nodes"}, {$unwind:"$nodes.two"}],
  72. e = [
  73. {_id:0,nodes:{one:[11],two:2}},
  74. {_id:0,nodes:{one:[11],two:2}},
  75. {_id:0,nodes:{one:[1,1],two:22}},
  76. {_id:1,nodes:{two:22,three:[333]}}
  77. ],
  78. munger = munge(p),
  79. a = munger(i);
  80. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  81. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  82. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  83. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  84. },
  85. "should be able to use a $project operator": function(){
  86. var i = [{_id:0, e:1, f:23}, {_id:2, e:2, g:34}, {_id:4, e:3}],
  87. p = [{$project:{
  88. e:1,
  89. a:{$add:["$e", "$e"]},
  90. b:{$cond:[{$eq:["$e", 2]}, "two", "not two"]}
  91. //TODO: high level test of all other expression operators
  92. }}],
  93. e = [{_id:0, e:1, b:"not two", a:2}, {_id:2, e:2, b:"two", a:4}, {_id:4, e:3, b:"not two", a:6}],
  94. munger = munge(p),
  95. a = munger(i);
  96. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  97. assert.deepEqual(munger(i), e, "Reuse of munger should yield the same results!");
  98. assert.deepEqual(munge(p, i), e, "Alternate use of munge should yield the same results!");
  99. },
  100. "should be able to use a $project operator to exclude the _id field": function(){
  101. var i = [{_id:0, e:1, f:23}, {_id:2, e:2, g:34}, {_id:4, e:3}],
  102. p = [{$project:{
  103. _id:0,
  104. e:1
  105. //TODO: high level test of all other expression operators
  106. }}],
  107. e = [{e:1}, {e:2}, {e:3}],
  108. munger = munge(p),
  109. a = munger(i);
  110. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  111. assert.deepEqual(munger(i), e, "Reuse of munger should yield the same results!");
  112. assert.deepEqual(munge(p, i), e, "Alternate use of munge should yield the same results!");
  113. },
  114. "should be able to construct an instance with $sort operators properly (ascending)": function(){
  115. var i = [
  116. {_id:3.14159}, {_id:-273.15},
  117. {_id:42}, {_id:11}, {_id:1},
  118. {_id:null}, {_id:NaN}
  119. ],
  120. p = [{$sort:{_id:1}}],
  121. e = [
  122. {_id:null}, {_id:NaN},
  123. {_id:-273.15}, {_id:1}, {_id:3.14159}, {_id:11}, {_id:42}
  124. ],
  125. munger = munge(p),
  126. a = munger(i);
  127. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  128. //assert.deepEqual(a, e); //does not work with NaN
  129. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  130. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  131. },
  132. "should be able to construct an instance with $group operators properly": function(){
  133. var i = [
  134. {_id:0, a:1},
  135. {_id:0, a:2},
  136. {_id:0, a:3},
  137. {_id:0, a:4},
  138. {_id:0, a:1.5},
  139. {_id:0, a:null},
  140. {_id:1, b:"a"},
  141. {_id:1, b:"b"},
  142. {_id:1, b:"b"},
  143. {_id:1, b:"c"}
  144. ],
  145. p = [{$group:{
  146. _id:"$_id",
  147. sum_a:{$sum:"$a"},
  148. //min_a:{$min:"$a"}, //this is busted in this version of mongo
  149. max_a:{$max:"$a"},
  150. avg_a:{$avg:"$a"},
  151. first_b:{$first:"$b"},
  152. last_b:{$last:"$b"},
  153. addToSet_b:{$addToSet:"$b"},
  154. push_b:{$push:"$b"}
  155. }}],
  156. e = [
  157. {
  158. _id:0,
  159. sum_a:11.5,
  160. //min_a:1,
  161. max_a:4,
  162. avg_a:2.3,
  163. first_b:undefined,
  164. last_b:undefined,
  165. addToSet_b:[],
  166. push_b:[]
  167. },
  168. {
  169. _id:1,
  170. sum_a:0,
  171. //min_a:null,
  172. max_a:undefined,
  173. avg_a:0,
  174. first_b:"a",
  175. last_b:"c",
  176. addToSet_b:["a", "b", "c"],
  177. push_b:["a", "b", "b", "c"]
  178. }
  179. ],
  180. munger = munge(p),
  181. a = munger(i);
  182. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  183. //assert.deepEqual(a, e); //does not work with NaN
  184. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  185. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  186. },
  187. "should be able to use a $split operator": function(){
  188. var i = [{_id:0, a:1}, {_id:1, a:1}, {_id:2, a:1}, {_id:3, a:1}, {_id:4, a:1}, {_id:5, a:1}],
  189. p = [{$match:{_id:0}}, {$split:{aX2:[{$project:{a:{$multiply:["$a", 2]}}}], aX3:[{$project:{a:{$multiply:["$a", 3]}}}]}}, {$unwind:"$aX2"}, {$unwind:"$aX3"}],
  190. //e = [{aX2:[{_id:0, a:2}], aX3:[{_id:0, a:3}]}],
  191. e = [{aX2:{_id:0, a:2}, aX3:{_id:0, a:3}}],
  192. munger = munge(p),
  193. a = munger(i);
  194. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  195. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  196. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  197. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  198. },
  199. }
  200. };
  201. if(!module.parent) (new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();