GroupDocumentSource.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var assert = require("assert"),
  2. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  3. Cursor = require("../../../../lib/Cursor"),
  4. GroupDocumentSource = require("../../../../lib/pipeline/documentSources/GroupDocumentSource");
  5. /// An assertion for `ObjectExpression` instances based on Mongo's `ExpectedResultBase` class
  6. function assertExpectedResult(args) {
  7. {// check for required args
  8. if (args === undefined) throw new TypeError("missing arg: `args` is required");
  9. if (args.spec && args.throw === undefined) args.throw = true; // Assume that spec only tests expect an error to be thrown
  10. //if (args.spec === undefined) throw new Error("missing arg: `args.spec` is required");
  11. if (args.expected !== undefined && args.docs === undefined) throw new Error("must provide docs with expected value");
  12. }// check for required args
  13. // run implementation
  14. if(args.expected && args.docs){
  15. var gds = new GroupDocumentSource(args.spec),
  16. cwc = new CursorDocumentSource.CursorWithContext();
  17. cwc._cursor = new Cursor( args.docs );
  18. var cds = new CursorDocumentSource(cwc);
  19. gds.setSource(cds);
  20. gds.advance();
  21. var result = gds.getCurrent();
  22. assert.deepEqual(result, args.expected);
  23. }else{
  24. if(args.throw)
  25. assert.throws(function(){
  26. new GroupDocumentSource(args.spec);
  27. });
  28. else
  29. assert.doesNotThrow(function(){
  30. new GroupDocumentSource(args.spec);
  31. });
  32. }
  33. }
  34. module.exports = {
  35. "GroupDocumentSource": {
  36. "constructor()": {
  37. /** $group spec is not an object. */
  38. "should throw Error when constructing without args": function testConstructor(){
  39. assertExpectedResult({throw:true});
  40. },
  41. /** $group spec is not an object. */
  42. "should throw Error when $group spec is not an object": function testConstructor(){
  43. assertExpectedResult({spec:"Foo"});
  44. },
  45. /** $group spec is an empty object. */
  46. "should throw Error when $group spec is an empty object": function testConstructor(){
  47. assertExpectedResult({spec:{}});
  48. },
  49. // /** $group _id is an empty object. */
  50. // "should not throw when _id is an empty object": function advanceTest(){
  51. // assertExpectedResult({spec:{$group:{_id:{}}}, throw:false});
  52. // },
  53. /** $group _id is specified as an invalid object expression. */
  54. "should throw error when _id is an invalid object expression": function testConstructor(){
  55. assertExpectedResult({
  56. spec:{$group:{_id:{$add:1, $and:1}}},
  57. });
  58. },
  59. /** $group with two _id specs. */
  60. //NOT Implemented can't do this in Javascript
  61. // /** $group _id is the empty string. */
  62. // "should not throw when _id is an empty string": function advanceTest(){
  63. // assertExpectedResult({spec:{$group:{_id:""}}, throw:false});
  64. // },
  65. // /** $group _id is a string constant. */
  66. // "should not throw when _id is a string constant": function advanceTest(){
  67. // assertExpectedResult({spec:{$group:{_id:"abc"}}, throw:false});
  68. // },
  69. /** $group with _id set to an invalid field path. */
  70. "should throw when _id is an invalid field path": function advanceTest(){
  71. assertExpectedResult({spec:{$group:{_id:"$a.."}}});
  72. },
  73. // /** $group _id is a numeric constant. */
  74. // "should not throw when _id is a numeric constant": function advanceTest(){
  75. // assertExpectedResult({spec:{$group:{_id:2}}, throw:false});
  76. // },
  77. // /** $group _id is an array constant. */
  78. // "should not throw when _id is an array constant": function advanceTest(){
  79. // assertExpectedResult({spec:{$group:{_id:[1,2]}}, throw:false});
  80. // },
  81. /** $group _id is a regular expression (not supported). */
  82. "should throw when _id is a regex": function advanceTest(){
  83. assertExpectedResult({spec:{$group:{_id:/a/}}});
  84. },
  85. /** The name of an aggregate field is specified with a $ prefix. */
  86. "should throw when aggregate field spec is specified with $ prefix": function advanceTest(){
  87. assertExpectedResult({spec:{$group:{_id:1, $foo:{$sum:1}}}});
  88. },
  89. /** An aggregate field spec that is not an object. */
  90. "should throw when aggregate field spec is not an object": function advanceTest(){
  91. assertExpectedResult({spec:{$group:{_id:1, a:1}}});
  92. },
  93. /** An aggregate field spec that is not an object. */
  94. "should throw when aggregate field spec is an empty object": function advanceTest(){
  95. assertExpectedResult({spec:{$group:{_id:1, a:{}}}});
  96. },
  97. /** An aggregate field spec with an invalid accumulator operator. */
  98. "should throw when aggregate field spec is an invalid accumulator": function advanceTest(){
  99. assertExpectedResult({spec:{$group:{_id:1, a:{$bad:1}}}});
  100. },
  101. /** An aggregate field spec with an array argument. */
  102. "should throw when aggregate field spec with an array as an argument": function advanceTest(){
  103. assertExpectedResult({spec:{$group:{_id:1, a:{$sum:[]}}}});
  104. },
  105. /** Multiple accumulator operators for a field. */
  106. "should throw when aggregate field spec with multiple accumulators": function advanceTest(){
  107. assertExpectedResult({spec:{$group:{_id:1, a:{$sum:1, $push:1}}}});
  108. }
  109. //Not Implementing, not way to support this in Javascript Objects
  110. /** Aggregation using duplicate field names is allowed currently. */
  111. },
  112. "#getSourceName()": {
  113. // "should return the correct source name; $group": function testSourceName(){
  114. // var gds = new GroupDocumentSource({$group:{_id:{}}});
  115. // assert.strictEqual(gds.getSourceName(), "$group");
  116. // }
  117. },
  118. "#advance": {
  119. // /** $group _id is computed from an object expression. */
  120. // "should compute _id from an object expression": function advanceTest(){
  121. // assertExpectedResult({
  122. // docs:[{a:6}],
  123. // spec:{$group:{_id:{z:"$a"}}},
  124. // expected:{_id:{z:6}}
  125. // });
  126. // },
  127. // /** $group _id is a field path expression. */
  128. // "should compute _id a field path expression": function advanceTest(){
  129. // assertExpectedResult({
  130. // docs:[{a:5}],
  131. // spec:{$group:{_id:"$a"}},
  132. // expected:{_id:{z:5}}
  133. // });
  134. // },
  135. // /** Aggregate the value of an object expression. */
  136. // "should aggregate the value of an object expression": function advanceTest(){
  137. // assertExpectedResult({
  138. // docs:[{a:6}],
  139. // spec:{$group:{_id:0, z:{$first:{x:"$a"}}}},
  140. // expected:{_id:0, z:6}
  141. // });
  142. // },
  143. // /** Aggregate the value of an operator expression. */
  144. // "should aggregate the value of an operator expression": function advanceTest(){
  145. // assertExpectedResult({
  146. // docs:[{a:6}],
  147. // spec:{$group:{_id:0, z:{$first:"$a"}}},
  148. // expected:{_id:0, z:6}
  149. // });
  150. // }
  151. }
  152. }
  153. };
  154. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);