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

model.mapreduce.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* global emit *//** * Test dependencies. */
'use strict';
const start = require('./common');
const assert = require('assert');const random = require('./util').random;
const mongoose = start.mongoose;const Schema = mongoose.Schema;const ObjectId = Schema.Types.ObjectId;
describe('model: mapreduce:', function() { let Comments; let BlogPost; let db;
before(function() { Comments = new Schema();
Comments.add({ title: String, date: Date, body: String, comments: [Comments] });
BlogPost = new Schema({ title: String, author: String, slug: String, date: Date, meta: { date: Date, visitors: Number }, published: Boolean, mixed: {}, numbers: [Number], owners: [ObjectId], comments: [Comments] });
db = start(); });
after(async function() { await db.close(); });
beforeEach(function() { db.deleteModel(/.*/); });
it('works', async function() { const MR = db.model('MapReduce', BlogPost);
const id = new mongoose.Types.ObjectId(); const authors = 'aaron guillermo brian nathan'.split(' '); const num = 10; const docs = []; for (let i = 0; i < num; ++i) { docs.push({ author: authors[i % authors.length], owners: [id], published: true }); }
const insertedDocs = await MR.create(docs);
const magicID = insertedDocs[1]._id;
const o1 = { map: 'function() { emit(this.author, 1); }', reduce: function(k, vals) { return vals.length; } };
const { results: ret, stats } = await MR.mapReduce(o1);

assert.ok(Array.isArray(ret)); assert.ok(stats); ret.forEach(function(res) { if (res._id === 'aaron') { assert.equal(res.value, 3); } if (res._id === 'guillermo') { assert.equal(res.value, 3); } if (res._id === 'brian') { assert.equal(res.value, 2); } if (res._id === 'nathan') { assert.equal(res.value, 2); } });
const o2 = { map: function() { emit(this.author, 1); }, reduce: function(k, vals) { return vals.length; }, query: { author: 'aaron', published: 1, owners: id } };
const res2 = await MR.mapReduce(o2);
const ret2 = res2.results; const stats2 = res2.stats;
assert.ok(Array.isArray(ret2)); assert.equal(ret2.length, 1); assert.equal(ret2[0]._id, 'aaron'); assert.equal(ret2[0].value, 3); assert.ok(stats2);
const o3 = { map: function() { emit(this.author, { own: magicID }); }, scope: { magicID: magicID }, reduce: function(k, vals) { return { own: vals[0].own, count: vals.length }; }, out: { replace: '_mapreduce_test_' + random() } };
const res3 = await MR.mapReduce(o3);
const model = res3.model;
// ret is a model assert.equal(typeof model.findOne, 'function'); assert.equal(typeof model.mapReduce, 'function');
// queries work const docs3 = await model.where('value.count').gt(1).sort({ _id: 1 }).exec();
assert.equal(docs3[0]._id, 'aaron'); assert.equal(docs3[1]._id, 'brian'); assert.equal(docs3[2]._id, 'guillermo'); assert.equal(docs3[3]._id, 'nathan');
// update casting works const doc2 = await model.findOneAndUpdate({ _id: 'aaron' }, { published: true }, { new: true });
assert.ok(doc2); assert.equal(doc2._id, 'aaron'); assert.equal(doc2.published, true);
// ad-hoc population works const doc3 = await model .findOne({ _id: 'aaron' }) .populate({ path: 'value.own', model: 'MapReduce', strictPopulate: false }) .exec();
assert.equal(doc3.value.own.author, 'guillermo'); });
it('withholds stats with false verbosity', async function() { const MR = db.model('MapReduce', BlogPost);
const o = { map: function() { }, reduce: function() { return 'test'; }, verbose: false };
assert.deepEqual(await MR.mapReduce(o), []); assert.equal(typeof stats, 'undefined'); });
describe('promises (gh-1628)', function() { it('are returned', function() { const MR = db.model('MapReduce', BlogPost);
const o = { map: function() { }, reduce: function() { return 'test'; } };
const promise = MR.mapReduce(o); assert.ok(promise instanceof mongoose.Promise); }); });
describe('with promises', function() { let MR; let db; let magicID; let id; const docs = [];
before(async function() { db = start(); MR = db.model('MapReduce', BlogPost);
id = new mongoose.Types.ObjectId(); const authors = 'aaron guillermo brian nathan'.split(' '); const num = 10; for (let i = 0; i < num; ++i) { docs.push({ author: authors[i % authors.length], owners: [id], published: true }); }
const [b] = await MR.create(docs); magicID = b._id; });
after(async function() { await db.close(); });
it('works', function() { const o = { map: function() { emit(this.author, 1); }, reduce: function(k, vals) { return vals.length; } };
return MR.mapReduce(o).then(function(res) { const ret = res.results; assert.ok(Array.isArray(ret)); ret.forEach(function(res) { if (res._id === 'aaron') { assert.equal(res.value, 6); } if (res._id === 'guillermo') { assert.equal(res.value, 6); } if (res._id === 'brian') { assert.equal(res.value, 4); } if (res._id === 'nathan') { assert.equal(res.value, 4); } }); }); });
it('works with model', function() { const o = { map: function() { emit(this.author, { own: magicID }); }, scope: { magicID: magicID }, reduce: function(k, vals) { return { own: vals[0].own, count: vals.length }; }, out: { replace: '_mapreduce_test_' + random() } };
return MR.mapReduce(o). then(function(res) { const ret = res.model; // ret is a model assert.ok(!Array.isArray(ret)); assert.equal(typeof ret.findOne, 'function'); assert.equal(typeof ret.mapReduce, 'function');
// queries work return ret.where('value.count').gt(1).sort({ _id: 1 }); }). then(function(docs) { assert.equal(docs[0]._id, 'aaron'); assert.equal(docs[1]._id, 'brian'); assert.equal(docs[2]._id, 'guillermo'); assert.equal(docs[3]._id, 'nathan'); }); }); });
it('withholds stats with false verbosity using then', async function() { const MR = db.model('MapReduce', BlogPost);
const o = { map: function() { }, reduce: function() { return 'test'; }, verbose: false };
const { stats } = await MR.mapReduce(o); assert.equal(typeof stats, 'undefined'); });});
mongoose

Version Info

Tagged at
a year ago