munge.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var jsext = require("jsext").install(), //TODO: remove this...
  2. assert = require("assert"),
  3. munge = require("../../");
  4. module.exports = {
  5. "munge": {
  6. "should be able to use an empty pipeline (no-op)": function(){
  7. console.debug("");
  8. var i = [1, 2, 3],
  9. p = [],
  10. e = [1, 2, 3],
  11. munger = munge(p),
  12. a = munger(i);
  13. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  14. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  15. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  16. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  17. },
  18. "should be able to use a $skip operator": function(){
  19. console.debug("");
  20. var i = [{_id:0}, {_id:1}, {_id:2}, {_id:3}, {_id:4}, {_id:5}],
  21. p = [{$skip:2}, {$skip:1}], //testing w/ 2 ensures independent state variables
  22. e = [{_id:3}, {_id:4}, {_id:5}],
  23. munger = munge(p),
  24. a = munger(i);
  25. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  26. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  27. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  28. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  29. },
  30. "should be able to use a $limit operator": function(){
  31. console.debug("");
  32. var i = [{_id:0}, {_id:1}, {_id:2}, {_id:3}, {_id:4}, {_id:5}],
  33. p = [{$limit:2}],
  34. e = [{_id:0}, {_id:1}],
  35. munger = munge(p),
  36. a = munger(i);
  37. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  38. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  39. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  40. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  41. },
  42. "should be able to use a $skip and then a $limit operator together in the same pipeline": function(){
  43. console.debug("");
  44. 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}],
  45. p = [{$skip:2}, {$limit:1}],
  46. e = [{_id:2, e:1}],
  47. munger = munge(p),
  48. a = munger(i);
  49. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  50. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  51. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  52. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  53. },
  54. "should be able to use a $match operator": function(){
  55. console.debug("");
  56. 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}],
  57. p = [{$match:{e:1}}],
  58. e = [{_id:0, e:1}, {_id:2, e:1}, {_id:4, e:1}],
  59. munger = munge(p),
  60. a = munger(i);
  61. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  62. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  63. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  64. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  65. },
  66. "should be able to use a $project operator": function(){
  67. console.debug("");
  68. 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}],
  69. p = [{$project:{e:1}}],
  70. e = [{_id:0, e:1}, {_id:2, e:1}, {_id:4, e:1}],
  71. munger = munge(p),
  72. a = munger(i);
  73. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  74. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  75. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  76. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  77. },
  78. //TODO: $project w/ expressions
  79. /*
  80. "should be able to construct an instance with $unwind operators properly": function(){
  81. console.debug("");
  82. var i = [
  83. {_id:0, nodes:[
  84. {one:[11], two:[2,2]},
  85. {one:[1,1], two:[22]}
  86. ]},
  87. {_id:1, nodes:[
  88. {two:[22], three:[333]},
  89. {one:[1], three:[3,3,3]}
  90. ]}
  91. ],
  92. p = [{$unwind:"$nodes"}, {$unwind:"$nodes.two"}],
  93. e = [
  94. {_id:0,nodes:{one:[11],two:2}},
  95. {_id:0,nodes:{one:[11],two:2}},
  96. {_id:0,nodes:{one:[1,1],two:22}},
  97. {_id:1,nodes:{two:22,three:[333]}}
  98. ],
  99. munger = munge(p),
  100. a = munger(i);
  101. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  102. assert.deepEqual(a, e, "Unexpected value (not deepEqual)!");
  103. assert.equal(JSON.stringify(munger(i)), JSON.stringify(e), "Reuse of munger should yield the same results!");
  104. assert.equal(JSON.stringify(munge(p, i)), JSON.stringify(e), "Alternate use of munge should yield the same results!");
  105. },
  106. "should be able to construct an instance with $sort operators properly (ascending)": function(){
  107. var i = [
  108. {_id:3.14159}, {_id:-273.15},
  109. {_id:42}, {_id:11}, {_id:1},
  110. {_id:false},{_id:true},
  111. {_id:""}, {_id:"a"}, {_id:"A"}, {_id:"Z"}, {_id:"z"},
  112. {_id:null}, {_id:NaN},
  113. //TODO: test with Objects; e.g., {_id:{a:{b:1}},
  114. {_id:new Date("2012-10-22T08:01:21.235Z")}, {_id:new Date("2012-10-15T15:48:55.181Z")}
  115. ],
  116. p = [{$sort:{_id:1}}],
  117. e = [
  118. {_id:null}, {_id:NaN},
  119. {_id:-273.15}, {_id:1}, {_id:3.14159}, {_id:11}, {_id:42},
  120. {_id:""}, {_id:"A"}, {_id:"Z"}, {_id:"a"}, {_id:"z"},
  121. {_id:false}, {_id:true},
  122. {_id:new Date("2012-10-15T15:48:55.181Z")}, {_id:new Date("2012-10-22T08:01:21.235Z")}
  123. ];
  124. console.debug("\nINPUTS:\n", i);
  125. console.debug("\nPIPELINE OPS:\n", p);
  126. var a = munge(p, i);
  127. assert.equal(JSON.stringify(a), JSON.stringify(e), "Unexpected value!");
  128. console.debug("\n");
  129. }
  130. */
  131. }
  132. };
  133. if(!module.parent) (new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();