forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.aggregate.test.js
139 lines (118 loc) · 3.7 KB
/
model.aggregate.test.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Test dependencies.
*/
var start = require('./common')
, mongoose = start.mongoose
, assert = require('assert')
, random = require('../lib/utils').random
, Query = require('../lib/query')
, Aggregate = require('../lib/aggregate')
, Schema = mongoose.Schema
, SchemaType = mongoose.SchemaType
, ObjectId = Schema.Types.ObjectId
, DocumentObjectId = mongoose.Types.ObjectId
, DocumentArray = mongoose.Types.DocumentArray
, EmbeddedDocument = mongoose.Types.Embedded
, MongooseArray = mongoose.Types.Array
, MongooseError = mongoose.Error;
/**
* Setup.
*/
var userSchema = new Schema({
name: String
, age: Number
});
var collection = 'aggregate_' + random();
mongoose.model('Aggregate', userSchema);
describe('model aggregate', function(){
var group = { $group: { _id: null, maxAge: { $max: '$age' } }}
var project = { $project: { maxAge: 1, _id: 0 }};
var db, A, maxAge;
var mongo26_or_greater = false;
before(function(done){
db = start()
A = db.model('Aggregate', collection)
var authors = 'guillermo nathan tj damian marco'.split(' ');
var num = 10;
var docs = [];
maxAge = 0;
for (var i = 0; i< num; ++i) {
var age = Math.random() * 100 | 0;
maxAge = Math.max(maxAge, age);
docs.push({ author: authors[i%authors.length], age: age });
}
A.create(docs, function (err) {
assert.ifError(err);
start.mongodVersion(function (err, version) {
if (err) throw err;
mongo26_or_greater = 2 < version[0] || (2 == version[0] && 6 <= version[1]);
if (!mongo26_or_greater) console.log('not testing mongodb 2.6 features');
done();
});
});
})
describe('works', function(done){
it('with argument lists', function(done){
this.timeout(4000);
A.aggregate(group, project, function (err, res) {
assert.ifError(err);
assert.ok(res);
assert.equal(1, res.length);
assert.ok('maxAge' in res[0]);
assert.equal(maxAge, res[0].maxAge);
done();
});
});
it('with arrays', function(done){
this.timeout(4000);
A.aggregate([group, project], function (err, res) {
assert.ifError(err);
assert.ok(res);
assert.equal(1, res.length);
assert.ok('maxAge' in res[0]);
assert.equal(maxAge, res[0].maxAge);
done();
});
});
it('with Aggregate syntax', function(done) {
this.timeout(4000);
var promise = A.aggregate()
.group(group.$group)
.project(project.$project)
.exec(function (err, res) {
assert.ifError(err);
assert.ok(promise instanceof mongoose.Promise);
assert.ok(res);
assert.equal(1, res.length);
assert.ok('maxAge' in res[0]);
assert.equal(maxAge, res[0].maxAge);
done();
});
});
it('when returning Aggregate', function(done) {
assert(A.aggregate(project) instanceof Aggregate);
done();
});
it('can use helper for $out', function(done) {
if (!mongo26_or_greater) {
return done();
}
this.timeout(4000);
var outputCollection = 'aggregate_output_' + random();
var promise = A.aggregate()
.group(group.$group)
.project(project.$project)
.out(outputCollection)
.exec(function(error, result) {
assert.ifError(error);
A.db.collection(outputCollection).find().toArray(function(error, documents) {
assert.ifError(error);
assert.equal(1, documents.length);
assert.ok('maxAge' in documents[0]);
assert.equal(maxAge, documents[0].maxAge);
done();
});
});
});
})
});