GroupDocumentSource.js 7.4 KB

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