deno.land / x / mongoose@6.7.5 / test / model.aggregate.test.js

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
140

/** * Test dependencies. */
'use strict';
const start = require('./common');
const Aggregate = require('../lib/aggregate');const assert = require('assert');const random = require('./util').random;
const mongoose = start.mongoose;const Schema = mongoose.Schema;
/** * Setup. */
const userSchema = new Schema({ name: String, age: Number});
describe('model aggregate', function() { this.timeout(4500);
const group = { $group: { _id: null, maxAge: { $max: '$age' } } }; const project = { $project: { maxAge: 1, _id: 0 } }; let db, A, maxAge;
let mongo26_or_greater = false;
before(async function() { db = start(); A = db.model('Test', userSchema);
const authors = 'guillermo nathan tj damian marco'.split(' '); const num = 10; const docs = []; maxAge = 0;

for (let i = 0; i < num; ++i) { const age = Math.random() * 100 | 0; maxAge = Math.max(maxAge, age); docs.push({ author: authors[i % authors.length], age: age }); }
await A.create(docs);
const version = await start.mongodVersion();
mongo26_or_greater = version[0] > 2 || (version[0] === 2 && version[1] >= 6);
if (!mongo26_or_greater) { console.log('not testing mongodb 2.6 features'); } });
after(async function() { await db.close(); });
describe('works', function() { it('when return promise', async function() { const res = await A.aggregate([group, project]);
assert.ok(res); assert.equal(1, res.length); assert.ok('maxAge' in res[0]); assert.equal(maxAge, res[0].maxAge); });
it('with arrays', async function() { const res = await A.aggregate([group, project]);
assert.ok(res); assert.equal(res.length, 1); assert.ok('maxAge' in res[0]); assert.equal(res[0].maxAge, maxAge); });
it('with Aggregate syntax', async function() { const res = await A.aggregate() .group(group.$group) .project(project.$project) .exec();
assert.ok(res); assert.equal(res.length, 1); assert.ok('maxAge' in res[0]); assert.equal(res[0].maxAge, maxAge); });
it('with Aggregate syntax if callback not provided', async function() { const promise = A.aggregate() .group(group.$group) .project(project.$project) .exec();
const res = await promise;
assert.ok(promise instanceof mongoose.Promise); assert.ok(res); assert.equal(res.length, 1); assert.ok('maxAge' in res[0]); assert.equal(maxAge, res[0].maxAge); });
it('when returning Aggregate', function() { assert(A.aggregate([project]) instanceof Aggregate); });
it('throws when passing object (gh-6732)', function() { assert.throws(() => A.aggregate({}), /disallows passing a spread/); });
it('can use helper for $out', async function() { if (!mongo26_or_greater) { return; }
const outputCollection = 'aggregate_output_' + random(); await A.aggregate() .group(group.$group) .project(project.$project) .out(outputCollection) .exec();
const documents = await A.db.collection(outputCollection).find().toArray();
assert.equal(documents.length, 1); assert.ok('maxAge' in documents[0]); assert.equal(maxAge, documents[0].maxAge); }); });});
mongoose

Version Info

Tagged at
a year ago