munge.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /*
  61. "should be able to use a $project operator": function(){
  62. 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}],
  63. p = [{$project:{e:1}}],
  64. e = [{_id:0, e:1}, {_id:2, e:1}, {_id:4, e:1}],
  65. munger = munge(p),
  66. a = munger(i);
  67. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  68. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  69. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  70. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  71. },
  72. //TODO: $project w/ expressions
  73. "should be able to construct an instance with $unwind operators properly": function(){
  74. var i = [
  75. {_id:0, nodes:[
  76. {one:[11], two:[2,2]},
  77. {one:[1,1], two:[22]}
  78. ]},
  79. {_id:1, nodes:[
  80. {two:[22], three:[333]},
  81. {one:[1], three:[3,3,3]}
  82. ]}
  83. ],
  84. p = [{$unwind:"$nodes"}, {$unwind:"$nodes.two"}],
  85. e = [
  86. {_id:0,nodes:{one:[11],two:2}},
  87. {_id:0,nodes:{one:[11],two:2}},
  88. {_id:0,nodes:{one:[1,1],two:22}},
  89. {_id:1,nodes:{two:22,three:[333]}}
  90. ],
  91. munger = munge(p),
  92. a = munger(i);
  93. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  94. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  95. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  96. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  97. },
  98. "should be able to construct an instance with $sort operators properly (ascending)": function(){
  99. var i = [
  100. {_id:3.14159}, {_id:-273.15},
  101. {_id:42}, {_id:11}, {_id:1},
  102. {_id:false},{_id:true},
  103. {_id:""}, {_id:"a"}, {_id:"A"}, {_id:"Z"}, {_id:"z"},
  104. {_id:null}, {_id:NaN},
  105. //TODO: test with Objects; e.g., {_id:{a:{b:1}},
  106. {_id:new Date("2012-10-22T08:01:21.235Z")}, {_id:new Date("2012-10-15T15:48:55.181Z")}
  107. ],
  108. p = [{$sort:{_id:1}}],
  109. e = [
  110. {_id:null}, {_id:NaN},
  111. {_id:-273.15}, {_id:1}, {_id:3.14159}, {_id:11}, {_id:42},
  112. {_id:""}, {_id:"A"}, {_id:"Z"}, {_id:"a"}, {_id:"z"},
  113. {_id:false}, {_id:true},
  114. {_id:new Date("2012-10-15T15:48:55.181Z")}, {_id:new Date("2012-10-22T08:01:21.235Z")}
  115. ];
  116. var a = munge(p, i);
  117. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  118. }
  119. */
  120. }
  121. };
  122. if(!module.parent) (new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();