deno.land / x / mongoose@6.7.5 / test / shard.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
'use strict';
const start = require('./common');
const assert = require('assert');const random = require('./util').random;
const mongoose = start.mongoose;const Schema = mongoose.Schema;
const uri = process.env.MONGOOSE_SHARD_TEST_URI;const redColorEscapeCharacter = '\x1b[31m';const colorResetEscapeCharacter = '\u001b[39m';
if (!uri) { console.log([ redColorEscapeCharacter, 'You\'re not testing shards!', 'Please set the MONGOOSE_SHARD_TEST_URI env variable.', 'e.g: `mongodb://localhost:27017/database', 'Sharding must already be enabled on your database', colorResetEscapeCharacter ].join('\n'));
return;}
const schema = new Schema({ name: String, age: Number, likes: [String]}, { shardkey: { name: 1, age: 1 } });
// to keep mongodb happy when sharding the collection// we add a matching indexschema.index({ name: 1, age: 1 });
const collection = 'shardperson_' + random();mongoose.model('ShardPerson', schema, collection);
let version;let greaterThan20x;let db;describe('shard', function() { before(function(done) { db = start({ uri: uri }); db.on('error', function(err) { if (/failed to connect/.test(err)) { err.message = 'Shard test error: ' + err.message + '\n' + ' Are you sure there is a db running at ' + uri + ' ?' + '\n'; } return done(err); }); db.on('open', function() { // set up a sharded test collection const P = db.model('ShardPerson', collection);
// an existing index on shard key is required before sharding P.on('index', function() { // enable sharding on our collection const cmd = {}; cmd.shardcollection = db.name + '.' + collection; cmd.key = P.schema.options.shardkey;
P.db.db.executeDbAdminCommand(cmd, function(err) { assert.ifError(err);
db.db.admin().serverStatus(function(err, info) { db.close(); assert.ifError(err); version = info.version.split('.').map(function(n) { return parseInt(n, 10); }); greaterThan20x = version[0] > 2 || version[0] === 2 && version[0] > 0; done(); }); }); }); }); });
it('can read and write to a shard', function(done) { const db = start({ uri: uri }); const P = db.model('ShardPerson', collection);
P.create({ name: 'ryu', age: 25, likes: ['street fighting'] }, function(err, ryu) { assert.ifError(err); P.findById(ryu._id, function(err, doc) { db.close(); assert.ifError(err); assert.equal(doc.id, ryu.id); done(); }); }); });
it('save() and remove() works with shard keys transparently', function(done) { const db = start({ uri: uri }); const P = db.model('ShardPerson', collection);
const zangief = new P({ name: 'Zangief', age: 33 }); zangief.save(function(err) { assert.ifError(err);
assert.equal(zangief.$__.shardval.name, 'Zangief'); assert.equal(zangief.$__.shardval.age, 33);
P.findById(zangief._id, function(err, zang) { assert.ifError(err);
assert.equal(zang.$__.shardval.name, 'Zangief'); assert.equal(zang.$__.shardval.age, 33);
zang.likes = ['spinning', 'laughing']; zang.save(function(err) { assert.ifError(err);
assert.equal(zang.$__.shardval.name, 'Zangief'); assert.equal(zang.$__.shardval.age, 33);
zang.likes.addToSet('winning'); zang.save(function(err) { assert.ifError(err); assert.equal(zang.$__.shardval.name, 'Zangief'); assert.equal(zang.$__.shardval.age, 33); zang.remove(function(err) { db.close(); assert.ifError(err); done(); }); }); }); }); }); });
it('inserting to a sharded collection without the full shard key fails', function(done) { const db = start({ uri: uri }); const P = db.model('ShardPerson', collection);
let pending = 6;
P.create({ name: 'ryu', likes: ['street fighting'] }, function(err) { assert.ok(err); assert.ok(err.message); if (!--pending) { db.close(); done(); } });
P.create({ likes: ['street fighting'] }, function(err) { assert.ok(err); assert.ok(err.message); if (!--pending) { db.close(); done(); } });
P.create({ name: 'ryu' }, function(err) { assert.ok(err); assert.ok(err.message); if (!--pending) { db.close(); done(); } });
P.create({ age: 49 }, function(err) { assert.ok(err); assert.ok(err.message); if (!--pending) { db.close(); done(); } });
P.create({ likes: ['street fighting'], age: 8 }, function(err) { assert.ok(err); assert.ok(err.message); if (!--pending) { db.close(); done(); } });
const p = new P(); p.save(function(err) { assert.ok(err); assert.ok(err.message); if (!--pending) { db.close(); done(); } }); });
it('updating a sharded collection without the full shard key fails', function(done) { const db = start({ uri: uri }); const P = db.model('ShardPerson', collection);
P.create({ name: 'ken', age: 27 }, function(err, ken) { assert.ifError(err);
P.update({ name: 'ken' }, { likes: ['kicking', 'punching'] }, function(err) { assert.ok(/shard key/.test(err.message));
P.update({ _id: ken._id, name: 'ken' }, { likes: ['kicking', 'punching'] }, function(err) { // mongo 2.0.x returns: can't do non-multi update with query that doesn't have a valid shard key if (greaterThan20x) { assert.ok(!err, err); } else { assert.ok(/shard key/.test(err.message)); }
P.update({ _id: ken._id, age: 27 }, { likes: ['kicking', 'punching'] }, function(err) { // mongo 2.0.x returns: can't do non-multi update with query that doesn't have a valid shard key if (greaterThan20x) { assert.ok(!err, err); } else { assert.ok(/shard key/.test(err.message)); }
P.update({ age: 27 }, { likes: ['kicking', 'punching'] }, function(err) { db.close(); assert.ok(err); done(); }); }); }); }); }); });
it('updating shard key values fails', function(done) { const db = start({ uri: uri }); const P = db.model('ShardPerson', collection); P.create({ name: 'chun li', age: 19, likes: ['street fighting'] }, function(err, chunli) { assert.ifError(err);
assert.equal(chunli.$__.shardval.name, 'chun li'); assert.equal(chunli.$__.shardval.age, 19);
chunli.age = 20; chunli.save(function(err) { assert.ok(/^After applying the update to the document/.test(err.message));
assert.equal(chunli.$__.shardval.name, 'chun li'); assert.equal(chunli.$__.shardval.age, 19);
P.findById(chunli._id, function(err, chunli) { assert.ifError(err);
assert.equal(chunli.$__.shardval.name, 'chun li'); assert.equal(chunli.$__.shardval.age, 19);
chunli.name = 'chuuuun liiiii'; chunli.save(function(err) { db.close(); assert.ok(/^After applying the update to the document/.test(err.message)); done(); }); }); }); }); });
it('allows null shard key values', function(done) { const db = start({ uri: uri }); const P = db.model('ShardPerson', collection);
P.create({ name: null, age: 27 }, function(err, ken) { assert.ifError(err); P.findById(ken, function(err) { assert.ifError(err); done(); }); }); });
after(function(done) { const db = start({ uri: uri }); const P = db.model('ShardPerson', collection); P.collection.drop(function() { db.close(); done(); }); });});
mongoose

Version Info

Tagged at
a year ago