munge.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 construct an instance with $sort operators properly (ascending)": function(){
  101. var i = [
  102. {_id:3.14159}, {_id:-273.15},
  103. {_id:42}, {_id:11}, {_id:1},
  104. {_id:null}, {_id:NaN}
  105. ],
  106. p = [{$sort:{_id:1}}],
  107. e = [
  108. {_id:null}, {_id:NaN},
  109. {_id:-273.15}, {_id:1}, {_id:3.14159}, {_id:11}, {_id:42}
  110. ],
  111. munger = munge(p),
  112. a = munger(i);
  113. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  114. //assert.deepEqual(a, e); //does not work with NaN
  115. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  116. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  117. },
  118. "should be able to construct an instance with $group operators properly": function(){
  119. var i = [
  120. {_id:0, a:1},
  121. {_id:0, a:2},
  122. {_id:0, a:3},
  123. {_id:0, a:4},
  124. {_id:0, a:1.5},
  125. {_id:0, a:null},
  126. {_id:1, b:"a"},
  127. {_id:1, b:"b"},
  128. {_id:1, b:"b"},
  129. {_id:1, b:"c"}
  130. ],
  131. p = [{$group:{
  132. _id:"$_id",
  133. sum_a:{$sum:"$a"},
  134. //min_a:{$min:"$a"}, //this is busted in this version of mongo
  135. max_a:{$max:"$a"},
  136. avg_a:{$avg:"$a"},
  137. first_b:{$first:"$b"},
  138. last_b:{$last:"$b"},
  139. addToSet_b:{$addToSet:"$b"},
  140. push_b:{$push:"$b"}
  141. }}],
  142. e = [
  143. {
  144. _id:0,
  145. sum_a:11.5,
  146. //min_a:1,
  147. max_a:4,
  148. avg_a:2.3,
  149. first_b:undefined,
  150. last_b:undefined,
  151. addToSet_b:[],
  152. push_b:[]
  153. },
  154. {
  155. _id:1,
  156. sum_a:0,
  157. //min_a:null,
  158. max_a:undefined,
  159. avg_a:0,
  160. first_b:"a",
  161. last_b:"c",
  162. addToSet_b:["a", "b", "c"],
  163. push_b:["a", "b", "b", "c"]
  164. }
  165. ],
  166. munger = munge(p),
  167. a = munger(i);
  168. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  169. //assert.deepEqual(a, e); //does not work with NaN
  170. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  171. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  172. }
  173. }
  174. };
  175. if(!module.parent) (new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();