-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathtest-aggregate.js
38 lines (35 loc) · 1.1 KB
/
test-aggregate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var insert = require('./insert')
var concat = require('concat-stream')
insert('aggregate', [{
name: 'Squirtle', type: 'water'
}, {
name: 'Starmie', type: 'water'
}, {
name: 'Charmander', type: 'fire'
}, {
name: 'Lapras', type: 'water'
}], function (db, t) {
db.a.aggregate({ $group: { _id: '$type' } }, function (err, types) {
t.error(err)
var arr = types.map(function (x) { return x._id })
t.equal(types.length, 2)
t.notEqual(arr.indexOf('fire'), -1)
t.notEqual(arr.indexOf('water'), -1)
// test as a stream
var strm = db.a.aggregate({ $group: { _id: '$type' } })
strm.pipe(concat(function (types) {
var arr = types.map(function (x) { return x._id })
t.equal(types.length, 2)
t.notEqual(arr.indexOf('fire'), -1)
t.notEqual(arr.indexOf('water'), -1)
t.end()
}))
strm.on('error', function (err) {
// Aggregation cursors are only supported on mongodb 2.6+
// this shouldn't fail the tests for other versions of mongodb
if (err.message === 'unrecognized field "cursor') t.ok(1)
else t.fail(err)
t.end()
})
})
})