GroupDocumentSource.js 12 KB

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