GroupDocumentSource.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. }
  46. module.exports = {
  47. "GroupDocumentSource": {
  48. "constructor()": {
  49. // $group spec is not an object
  50. "should throw Error when constructing without args": function testConstructor(){
  51. assertExpectedResult({"throw":true});
  52. },
  53. // $group spec is not an object
  54. "should throw Error when $group spec is not an object": function testConstructor(){
  55. assertExpectedResult({spec:"Foo"});
  56. },
  57. // $group spec is an empty object
  58. "should throw Error when $group spec is an empty object": function testConstructor(){
  59. assertExpectedResult({spec:{}});
  60. },
  61. // $group _id is an empty object
  62. "should not throw when _id is an empty object": function advanceTest(){
  63. assertExpectedResult({spec:{_id:{}}, "throw":false});
  64. },
  65. // $group _id is specified as an invalid object expression
  66. "should throw error when _id is an invalid object expression": function testConstructor(){
  67. assertExpectedResult({
  68. spec:{_id:{$add:1, $and:1}},
  69. });
  70. },
  71. // $group with two _id specs
  72. //NOT Implemented can't do this in Javascript
  73. // $group _id is the empty string
  74. "should not throw when _id is an empty string": function advanceTest(){
  75. assertExpectedResult({spec:{_id:""}, "throw":false});
  76. },
  77. // $group _id is a string constant
  78. "should not throw when _id is a string constant": function advanceTest(){
  79. assertExpectedResult({spec:{_id:"abc"}, "throw":false});
  80. },
  81. // $group with _id set to an invalid field path
  82. "should throw when _id is an invalid field path": function advanceTest(){
  83. assertExpectedResult({spec:{_id:"$a.."}});
  84. },
  85. // $group _id is a numeric constant
  86. "should not throw when _id is a numeric constant": function advanceTest(){
  87. assertExpectedResult({spec:{_id:2}, "throw":false});
  88. },
  89. // $group _id is an array constant
  90. "should not throw when _id is an array constant": function advanceTest(){
  91. assertExpectedResult({spec:{_id:[1,2]}, "throw":false});
  92. },
  93. // $group _id is a regular expression (not supported)
  94. "should throw when _id is a regex": function advanceTest(){
  95. assertExpectedResult({spec:{_id:/a/}});
  96. },
  97. // The name of an aggregate field is specified with a $ prefix
  98. "should throw when aggregate field spec is specified with $ prefix": function advanceTest(){
  99. assertExpectedResult({spec:{_id:1, $foo:{$sum:1}}});
  100. },
  101. // An aggregate field spec that is not an object
  102. "should throw when aggregate field spec is not an object": function advanceTest(){
  103. assertExpectedResult({spec:{_id:1, a:1}});
  104. },
  105. // An aggregate field spec that is not an object
  106. "should throw when aggregate field spec is an empty object": function advanceTest(){
  107. assertExpectedResult({spec:{_id:1, a:{}}});
  108. },
  109. // An aggregate field spec with an invalid accumulator operator
  110. "should throw when aggregate field spec is an invalid accumulator": function advanceTest(){
  111. assertExpectedResult({spec:{_id:1, a:{$bad:1}}});
  112. },
  113. // An aggregate field spec with an array argument
  114. "should throw when aggregate field spec with an array as an argument": function advanceTest(){
  115. assertExpectedResult({spec:{_id:1, a:{$sum:[]}}});
  116. },
  117. // Multiple accumulator operators for a field
  118. "should throw when aggregate field spec with multiple accumulators": function advanceTest(){
  119. assertExpectedResult({spec:{_id:1, a:{$sum:1, $push:1}}});
  120. }
  121. //Not Implementing, not way to support this in Javascript Objects
  122. // Aggregation using duplicate field names is allowed currently
  123. },
  124. "#getSourceName()": {
  125. "should return the correct source name; $group": function testSourceName(){
  126. var gds = new GroupDocumentSource({_id:{}});
  127. assert.strictEqual(gds.getSourceName(), "$group");
  128. }
  129. },
  130. "#advance": {
  131. // $group _id is computed from an object expression
  132. "should compute _id from an object expression": function testAdvance_ObjectExpression(){
  133. assertExpectedResult({
  134. docs: [{a:6}],
  135. spec: {_id:{z:"$a"}},
  136. expected: {_id:{z:6}}
  137. });
  138. },
  139. // $group _id is a field path expression
  140. "should compute _id from a field path expression": function testAdvance_FieldPathExpression(){
  141. assertExpectedResult({
  142. docs: [{a:5}],
  143. spec: {_id:"$a"},
  144. expected: {_id:5}
  145. });
  146. },
  147. // $group _id is a field path expression
  148. "should compute _id from a Date": function testAdvance_Date(){
  149. var d = new Date();
  150. assertExpectedResult({
  151. docs: [{a:d}],
  152. spec: {_id:"$a"},
  153. expected: {_id:d}
  154. });
  155. },
  156. // Aggregate the value of an object expression
  157. "should aggregate the value of an object expression": function testAdvance_ObjectExpression(){
  158. assertExpectedResult({
  159. docs: [{a:6}],
  160. spec: {_id:0, z:{$first:{x:"$a"}}},
  161. expected: {_id:0, z:{x:6}}
  162. });
  163. },
  164. // Aggregate the value of an operator expression
  165. "should aggregate the value of an operator expression": function testAdvance_OperatorExpression(){
  166. assertExpectedResult({
  167. docs: [{a:6}],
  168. spec: {_id:0, z:{$first:"$a"}},
  169. expected: {_id:0, z:6}
  170. });
  171. },
  172. // Aggregate the value of an operator expression
  173. "should aggregate the value of an operator expression with a null id": function testAdvance_Null(){
  174. assertExpectedResult({
  175. docs: [{a:6}],
  176. spec: {_id:null, z:{$first:"$a"}},
  177. expected: {_id:null, z:6}
  178. });
  179. }
  180. }
  181. }
  182. };
  183. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);