GroupDocumentSource.js 6.3 KB

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