GroupDocumentSource.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. "use strict";
  2. var assert = require("assert"),
  3. DocumentSource = require("../../../../lib/pipeline/documentSources/DocumentSource"),
  4. CursorDocumentSource = require("../../../../lib/pipeline/documentSources/CursorDocumentSource"),
  5. Cursor = require("../../../../lib/Cursor"),
  6. GroupDocumentSource = require("../../../../lib/pipeline/documentSources/GroupDocumentSource"),
  7. async = require('async'),
  8. utils = require("../expressions/utils"),
  9. expressions = require("../../../../lib/pipeline/expressions");
  10. /// An assertion for `ObjectExpression` instances based on Mongo's `ExpectedResultBase` class
  11. function assertExpectedResult(args) {
  12. {// check for required args
  13. if (args === undefined) throw new TypeError("missing arg: `args` is required");
  14. if (args.spec && args.throw === undefined) args.throw = true; // Assume that spec only tests expect an error to be thrown
  15. //if (args.spec === undefined) throw new Error("missing arg: `args.spec` is required");
  16. if (args.expected !== undefined && args.docs === undefined) throw new Error("must provide docs with expected value");
  17. }// check for required args
  18. // run implementation
  19. if(args.expected && args.docs){
  20. var gds = GroupDocumentSource.createFromJson(args.spec),
  21. cwc = new CursorDocumentSource.CursorWithContext();
  22. cwc._cursor = new Cursor( args.docs );
  23. var next,
  24. results = [],
  25. cds = new CursorDocumentSource(cwc);
  26. gds.setSource(cds);
  27. var serialized = gds.serialize();
  28. async.whilst(
  29. function() {
  30. return next !== DocumentSource.EOF;
  31. },
  32. function(done) {
  33. gds.getNext(function(err, doc) {
  34. if(err) return done(err);
  35. next = doc;
  36. if(next === DocumentSource.EOF) {
  37. return done();
  38. } else {
  39. results.push(next);
  40. return done();
  41. }
  42. });
  43. },
  44. function(err) {
  45. assert.equal(JSON.stringify(results), JSON.stringify(args.expected));
  46. if(args.done) {
  47. return args.done();
  48. }
  49. }
  50. );
  51. }else{
  52. if(args.throw) {
  53. assert.throws(function(){
  54. GroupDocumentSource.createFromJson(args.spec);
  55. });
  56. } else {
  57. assert.doesNotThrow(function(){
  58. var gds = GroupDocumentSource.createFromJson(args.spec);
  59. });
  60. }
  61. }
  62. }
  63. module.exports = {
  64. "GroupDocumentSource": {
  65. "constructor()": {
  66. // $group spec is not an object
  67. "should throw Error when constructing without args": function testConstructor(){
  68. assertExpectedResult({"throw":true});
  69. },
  70. // $group spec is not an object
  71. "should throw Error when $group spec is not an object": function testConstructor(){
  72. assertExpectedResult({spec:"Foo"});
  73. },
  74. // $group spec is an empty object
  75. "should throw Error when $group spec is an empty object": function testConstructor(){
  76. assertExpectedResult({spec:{}});
  77. },
  78. // $group _id is an empty object
  79. "should not throw when _id is an empty object": function advanceTest(){
  80. assertExpectedResult({spec:{_id:{}}, "throw":false});
  81. },
  82. // $group _id is specified as an invalid object expression
  83. "should throw error when _id is an invalid object expression": function testConstructor(){
  84. assertExpectedResult({
  85. spec:{_id:{$add:1, $and:1}},
  86. });
  87. },
  88. // $group with two _id specs
  89. //NOT Implemented can't do this in Javascript
  90. // $group _id is the empty string
  91. "should not throw when _id is an empty string": function advanceTest(){
  92. assertExpectedResult({spec:{_id:""}, "throw":false});
  93. },
  94. // $group _id is a string constant
  95. "should not throw when _id is a string constant": function advanceTest(){
  96. assertExpectedResult({spec:{_id:"abc"}, "throw":false});
  97. },
  98. // $group with _id set to an invalid field path
  99. "should throw when _id is an invalid field path": function advanceTest(){
  100. assertExpectedResult({spec:{_id:"$a.."}});
  101. },
  102. // $group _id is a numeric constant
  103. "should not throw when _id is a numeric constant": function advanceTest(){
  104. assertExpectedResult({spec:{_id:2}, "throw":false});
  105. },
  106. // $group _id is an array constant
  107. "should not throw when _id is an array constant": function advanceTest(){
  108. assertExpectedResult({spec:{_id:[1,2]}, "throw":false});
  109. },
  110. // $group _id is a regular expression (not supported)
  111. "should not throw when _id is a regex": function advanceTest(){
  112. assertExpectedResult({spec:{_id:/a/}, "throw":false});
  113. },
  114. // The name of an aggregate field is specified with a $ prefix
  115. "should throw when aggregate field spec is specified with $ prefix": function advanceTest(){
  116. assertExpectedResult({spec:{_id:1, $foo:{$sum:1}}});
  117. },
  118. // An aggregate field spec that is not an object
  119. "should throw when aggregate field spec is not an object": function advanceTest(){
  120. assertExpectedResult({spec:{_id:1, a:1}});
  121. },
  122. // An aggregate field spec that is not an object
  123. "should throw when aggregate field spec is an empty object": function advanceTest(){
  124. assertExpectedResult({spec:{_id:1, a:{}}});
  125. },
  126. // An aggregate field spec with an invalid accumulator operator
  127. "should throw when aggregate field spec is an invalid accumulator": function advanceTest(){
  128. assertExpectedResult({spec:{_id:1, a:{$bad:1}}});
  129. },
  130. // An aggregate field spec with an array argument
  131. "should throw when aggregate field spec with an array as an argument": function advanceTest(){
  132. assertExpectedResult({spec:{_id:1, a:{$sum:[]}}});
  133. },
  134. // Multiple accumulator operators for a field
  135. "should throw when aggregate field spec with multiple accumulators": function advanceTest(){
  136. assertExpectedResult({spec:{_id:1, a:{$sum:1, $push:1}}});
  137. }
  138. },
  139. "#getSourceName()": {
  140. "should return the correct source name; $group": function testSourceName(){
  141. var gds = new GroupDocumentSource({_id:{}});
  142. assert.strictEqual(gds.getSourceName(), "$group");
  143. }
  144. },
  145. "#getNext, #populate": {
  146. // Aggregation using duplicate field names is allowed currently
  147. // Note: Can't duplicate fields in javascript objects -- skipped
  148. // $group _id is computed from an object expression
  149. "should compute _id from an object expression": function testAdvance_ObjectExpression(){
  150. assertExpectedResult({
  151. docs: [{a:6}],
  152. spec: {_id:{z:"$a"}},
  153. expected: [{_id:{z:6}}]
  154. });
  155. },
  156. // $group _id is a field path expression
  157. "should compute _id from a field path expression": function testAdvance_FieldPathExpression(){
  158. assertExpectedResult({
  159. docs: [{a:5}],
  160. spec: {_id:"$a"},
  161. expected: [{_id:5}]
  162. });
  163. },
  164. // $group _id is a field path expression
  165. "should compute _id from a Date": function testAdvance_Date(){
  166. var d = new Date();
  167. assertExpectedResult({
  168. docs: [{a:d}],
  169. spec: {_id:"$a"},
  170. expected: [{_id:d}]
  171. });
  172. },
  173. // Aggregate the value of an object expression
  174. "should aggregate the value of an object expression": function testAdvance_ObjectExpression(){
  175. assertExpectedResult({
  176. docs: [{a:6}],
  177. spec: {_id:0, z:{$first:{x:"$a"}}},
  178. expected: [{_id:0, z:{x:6}}]
  179. });
  180. },
  181. // Aggregate the value of an operator expression
  182. "should aggregate the value of an operator expression": function testAdvance_OperatorExpression(){
  183. assertExpectedResult({
  184. docs: [{a:6}],
  185. spec: {_id:0, z:{$first:"$a"}},
  186. expected: [{_id:0, z:6}]
  187. });
  188. },
  189. // Aggregate the value of an operator expression
  190. "should aggregate the value of an operator expression with a null id": function testAdvance_Null(){
  191. assertExpectedResult({
  192. docs: [{a:6}],
  193. spec: {_id:null, z:{$first:"$a"}},
  194. expected: [{_id:null, z:6}]
  195. });
  196. },
  197. // A $group performed on a single document
  198. "should make one group with one values": function SingleDocument() {
  199. assertExpectedResult({
  200. docs: [{a:1}],
  201. spec: {_id:0, a:{$sum:"$a"}},
  202. expected: [{_id:0, a:1}]
  203. });
  204. },
  205. // A $group performed on two values for a single key
  206. "should make one group with two values": function TwoValuesSingleKey() {
  207. assertExpectedResult({
  208. docs: [{a:1}, {a:2}],
  209. spec: {_id:0, a:{$push:"$a"}},
  210. expected: [{_id:0, a:[1,2]}]
  211. });
  212. },
  213. // A $group performed on two values with one key each.
  214. "should make two groups with one value": function TwoValuesTwoKeys() {
  215. assertExpectedResult({
  216. docs: [{_id:0,a:1}, {_id:1,a:2}],
  217. spec: {_id:"$_id", a:{$push:"$a"}},
  218. expected: [{_id:0, a:[1]}, {_id:1, a:[2]}]
  219. });
  220. },
  221. // A $group performed on two values with two keys each.
  222. "should make two groups with two values": function FourValuesTwoKeys() {
  223. assertExpectedResult({
  224. docs: [{_id:0,a:1}, {_id:1,a:2}, {_id:0,a:3}, {_id:1,a:4}],
  225. spec: {_id:"$_id", a:{$push:"$a"}},
  226. expected: [{_id:0, a:[1, 3]}, {_id:1, a:[2, 4]}]
  227. });
  228. },
  229. // A $group performed on two values with two keys each and two accumulator operations.
  230. "should make two groups with two values with two accumulators": function FourValuesTwoKeysTwoAccumulators() {
  231. assertExpectedResult({
  232. docs: [{_id:0,a:1}, {_id:1,a:2}, {_id:0,a:3}, {_id:1,a:4}],
  233. spec: {_id:"$_id", list:{$push:"$a"}, sum:{$sum:{$divide:["$a", 2]}}},
  234. expected: [{_id:0, list:[1, 3], sum:2}, {_id:1, list:[2, 4], sum:3}]
  235. });
  236. },
  237. // Null and undefined _id values are grouped together.
  238. "should group null and undefined _id's together": function GroupNullUndefinedIds() {
  239. assertExpectedResult({
  240. docs: [{a:null, b:100}, {b:10}],
  241. spec: {_id:"$a", sum:{$sum:"$b"}},
  242. expected: [{_id:null, sum:110}]
  243. });
  244. },
  245. // A complex _id expression.
  246. "should group based on a complex id": function ComplexId() {
  247. assertExpectedResult({
  248. docs: [{a:"de", b:"ad", c:"beef", d:""}, {a:"d", b:"eadbe", c:"", d:"ef"}],
  249. spec: {_id:{$concat:["$a", "$b", "$c", "$d"]}},
  250. expected: [{_id:'deadbeef'}]
  251. });
  252. },
  253. // An undefined accumulator value is dropped.
  254. "should ignore undefined values during accumulation":function UndefinedAccumulatorValue() {
  255. assertExpectedResult({
  256. docs: [{}],
  257. spec: {_id:0, first:{$first:"$missing"}},
  258. expected: [{_id:0, first:null}]
  259. });
  260. }
  261. }
  262. }
  263. };
  264. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).grep(process.env.MOCHA_GREP || '').run(process.exit);