aggregate_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. "use strict";
  2. /*jshint camelcase:false*/
  3. if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
  4. var assert = require("assert"),
  5. aggregate = require("../../");
  6. aggregate.cmdDefaults.batchSize = Infinity;
  7. // Utility to test the various use cases of `aggregate`
  8. function testAggregate(opts){
  9. if (!opts.asyncOnly){
  10. // SYNC: test one-off usage
  11. var results = aggregate(opts.pipeline, opts.inputs);
  12. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
  13. // SYNC: test one-off usage with context
  14. results = aggregate(opts.pipeline, {hi: "there"}, opts.inputs);
  15. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
  16. // SYNC: test use with context
  17. var aggregator = aggregate(opts.pipeline, {hi: "there"});
  18. results = aggregator(opts.inputs);
  19. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
  20. // SYNC: test reusable aggregator functionality
  21. aggregator = aggregate(opts.pipeline);
  22. results = aggregator(opts.inputs);
  23. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
  24. // SYNC: test that it is actually reusable
  25. results = aggregator(opts.inputs);
  26. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected), "should allow sync aggregator reuse");
  27. }
  28. // ASYNC: test one-off usage
  29. aggregate(opts.pipeline, opts.inputs, function(err, results){
  30. assert.ifError(err);
  31. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
  32. // ASYNC: test one-off usage with context
  33. aggregate(opts.pipeline, {hi: "there"}, opts.inputs, function(err, results){
  34. assert.ifError(err);
  35. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
  36. // ASYNC: test reusable aggregator functionality with context
  37. var aggregator = aggregate(opts.pipeline);
  38. aggregator({hi: "there"}, opts.inputs, function(err, results){
  39. assert.ifError(err);
  40. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
  41. // ASYNC: test reusable aggregator functionality
  42. var aggregator = aggregate(opts.pipeline);
  43. aggregator(opts.inputs, function(err, results){
  44. assert.ifError(err);
  45. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected));
  46. // ASYNC: test that it is actually reusable
  47. aggregator(opts.inputs, function(err, results){
  48. assert.ifError(err);
  49. assert.equal(JSON.stringify(results), JSON.stringify(opts.expected), "should allow async aggregator reuse");
  50. // success!
  51. return opts.next();
  52. });
  53. });
  54. });
  55. });
  56. });
  57. }
  58. function testBatches(opts){
  59. var inputs = [],
  60. actual = [],
  61. eachExpected = [],
  62. expected = [];
  63. for(var i = 0; i < opts.documents; i++){
  64. inputs.push({a:i});
  65. eachExpected.push({foo:i});
  66. if (eachExpected.length % opts.batchSize === 0){
  67. expected.push(eachExpected);
  68. eachExpected = [];
  69. }
  70. }
  71. expected.push(eachExpected);
  72. aggregate({
  73. batchSize:opts.batchSize,
  74. pipeline: [
  75. {$project:{
  76. foo: "$a"
  77. }}
  78. ]},
  79. inputs,
  80. function(err, results){
  81. assert.ifError(err);
  82. if (results) {
  83. actual.push(results);
  84. } else {
  85. assert.deepEqual(actual, expected);
  86. opts.next();
  87. }
  88. });
  89. }
  90. exports.aggregate = {
  91. "should be able to use an empty pipeline (no-op)": function(next){
  92. testAggregate({
  93. inputs: [1, 2, 3],
  94. pipeline: [],
  95. expected: [1, 2, 3],
  96. next: next
  97. });
  98. },
  99. "should be able to use a limit operator": function(next){
  100. testAggregate({
  101. inputs: [{_id:0}, {_id:1}, {_id:2}, {_id:3}, {_id:4}, {_id:5}],
  102. pipeline: [{$limit:2}],
  103. expected: [{_id:0}, {_id:1}],
  104. next: next
  105. });
  106. },
  107. "should be able to use a match operator": function(next){
  108. testAggregate({
  109. inputs: [{_id:0, e:1}, {_id:1, e:0}, {_id:2, e:1}, {_id:3, e:0}, {_id:4, e:1}, {_id:5, e:0}],
  110. pipeline: [{$match:{e:1}}],
  111. expected: [{_id:0, e:1}, {_id:2, e:1}, {_id:4, e:1}],
  112. next: next
  113. });
  114. },
  115. "should be able to use a skip operator": function(next){
  116. testAggregate({
  117. inputs: [{_id:0}, {_id:1}, {_id:2}, {_id:3}, {_id:4}, {_id:5}],
  118. pipeline: [{$skip:2}, {$skip:1}], //testing w/ 2 ensures independent state variables
  119. expected: [{_id:3}, {_id:4}, {_id:5}],
  120. next: next
  121. });
  122. },
  123. "should be able to use a skip and then a limit operator together in the same pipeline": function(next){
  124. testAggregate({
  125. inputs: [{_id:0, e:1}, {_id:1, e:0}, {_id:2, e:1}, {_id:3, e:0}, {_id:4, e:1}, {_id:5, e:0}],
  126. pipeline: [{$skip:2}, {$limit:1}],
  127. expected: [{_id:2, e:1}],
  128. next: next
  129. });
  130. },
  131. "should be able to construct an instance with unwind operators properly": function(next){
  132. testAggregate({
  133. inputs: [
  134. {_id:0, nodes:[
  135. {one:[11], two:[2,2]},
  136. {one:[1,1], two:[22]}
  137. ]},
  138. {_id:1, nodes:[
  139. {two:[22], three:[333]},
  140. {one:[1], three:[3,3,3]}
  141. ]}
  142. ],
  143. pipeline: [{$unwind:"$nodes"}, {$unwind:"$nodes.two"}],
  144. expected: [
  145. {_id:0,nodes:{one:[11],two:2}},
  146. {_id:0,nodes:{one:[11],two:2}},
  147. {_id:0,nodes:{one:[1,1],two:22}},
  148. {_id:1,nodes:{two:22,three:[333]}}
  149. ],
  150. next: next
  151. });
  152. },
  153. "should be able to use a project operator": function(next){
  154. // NOTE: Test case broken until expression is fixed
  155. testAggregate({
  156. inputs: [{_id:0, e:1, f:23}, {_id:2, e:2, g:34}, {_id:4, e:3}],
  157. pipeline: [
  158. {$project:{
  159. e:1,
  160. a:{$add:["$e", "$e"]},
  161. b:{$cond:[{$eq:["$e", 2]}, "two", "not two"]}
  162. //TODO: high level test of all other expression operators
  163. }}
  164. ],
  165. expected: [{_id:0, e:1, a:2, b:"not two"}, {_id:2, e:2, a:4, b:"two"}, {_id:4, e:3, a:6, b:"not two"}],
  166. next: next
  167. });
  168. },
  169. "should be able to use a project operator to exclude the _id field": function(next){
  170. // NOTE: Test case broken until expression is fixed
  171. testAggregate({
  172. inputs: [{_id:0, e:1, f:23}, {_id:2, e:2, g:34}, {_id:4, e:3}],
  173. pipeline: [
  174. {$project:{
  175. _id:0,
  176. e:1
  177. //TODO: high level test of all other expression operators
  178. }}
  179. ],
  180. expected: [{e:1}, {e:2}, {e:3}],
  181. next: next
  182. });
  183. },
  184. "should be able to project out a whole document and leave an empty": function(next) {
  185. testAggregate({
  186. inputs: [{_id:0, a:1}, {_id:1, a:2, b:1}, {_id:2, b:2, c:1}],
  187. pipeline: [
  188. {$project:{
  189. _id:0,
  190. a:1
  191. //TODO: high level test of all other expression operators
  192. }}
  193. ],
  194. expected: [{a:1}, {a:2}, {}],
  195. next: next
  196. });
  197. },
  198. "should be able to construct an instance with sort operators properly (ascending)": function(next){
  199. testAggregate({
  200. inputs: [
  201. {_id:3.14159}, {_id:-273.15},
  202. {_id:42}, {_id:11}, {_id:1},
  203. {_id:null}, {_id:NaN}
  204. ],
  205. pipeline: [{$sort:{_id:1}}],
  206. expected: [
  207. {_id:null}, {_id:NaN},
  208. {_id:-273.15}, {_id:1}, {_id:3.14159}, {_id:11}, {_id:42}
  209. ],
  210. next: next
  211. });
  212. },
  213. "should be able to construct an instance with $group operators properly": function(next){
  214. testAggregate({
  215. inputs: [
  216. {_id:0, a:1},
  217. {_id:0, a:2},
  218. {_id:0, a:3},
  219. {_id:0, a:4},
  220. {_id:0, a:1.5},
  221. {_id:0, a:null},
  222. {_id:1, b:"a"},
  223. {_id:1, b:"b"},
  224. {_id:1, b:"b"},
  225. {_id:1, b:"c"}
  226. ],
  227. pipeline:[
  228. {$group:{
  229. _id:"$_id",
  230. sum_a:{$sum:"$a"},
  231. //min_a:{$min:"$a"}, //this is busted in this version of mongo
  232. max_a:{$max:"$a"},
  233. avg_a:{$avg:"$a"},
  234. first_b:{$first:"$b"},
  235. last_b:{$last:"$b"},
  236. addToSet_b:{$addToSet:"$b"},
  237. push_b:{$push:"$b"}
  238. }}
  239. ],
  240. expected: [
  241. {
  242. _id:0,
  243. sum_a:11.5,
  244. //min_a:1,
  245. max_a:4,
  246. avg_a:2.3,
  247. first_b:null,
  248. last_b:null,
  249. addToSet_b:[],
  250. push_b:[]
  251. },
  252. {
  253. _id:1,
  254. sum_a:0,
  255. //min_a:null,
  256. max_a:null,
  257. avg_a:0,
  258. first_b:"a",
  259. last_b:"c",
  260. addToSet_b:["a", "b", "c"],
  261. push_b:["a", "b", "b", "c"]
  262. }
  263. ],
  264. next: next
  265. });
  266. },
  267. "should be able to construct an instance with $group using concat": function(next){
  268. testAggregate({
  269. inputs: [
  270. {_id:0, a:null},
  271. {_id:1, a:"a"},
  272. {_id:1, a:"b"},
  273. {_id:1, a:"b"},
  274. {_id:1, a:"c"}
  275. ],
  276. pipeline: [
  277. {$group:{
  278. _id:{$concat:["$a"]}
  279. }}
  280. ],
  281. expected: [
  282. {_id: null},
  283. {_id: "a"},
  284. {_id: "b"},
  285. {_id: "c"}
  286. ],
  287. next: next
  288. });
  289. },
  290. "should be able to successfully use comparisions of objects to nulls without throwing an exception": function(next){
  291. testAggregate({
  292. inputs: [
  293. {
  294. cond:{$or:[
  295. {$eq:["$server","Starmetal.demo.com"]},
  296. ]},
  297. value:"PII"
  298. },
  299. {
  300. cond:{$or:[
  301. {$eq:["$server","Specium.demo.com"]},
  302. {$eq:["$server","Germanium.demo.com"]},
  303. {$eq:["$server","Runite.demo.com"]}
  304. ]},
  305. value:"PI"
  306. },
  307. {
  308. cond:{$or:[
  309. {$eq:["$server","Primal.demo.com"]}
  310. ]},
  311. value:"Confidential"
  312. },
  313. {
  314. cond:{$or:[
  315. {$eq:["$server","Polarite.demo.com"]},
  316. {$eq:["$server","Ryanium.demo.com"]}
  317. ]},
  318. value:"Proprietary"
  319. },
  320. {
  321. cond:{$or:[
  322. {$eq:["$server","Phazon.demo.com"]}
  323. ]},
  324. value:"PHI"
  325. },
  326. {
  327. cond:null,
  328. value:"Authorized"
  329. }
  330. ],
  331. pipeline: [
  332. {$skip:1},
  333. {$limit:1},
  334. {$project:{
  335. retValue:{$cond:[
  336. {$ne:["$cond", null]},
  337. null,
  338. "$value"
  339. ]}
  340. }}
  341. ],
  342. expected: [{"retValue":null}],
  343. next: next
  344. });
  345. },
  346. "should be able to successfully compare a null to a null": function(next){
  347. testAggregate({
  348. inputs: [
  349. {
  350. cond:null,
  351. value:"Authorized"
  352. }
  353. ],
  354. pipeline: [
  355. {$project:{
  356. retValue:{$cond:[
  357. {$eq:["$cond", null]},
  358. "$value",
  359. null
  360. ]}
  361. }}
  362. ],
  363. expected: [{"retValue":"Authorized"}],
  364. next: next
  365. });
  366. },
  367. "should be able to handle a large array of inputs": function(next){
  368. var inputs = [],
  369. expected = [];
  370. for(var i = 0; i < 10000; i++){
  371. inputs.push({a:i});
  372. expected.push({foo:i});
  373. }
  374. testAggregate({
  375. asyncOnly: true,
  376. inputs: inputs,
  377. pipeline: [
  378. {$project:{
  379. foo: "$a"
  380. }}
  381. ],
  382. expected: expected,
  383. next: next
  384. });
  385. },
  386. "should be able to handle a small arrays in batches": function(next){
  387. testBatches({
  388. documents: 5,
  389. batchSize: 100,
  390. next: next
  391. });
  392. },
  393. "should be able to handle an array equal to the batch size": function(next){
  394. testBatches({
  395. documents: 100,
  396. batchSize: 100,
  397. next: next
  398. });
  399. },
  400. "should be able to handle a large array in batches": function(next){
  401. testBatches({
  402. documents: 10000,
  403. batchSize: 100,
  404. next: next
  405. });
  406. },
  407. "should be able to explain an empty pipeline": function(){
  408. var pipeline = [],
  409. expected = [],
  410. actual = aggregate({
  411. pipeline: pipeline,
  412. explain: true
  413. });
  414. assert.deepEqual(actual, expected);
  415. },
  416. "should be able to explain a full pipeline": function(){
  417. var pipeline = [
  418. {$match:{e:1}},
  419. {$match:{d:1}},
  420. {$skip:2},
  421. {$limit:1},
  422. {$project:{
  423. foo: "$a"
  424. }},
  425. {$group:{
  426. _id:{$concat:["$foo"]}
  427. }}
  428. ],
  429. expected = [
  430. {$match:{$and:[{e:1},{d:1}]}},
  431. {$limit:3},
  432. {$skip:2},
  433. {$project:{
  434. foo: "$a"
  435. }},
  436. {$group:{
  437. _id:{$concat:["$foo"]}
  438. }}
  439. ],
  440. actual = aggregate({
  441. pipeline: pipeline,
  442. explain: true
  443. });
  444. assert.deepEqual(actual, expected);
  445. },
  446. "should be able to explain a full pipeline with inputs": function(){
  447. var pipeline = [
  448. {$match:{e:1}},
  449. {$match:{d:1}},
  450. {$skip:2},
  451. {$limit:1},
  452. {$project:{
  453. foo: "$a"
  454. }},
  455. {$group:{
  456. _id:{$concat:["$foo"]}
  457. }}
  458. ],
  459. expected = [
  460. {"$cursor":{
  461. "query":{"$and":[{"e":1},{"d":1}]},
  462. "fields":{"a":1,"_id":1},
  463. "plan":{
  464. "type":"ArrayRunner",
  465. "nDocs":1,
  466. "position":0,
  467. "state":"RUNNER_ADVANCED"
  468. }
  469. }},
  470. {"$match":{"$and":[{"e":1},{"d":1}]}},
  471. {"$limit":3},
  472. {"$skip":2},
  473. {"$project":{"foo":"$a"}},
  474. {"$group":{"_id":{"$concat":["$foo"]}}}
  475. ],
  476. actual = aggregate({
  477. pipeline: pipeline,
  478. explain: true
  479. }, [{e:1,d:2,a:4}]);
  480. assert.deepEqual(actual, expected);
  481. },
  482. "should throw parse errors if called sync-ly": function(){
  483. assert.throws(function(){
  484. aggregate([{"$project":{"foo":"bar"}}], [{"bar":1}]);
  485. });
  486. assert.throws(function(){
  487. aggregate([{"$project":{"foo":"bar"}}]);
  488. });
  489. },
  490. "should return parse errors in the callback if called async-ly": function(done){
  491. aggregate([{"$project":{"foo":"bar"}}], [{"bar":1}], function(err, results){
  492. assert(err, "Expected Error");
  493. done();
  494. });
  495. },
  496. "should throw pipeline errors if called sync-ly": function(){
  497. assert.throws(function(){
  498. aggregate([{"$project":{"sum":{"$add":["$foo", "$bar"]}}}], [{"foo":1, "bar":"baz"}]);
  499. });
  500. var agg = aggregate([{"$project":{"sum":{"$add":["$foo", "$bar"]}}}]);
  501. assert.throws(function(){
  502. agg([{"foo":1, "bar":"baz"}]);
  503. });
  504. assert.doesNotThrow(function(){
  505. agg([{"foo":1, "bar":2}]);
  506. });
  507. },
  508. "should return pipeline errors in the callback if called async-ly": function(done){
  509. aggregate([{"$project":{"sum":{"$add":["$foo", "$bar"]}}}], [{"foo":1, "bar":"baz"}], function(err, results){
  510. assert(err, "Expected Error");
  511. var agg = aggregate([{"$project":{"sum":{"$add":["$foo", "$bar"]}}}]);
  512. agg([{"foo":1, "bar":"baz"}], function(err, results){
  513. assert(err, "Expected Error");
  514. agg([{"foo":1, "bar":2}], function(err, results){
  515. assert.ifError(err, "UnExpected Error");
  516. done();
  517. });
  518. });
  519. });
  520. },
  521. };