GroupDocumentSource.js 6.3 KB

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