munge.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. }}],
  92. 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}],
  93. munger = munge(p),
  94. a = munger(i);
  95. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  96. assert.deepEqual(munger(i), e, "Reuse of munger should yield the same results!");
  97. assert.deepEqual(munge(p, i), e, "Alternate use of munge should yield the same results!");
  98. },
  99. "should be able to construct an instance with $sort operators properly (ascending)": function(){
  100. var i = [
  101. {_id:3.14159}, {_id:-273.15},
  102. {_id:42}, {_id:11}, {_id:1},
  103. {_id:null}, {_id:NaN}
  104. ],
  105. p = [{$sort:{_id:1}}],
  106. e = [
  107. {_id:null}, {_id:NaN},
  108. {_id:-273.15}, {_id:1}, {_id:3.14159}, {_id:11}, {_id:42}
  109. ],
  110. munger = munge(p),
  111. a = munger(i);
  112. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  113. //assert.deepEqual(a, e); //does not work with NaN
  114. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  115. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  116. }
  117. }
  118. };
  119. if(!module.parent) (new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();