mongoid_monkey 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mongoid_monkey.rb +5 -13
- data/lib/patches/atomic.rb +5 -0
- data/lib/patches/big_decimal.rb +24 -21
- data/lib/patches/db_commands.rb +98 -28
- data/lib/patches/instrument.rb +27 -24
- data/lib/patches/reorder.rb +8 -5
- data/lib/version.rb +1 -1
- data/spec/app/models/account.rb +4 -0
- data/spec/app/models/address.rb +7 -0
- data/spec/app/models/draft.rb +7 -0
- data/spec/app/models/person.rb +11 -0
- data/spec/app/models/user.rb +6 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/unit/db_commands/mongoid3_indexes_spec.rb +408 -0
- data/spec/unit/db_commands/mongoid3_tasks_spec.rb +112 -0
- data/spec/unit/db_commands/mongoid4_indexes_spec.rb +527 -0
- data/spec/unit/db_commands/mongoid4_tasks_spec.rb +163 -0
- data/spec/unit/db_commands/moped_database_spec.rb +62 -0
- data/spec/unit/db_commands/moped_indexes_spec.rb +85 -0
- metadata +23 -3
- data/spec/unit/db_commands_spec.rb +0 -1086
@@ -1,1086 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
if defined?(Moped)
|
4
|
-
|
5
|
-
describe Moped::Database do
|
6
|
-
|
7
|
-
describe "#collection_names" do
|
8
|
-
|
9
|
-
let(:session) do
|
10
|
-
Moped::Session.new([ "127.0.0.1:27017" ], database: $database_name)
|
11
|
-
end
|
12
|
-
|
13
|
-
let(:database) do
|
14
|
-
described_class.new(session, $database_name)
|
15
|
-
end
|
16
|
-
|
17
|
-
let(:collection_names) do
|
18
|
-
database.collection_names
|
19
|
-
end
|
20
|
-
|
21
|
-
before do
|
22
|
-
session.drop
|
23
|
-
names.map do |name|
|
24
|
-
session.command(create: name)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context "when name doesn't include system" do
|
29
|
-
|
30
|
-
let(:names) do
|
31
|
-
%w[ users comments ]
|
32
|
-
end
|
33
|
-
|
34
|
-
it "returns the name of all non system collections" do
|
35
|
-
expect(collection_names.sort).to eq([ "comments", "users" ])
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
context "when name includes system not at the beginning" do
|
40
|
-
|
41
|
-
let(:names) do
|
42
|
-
%w[ users comments_system_fu ]
|
43
|
-
end
|
44
|
-
|
45
|
-
it "returns the name of all non system collections" do
|
46
|
-
expect(collection_names.sort).to eq([ "comments_system_fu", "users" ])
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context "when name includes system at the beginning" do
|
51
|
-
|
52
|
-
let(:names) do
|
53
|
-
%w[ users system_comments_fu ]
|
54
|
-
end
|
55
|
-
|
56
|
-
it "returns the name of all non system collections" do
|
57
|
-
expect(collection_names.sort).to eq([ "system_comments_fu", "users" ])
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe Moped::Indexes do
|
64
|
-
|
65
|
-
let(:session) do
|
66
|
-
Moped::Session.new %w[127.0.0.1:27017], database: $database_name
|
67
|
-
end
|
68
|
-
|
69
|
-
let(:indexes) do
|
70
|
-
session[:users].indexes
|
71
|
-
end
|
72
|
-
|
73
|
-
before do
|
74
|
-
begin
|
75
|
-
indexes.drop
|
76
|
-
rescue Exception
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe "#create" do
|
81
|
-
|
82
|
-
context "when called without extra options" do
|
83
|
-
|
84
|
-
it "creates an index with no options" do
|
85
|
-
indexes.create name: 1
|
86
|
-
indexes[name: 1].should_not eq nil
|
87
|
-
keys = []
|
88
|
-
indexes.each do |idx|
|
89
|
-
keys << idx['key']
|
90
|
-
end
|
91
|
-
keys.should eq([{"_id" => 1}, {"name" => 1}])
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
context "when called with extra options" do
|
96
|
-
|
97
|
-
it "creates an index with the extra options" do
|
98
|
-
indexes.create({name: 1}, {unique: true})
|
99
|
-
index = indexes[name: 1]
|
100
|
-
index["unique"].should eq true
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
context "when there is existent data" do
|
105
|
-
|
106
|
-
before do
|
107
|
-
3.times { session[:users].insert(name: 'John') }
|
108
|
-
end
|
109
|
-
|
110
|
-
it "raises an error" do
|
111
|
-
expect {
|
112
|
-
indexes.create({name: 1}, {unique: true})
|
113
|
-
}.to raise_error(Moped::Errors::OperationFailure)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
describe "#drop" do
|
119
|
-
|
120
|
-
context "when provided a key" do
|
121
|
-
before { session.drop }
|
122
|
-
|
123
|
-
it "drops the index" do
|
124
|
-
indexes.create(name: 1)["numIndexesAfter"].should eq 2
|
125
|
-
indexes.drop(name: 1).should eq true
|
126
|
-
indexes.create({name: 1}, {unique: true})["numIndexesAfter"].should eq 2
|
127
|
-
indexes.drop(name: 1).should eq true
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
context "when not provided a key" do
|
132
|
-
|
133
|
-
it "drops all indexes" do
|
134
|
-
indexes.create name: 1
|
135
|
-
indexes.create age: 1
|
136
|
-
indexes.drop
|
137
|
-
indexes[name: 1].should eq nil
|
138
|
-
indexes[age: 1].should eq nil
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
class Person
|
145
|
-
include Mongoid::Document
|
146
|
-
|
147
|
-
index age: 1
|
148
|
-
index addresses: 1
|
149
|
-
index dob: 1
|
150
|
-
index name: 1
|
151
|
-
index title: 1
|
152
|
-
index({ ssn: 1 }, { unique: true })
|
153
|
-
end
|
154
|
-
|
155
|
-
if defined?(Mongoid::Indexes)
|
156
|
-
|
157
|
-
describe Mongoid::Indexes do
|
158
|
-
|
159
|
-
describe ".included" do
|
160
|
-
|
161
|
-
let(:klass) do
|
162
|
-
Class.new do
|
163
|
-
include Mongoid::Indexes
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
it "adds an index_options accessor" do
|
168
|
-
klass.should respond_to(:index_options)
|
169
|
-
end
|
170
|
-
|
171
|
-
it "defaults index_options to empty hash" do
|
172
|
-
klass.index_options.should eq({})
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
describe ".remove_indexes" do
|
177
|
-
|
178
|
-
context "when no database specific options exist" do
|
179
|
-
|
180
|
-
let(:klass) do
|
181
|
-
Person
|
182
|
-
end
|
183
|
-
|
184
|
-
let(:collection) do
|
185
|
-
klass.collection
|
186
|
-
end
|
187
|
-
|
188
|
-
before do
|
189
|
-
klass.create_indexes
|
190
|
-
klass.remove_indexes
|
191
|
-
end
|
192
|
-
|
193
|
-
it "removes the indexes" do
|
194
|
-
collection.indexes.reject{ |doc| doc["name"] == "_id_" }.should be_empty
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
context "when database specific options exist" do
|
199
|
-
|
200
|
-
let(:klass) do
|
201
|
-
Class.new do
|
202
|
-
include Mongoid::Document
|
203
|
-
store_in collection: "test_db_remove"
|
204
|
-
index({ test: 1 }, { database: "mia_2" })
|
205
|
-
index({ name: 1 }, { background: true })
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
before do
|
210
|
-
klass.create_indexes
|
211
|
-
klass.remove_indexes
|
212
|
-
end
|
213
|
-
|
214
|
-
let(:indexes) do
|
215
|
-
klass.with(database: "mia_2").collection.indexes
|
216
|
-
end
|
217
|
-
|
218
|
-
it "creates the indexes" do
|
219
|
-
indexes.reject{ |doc| doc["name"] == "_id_" }.should be_empty
|
220
|
-
end
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
describe ".create_indexes" do
|
225
|
-
|
226
|
-
context "when no database options are specified" do
|
227
|
-
|
228
|
-
let(:klass) do
|
229
|
-
Class.new do
|
230
|
-
include Mongoid::Document
|
231
|
-
store_in collection: "test_class"
|
232
|
-
index({ _type: 1 }, { unique: false, background: true })
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
before do
|
237
|
-
klass.create_indexes
|
238
|
-
end
|
239
|
-
|
240
|
-
it "creates the indexes" do
|
241
|
-
klass.collection.indexes[_type: 1].should_not be_nil
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
context "when database options are specified" do
|
246
|
-
|
247
|
-
let(:klass) do
|
248
|
-
Class.new do
|
249
|
-
include Mongoid::Document
|
250
|
-
store_in collection: "test_db_indexes"
|
251
|
-
index({ _type: 1 }, { database: "mia_1" })
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
before do
|
256
|
-
klass.create_indexes
|
257
|
-
end
|
258
|
-
|
259
|
-
let(:indexes) do
|
260
|
-
klass.with(database: "mia_1").collection.indexes
|
261
|
-
end
|
262
|
-
|
263
|
-
it "creates the indexes" do
|
264
|
-
indexes[_type: 1].should_not be_nil
|
265
|
-
end
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
describe ".add_indexes" do
|
270
|
-
|
271
|
-
context "when indexes have not been added" do
|
272
|
-
|
273
|
-
let(:klass) do
|
274
|
-
Class.new do
|
275
|
-
include Mongoid::Document
|
276
|
-
def self.hereditary?
|
277
|
-
true
|
278
|
-
end
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
before do
|
283
|
-
klass.add_indexes
|
284
|
-
end
|
285
|
-
|
286
|
-
it "adds the _type index" do
|
287
|
-
klass.index_options[_type: 1].should eq(
|
288
|
-
{ unique: false, background: true }
|
289
|
-
)
|
290
|
-
end
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
describe ".index" do
|
295
|
-
|
296
|
-
let(:klass) do
|
297
|
-
Class.new do
|
298
|
-
include Mongoid::Document
|
299
|
-
field :a, as: :authentication_token
|
300
|
-
end
|
301
|
-
end
|
302
|
-
|
303
|
-
context "when indexing a field that is aliased" do
|
304
|
-
|
305
|
-
before do
|
306
|
-
klass.index({ authentication_token: 1 }, { unique: true })
|
307
|
-
end
|
308
|
-
|
309
|
-
let(:options) do
|
310
|
-
klass.index_options[a: 1]
|
311
|
-
end
|
312
|
-
|
313
|
-
it "sets the index with unique options" do
|
314
|
-
options.should eq(unique: true)
|
315
|
-
end
|
316
|
-
end
|
317
|
-
|
318
|
-
context "when providing unique options" do
|
319
|
-
|
320
|
-
before do
|
321
|
-
klass.index({ name: 1 }, { unique: true })
|
322
|
-
end
|
323
|
-
|
324
|
-
let(:options) do
|
325
|
-
klass.index_options[name: 1]
|
326
|
-
end
|
327
|
-
|
328
|
-
it "sets the index with unique options" do
|
329
|
-
options.should eq(unique: true)
|
330
|
-
end
|
331
|
-
end
|
332
|
-
|
333
|
-
context "when providing a drop_dups option" do
|
334
|
-
|
335
|
-
before do
|
336
|
-
klass.index({ name: 1 }, { drop_dups: true })
|
337
|
-
end
|
338
|
-
|
339
|
-
let(:options) do
|
340
|
-
klass.index_options[name: 1]
|
341
|
-
end
|
342
|
-
|
343
|
-
it "sets the index with dropDups options" do
|
344
|
-
options.should eq(dropDups: true)
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
context "when providing a sparse option" do
|
349
|
-
|
350
|
-
before do
|
351
|
-
klass.index({ name: 1 }, { sparse: true })
|
352
|
-
end
|
353
|
-
|
354
|
-
let(:options) do
|
355
|
-
klass.index_options[name: 1]
|
356
|
-
end
|
357
|
-
|
358
|
-
it "sets the index with sparse options" do
|
359
|
-
options.should eq(sparse: true)
|
360
|
-
end
|
361
|
-
end
|
362
|
-
|
363
|
-
context "when providing a name option" do
|
364
|
-
|
365
|
-
before do
|
366
|
-
klass.index({ name: 1 }, { name: "index_name" })
|
367
|
-
end
|
368
|
-
|
369
|
-
let(:options) do
|
370
|
-
klass.index_options[name: 1]
|
371
|
-
end
|
372
|
-
|
373
|
-
it "sets the index with name options" do
|
374
|
-
options.should eq(name: "index_name")
|
375
|
-
end
|
376
|
-
end
|
377
|
-
|
378
|
-
context "when providing database options" do
|
379
|
-
|
380
|
-
before do
|
381
|
-
klass.index({ name: 1 }, { database: "mongoid_index_alt" })
|
382
|
-
end
|
383
|
-
|
384
|
-
let(:options) do
|
385
|
-
klass.index_options[name: 1]
|
386
|
-
end
|
387
|
-
|
388
|
-
it "sets the index with background options" do
|
389
|
-
options.should eq(database: "mongoid_index_alt")
|
390
|
-
end
|
391
|
-
end
|
392
|
-
|
393
|
-
context "when providing a background option" do
|
394
|
-
|
395
|
-
before do
|
396
|
-
klass.index({ name: 1 }, { background: true })
|
397
|
-
end
|
398
|
-
|
399
|
-
let(:options) do
|
400
|
-
klass.index_options[name: 1]
|
401
|
-
end
|
402
|
-
|
403
|
-
it "sets the index with background options" do
|
404
|
-
options.should eq(background: true)
|
405
|
-
end
|
406
|
-
end
|
407
|
-
|
408
|
-
context "when providing a compound index" do
|
409
|
-
|
410
|
-
before do
|
411
|
-
klass.index({ name: 1, title: -1 })
|
412
|
-
end
|
413
|
-
|
414
|
-
let(:options) do
|
415
|
-
klass.index_options[name: 1, title: -1]
|
416
|
-
end
|
417
|
-
|
418
|
-
it "sets the compound key index" do
|
419
|
-
options.should be_empty
|
420
|
-
end
|
421
|
-
end
|
422
|
-
|
423
|
-
context "when providing a geospacial index" do
|
424
|
-
|
425
|
-
before do
|
426
|
-
klass.index({ location: "2d" }, { min: -200, max: 200, bits: 32 })
|
427
|
-
end
|
428
|
-
|
429
|
-
let(:options) do
|
430
|
-
klass.index_options[location: "2d"]
|
431
|
-
end
|
432
|
-
|
433
|
-
it "sets the geospacial index" do
|
434
|
-
options.should eq({ min: -200, max: 200, bits: 32 })
|
435
|
-
end
|
436
|
-
end
|
437
|
-
|
438
|
-
context "when providing a geo haystack index" do
|
439
|
-
|
440
|
-
before do
|
441
|
-
klass.index({ location: "geoHaystack" }, { min: -200, max: 200, bucket_size: 0.5 })
|
442
|
-
end
|
443
|
-
|
444
|
-
let(:options) do
|
445
|
-
klass.index_options[location: "geoHaystack"]
|
446
|
-
end
|
447
|
-
|
448
|
-
it "sets the geo haystack index" do
|
449
|
-
options.should eq({ min: -200, max: 200, bucketSize: 0.5 })
|
450
|
-
end
|
451
|
-
end
|
452
|
-
|
453
|
-
context "when providing a Spherical Geospatial index" do
|
454
|
-
|
455
|
-
before do
|
456
|
-
klass.index({ location: "2dsphere" })
|
457
|
-
end
|
458
|
-
|
459
|
-
let(:options) do
|
460
|
-
klass.index_options[location: "2dsphere"]
|
461
|
-
end
|
462
|
-
|
463
|
-
it "sets the spherical geospatial index" do
|
464
|
-
options.should be_empty
|
465
|
-
end
|
466
|
-
end
|
467
|
-
|
468
|
-
context "when providing a hashed index" do
|
469
|
-
|
470
|
-
before do
|
471
|
-
klass.index({ a: "hashed" })
|
472
|
-
end
|
473
|
-
|
474
|
-
let(:options) do
|
475
|
-
klass.index_options[a: "hashed"]
|
476
|
-
end
|
477
|
-
|
478
|
-
it "sets the hashed index" do
|
479
|
-
options.should be_empty
|
480
|
-
end
|
481
|
-
end
|
482
|
-
|
483
|
-
context "when providing a text index" do
|
484
|
-
|
485
|
-
before do
|
486
|
-
klass.index({ content: "text" })
|
487
|
-
end
|
488
|
-
|
489
|
-
let(:options) do
|
490
|
-
klass.index_options[content: "text"]
|
491
|
-
end
|
492
|
-
|
493
|
-
it "sets the text index" do
|
494
|
-
options.should be_empty
|
495
|
-
end
|
496
|
-
end
|
497
|
-
|
498
|
-
context "when providing a compound text index" do
|
499
|
-
|
500
|
-
before do
|
501
|
-
klass.index({ content: "text", title: "text" }, { weights: { content: 1, title: 2 } })
|
502
|
-
end
|
503
|
-
|
504
|
-
let(:options) do
|
505
|
-
klass.index_options[content: "text", title: "text"]
|
506
|
-
end
|
507
|
-
|
508
|
-
it "sets the compound text index" do
|
509
|
-
options.should eq(weights: { content: 1, title: 2 })
|
510
|
-
end
|
511
|
-
end
|
512
|
-
|
513
|
-
context "when providing an expire_after_seconds option" do
|
514
|
-
|
515
|
-
before do
|
516
|
-
klass.index({ name: 1 }, { expire_after_seconds: 3600 })
|
517
|
-
end
|
518
|
-
|
519
|
-
let(:options) do
|
520
|
-
klass.index_options[name: 1]
|
521
|
-
end
|
522
|
-
|
523
|
-
it "sets the index with sparse options" do
|
524
|
-
options.should eq(expireAfterSeconds: 3600)
|
525
|
-
end
|
526
|
-
end
|
527
|
-
|
528
|
-
context "when providing an invalid option" do
|
529
|
-
|
530
|
-
it "raises an error" do
|
531
|
-
expect {
|
532
|
-
klass.index({ name: 1 }, { invalid: true })
|
533
|
-
}.to raise_error(Mongoid::Errors::InvalidIndex)
|
534
|
-
end
|
535
|
-
end
|
536
|
-
|
537
|
-
context "when providing an invalid spec" do
|
538
|
-
|
539
|
-
context "when the spec is not a hash" do
|
540
|
-
|
541
|
-
it "raises an error" do
|
542
|
-
expect {
|
543
|
-
klass.index(:name)
|
544
|
-
}.to raise_error(Mongoid::Errors::InvalidIndex)
|
545
|
-
end
|
546
|
-
end
|
547
|
-
|
548
|
-
context "when the spec key is invalid" do
|
549
|
-
|
550
|
-
it "raises an error" do
|
551
|
-
expect {
|
552
|
-
klass.index({ name: "something" })
|
553
|
-
}.to raise_error(Mongoid::Errors::InvalidIndex)
|
554
|
-
end
|
555
|
-
end
|
556
|
-
end
|
557
|
-
end
|
558
|
-
end
|
559
|
-
end
|
560
|
-
|
561
|
-
if defined?(Mongoid::Indexable)
|
562
|
-
|
563
|
-
describe Mongoid::Indexable do
|
564
|
-
|
565
|
-
describe ".included" do
|
566
|
-
|
567
|
-
let(:klass) do
|
568
|
-
Class.new do
|
569
|
-
include Mongoid::Indexable
|
570
|
-
end
|
571
|
-
end
|
572
|
-
|
573
|
-
it "adds an index_specifications accessor" do
|
574
|
-
expect(klass).to respond_to(:index_specifications)
|
575
|
-
end
|
576
|
-
|
577
|
-
it "defaults index_specifications to empty array" do
|
578
|
-
expect(klass.index_specifications).to be_empty
|
579
|
-
end
|
580
|
-
end
|
581
|
-
|
582
|
-
describe ".remove_indexes" do
|
583
|
-
|
584
|
-
context "when no database specific options exist" do
|
585
|
-
|
586
|
-
let(:klass) do
|
587
|
-
Person
|
588
|
-
end
|
589
|
-
|
590
|
-
let(:collection) do
|
591
|
-
klass.collection
|
592
|
-
end
|
593
|
-
|
594
|
-
before do
|
595
|
-
klass.create_indexes
|
596
|
-
klass.remove_indexes
|
597
|
-
end
|
598
|
-
|
599
|
-
it "removes the indexes" do
|
600
|
-
expect(collection.indexes.reject{ |doc| doc["name"] == "_id_" }).to be_empty
|
601
|
-
end
|
602
|
-
end
|
603
|
-
|
604
|
-
context "when database specific options exist" do
|
605
|
-
|
606
|
-
let(:klass) do
|
607
|
-
Class.new do
|
608
|
-
include Mongoid::Document
|
609
|
-
store_in collection: "test_db_remove"
|
610
|
-
index({ test: 1 }, { database: "mia_2" })
|
611
|
-
index({ name: 1 }, { background: true })
|
612
|
-
end
|
613
|
-
end
|
614
|
-
|
615
|
-
before do
|
616
|
-
klass.create_indexes
|
617
|
-
klass.remove_indexes
|
618
|
-
end
|
619
|
-
|
620
|
-
let(:indexes) do
|
621
|
-
klass.with(database: "mia_2").collection.indexes
|
622
|
-
end
|
623
|
-
|
624
|
-
it "creates the indexes" do
|
625
|
-
expect(indexes.reject{ |doc| doc["name"] == "_id_" }).to be_empty
|
626
|
-
end
|
627
|
-
end
|
628
|
-
end
|
629
|
-
|
630
|
-
describe ".create_indexes" do
|
631
|
-
|
632
|
-
context "when no database options are specified" do
|
633
|
-
|
634
|
-
let(:klass) do
|
635
|
-
Class.new do
|
636
|
-
include Mongoid::Document
|
637
|
-
store_in collection: "test_class"
|
638
|
-
index({ _type: 1 }, unique: false, background: true)
|
639
|
-
end
|
640
|
-
end
|
641
|
-
|
642
|
-
before do
|
643
|
-
klass.create_indexes
|
644
|
-
end
|
645
|
-
|
646
|
-
it "creates the indexes" do
|
647
|
-
expect(klass.collection.indexes[_type: 1]).to_not be_nil
|
648
|
-
end
|
649
|
-
end
|
650
|
-
|
651
|
-
context "when database options are specified" do
|
652
|
-
|
653
|
-
let(:klass) do
|
654
|
-
Class.new do
|
655
|
-
include Mongoid::Document
|
656
|
-
store_in collection: "test_db_indexes"
|
657
|
-
index({ _type: 1 }, { database: "mia_1" })
|
658
|
-
end
|
659
|
-
end
|
660
|
-
|
661
|
-
before do
|
662
|
-
klass.create_indexes
|
663
|
-
end
|
664
|
-
|
665
|
-
let(:indexes) do
|
666
|
-
klass.with(database: "mia_1").collection.indexes
|
667
|
-
end
|
668
|
-
|
669
|
-
it "creates the indexes" do
|
670
|
-
expect(indexes[_type: 1]).to_not be_nil
|
671
|
-
end
|
672
|
-
end
|
673
|
-
end
|
674
|
-
|
675
|
-
describe ".add_indexes" do
|
676
|
-
|
677
|
-
context "when indexes have not been added" do
|
678
|
-
|
679
|
-
let(:klass) do
|
680
|
-
Class.new do
|
681
|
-
include Mongoid::Document
|
682
|
-
def self.hereditary?
|
683
|
-
true
|
684
|
-
end
|
685
|
-
end
|
686
|
-
end
|
687
|
-
|
688
|
-
before do
|
689
|
-
klass.add_indexes
|
690
|
-
end
|
691
|
-
|
692
|
-
let(:spec) do
|
693
|
-
klass.index_specification(_type: 1)
|
694
|
-
end
|
695
|
-
|
696
|
-
it "adds the _type index" do
|
697
|
-
expect(spec.options).to eq(unique: false, background: true)
|
698
|
-
end
|
699
|
-
end
|
700
|
-
end
|
701
|
-
|
702
|
-
describe ".index" do
|
703
|
-
|
704
|
-
let(:klass) do
|
705
|
-
Class.new do
|
706
|
-
include Mongoid::Document
|
707
|
-
field :a, as: :authentication_token
|
708
|
-
end
|
709
|
-
end
|
710
|
-
|
711
|
-
context "when indexing a field that is aliased" do
|
712
|
-
|
713
|
-
before do
|
714
|
-
klass.index({ authentication_token: 1 }, unique: true)
|
715
|
-
end
|
716
|
-
|
717
|
-
let(:options) do
|
718
|
-
klass.index_specification(a: 1).options
|
719
|
-
end
|
720
|
-
|
721
|
-
it "sets the index with unique options" do
|
722
|
-
expect(options).to eq(unique: true)
|
723
|
-
end
|
724
|
-
end
|
725
|
-
|
726
|
-
context "when providing unique options" do
|
727
|
-
|
728
|
-
before do
|
729
|
-
klass.index({ name: 1 }, unique: true)
|
730
|
-
end
|
731
|
-
|
732
|
-
let(:options) do
|
733
|
-
klass.index_specification(name: 1).options
|
734
|
-
end
|
735
|
-
|
736
|
-
it "sets the index with unique options" do
|
737
|
-
expect(options).to eq(unique: true)
|
738
|
-
end
|
739
|
-
end
|
740
|
-
|
741
|
-
context "when providing a drop_dups option" do
|
742
|
-
|
743
|
-
before do
|
744
|
-
klass.index({ name: 1 }, drop_dups: true)
|
745
|
-
end
|
746
|
-
|
747
|
-
let(:options) do
|
748
|
-
klass.index_specification(name: 1).options
|
749
|
-
end
|
750
|
-
|
751
|
-
it "sets the index with dropDups options" do
|
752
|
-
expect(options).to eq(dropDups: true)
|
753
|
-
end
|
754
|
-
end
|
755
|
-
|
756
|
-
context "when providing a sparse option" do
|
757
|
-
|
758
|
-
before do
|
759
|
-
klass.index({ name: 1 }, sparse: true)
|
760
|
-
end
|
761
|
-
|
762
|
-
let(:options) do
|
763
|
-
klass.index_specification(name: 1).options
|
764
|
-
end
|
765
|
-
|
766
|
-
it "sets the index with sparse options" do
|
767
|
-
expect(options).to eq(sparse: true)
|
768
|
-
end
|
769
|
-
end
|
770
|
-
|
771
|
-
context "when providing a name option" do
|
772
|
-
|
773
|
-
before do
|
774
|
-
klass.index({ name: 1 }, name: "index_name")
|
775
|
-
end
|
776
|
-
|
777
|
-
let(:options) do
|
778
|
-
klass.index_specification(name: 1).options
|
779
|
-
end
|
780
|
-
|
781
|
-
it "sets the index with name options" do
|
782
|
-
expect(options).to eq(name: "index_name")
|
783
|
-
end
|
784
|
-
end
|
785
|
-
|
786
|
-
context "when providing database options" do
|
787
|
-
|
788
|
-
before do
|
789
|
-
klass.index({ name: 1 }, database: "mongoid_index_alt")
|
790
|
-
end
|
791
|
-
|
792
|
-
let(:options) do
|
793
|
-
klass.index_specification(name: 1).options
|
794
|
-
end
|
795
|
-
|
796
|
-
it "sets the index with background options" do
|
797
|
-
expect(options).to eq(database: "mongoid_index_alt")
|
798
|
-
end
|
799
|
-
end
|
800
|
-
|
801
|
-
context "when providing a background option" do
|
802
|
-
|
803
|
-
before do
|
804
|
-
klass.index({ name: 1 }, background: true)
|
805
|
-
end
|
806
|
-
|
807
|
-
let(:options) do
|
808
|
-
klass.index_specification(name: 1).options
|
809
|
-
end
|
810
|
-
|
811
|
-
it "sets the index with background options" do
|
812
|
-
expect(options).to eq(background: true)
|
813
|
-
end
|
814
|
-
end
|
815
|
-
|
816
|
-
context "when providing a compound index" do
|
817
|
-
|
818
|
-
before do
|
819
|
-
klass.index({ name: 1, title: -1 })
|
820
|
-
end
|
821
|
-
|
822
|
-
let(:options) do
|
823
|
-
klass.index_specification(name: 1, title: -1).options
|
824
|
-
end
|
825
|
-
|
826
|
-
it "sets the compound key index" do
|
827
|
-
expect(options).to be_empty
|
828
|
-
end
|
829
|
-
end
|
830
|
-
|
831
|
-
context "when providing multiple inverse compound indexes" do
|
832
|
-
|
833
|
-
before do
|
834
|
-
klass.index({ name: 1, title: -1 })
|
835
|
-
klass.index({ title: -1, name: 1 })
|
836
|
-
end
|
837
|
-
|
838
|
-
let(:first_spec) do
|
839
|
-
klass.index_specification(name: 1, title: -1)
|
840
|
-
end
|
841
|
-
|
842
|
-
let(:second_spec) do
|
843
|
-
klass.index_specification(title: -1, name: 1)
|
844
|
-
end
|
845
|
-
|
846
|
-
it "does not overwrite the index options" do
|
847
|
-
expect(first_spec).to_not eq(second_spec)
|
848
|
-
end
|
849
|
-
end
|
850
|
-
|
851
|
-
context "when providing multiple compound indexes with different order" do
|
852
|
-
|
853
|
-
before do
|
854
|
-
klass.index({ name: 1, title: -1 })
|
855
|
-
klass.index({ name: 1, title: 1 })
|
856
|
-
end
|
857
|
-
|
858
|
-
let(:first_spec) do
|
859
|
-
klass.index_specification(name: 1, title: -1)
|
860
|
-
end
|
861
|
-
|
862
|
-
let(:second_spec) do
|
863
|
-
klass.index_specification(name: 1, title: 1)
|
864
|
-
end
|
865
|
-
|
866
|
-
it "does not overwrite the index options" do
|
867
|
-
expect(first_spec).to_not eq(second_spec)
|
868
|
-
end
|
869
|
-
end
|
870
|
-
|
871
|
-
context "when providing a geospacial index" do
|
872
|
-
|
873
|
-
before do
|
874
|
-
klass.index({ location: "2d" }, { min: -200, max: 200, bits: 32 })
|
875
|
-
end
|
876
|
-
|
877
|
-
let(:options) do
|
878
|
-
klass.index_specification(location: "2d").options
|
879
|
-
end
|
880
|
-
|
881
|
-
it "sets the geospacial index" do
|
882
|
-
expect(options).to eq({ min: -200, max: 200, bits: 32 })
|
883
|
-
end
|
884
|
-
end
|
885
|
-
|
886
|
-
context "when providing a geo haystack index" do
|
887
|
-
|
888
|
-
before do
|
889
|
-
klass.index({ location: "geoHaystack" }, { min: -200, max: 200, bucket_size: 0.5 })
|
890
|
-
end
|
891
|
-
|
892
|
-
let(:options) do
|
893
|
-
klass.index_specification(location: "geoHaystack").options
|
894
|
-
end
|
895
|
-
|
896
|
-
it "sets the geo haystack index" do
|
897
|
-
expect(options).to eq({ min: -200, max: 200, bucketSize: 0.5 })
|
898
|
-
end
|
899
|
-
end
|
900
|
-
|
901
|
-
context "when providing a Spherical Geospatial index" do
|
902
|
-
|
903
|
-
before do
|
904
|
-
klass.index({ location: "2dsphere" })
|
905
|
-
end
|
906
|
-
|
907
|
-
let(:options) do
|
908
|
-
klass.index_specification(location: "2dsphere").options
|
909
|
-
end
|
910
|
-
|
911
|
-
it "sets the spherical geospatial index" do
|
912
|
-
expect(options).to be_empty
|
913
|
-
end
|
914
|
-
end
|
915
|
-
|
916
|
-
context "when providing a text index" do
|
917
|
-
|
918
|
-
context "when the index is a single field" do
|
919
|
-
|
920
|
-
before do
|
921
|
-
klass.index({ description: "text" })
|
922
|
-
end
|
923
|
-
|
924
|
-
let(:options) do
|
925
|
-
klass.index_specification(description: "text").options
|
926
|
-
end
|
927
|
-
|
928
|
-
it "allows the set of the text index" do
|
929
|
-
expect(options).to be_empty
|
930
|
-
end
|
931
|
-
end
|
932
|
-
|
933
|
-
context "when the index is multiple fields" do
|
934
|
-
|
935
|
-
before do
|
936
|
-
klass.index({ description: "text", name: "text" })
|
937
|
-
end
|
938
|
-
|
939
|
-
let(:options) do
|
940
|
-
klass.index_specification(description: "text", name: "text").options
|
941
|
-
end
|
942
|
-
|
943
|
-
it "allows the set of the text index" do
|
944
|
-
expect(options).to be_empty
|
945
|
-
end
|
946
|
-
end
|
947
|
-
|
948
|
-
context "when the index is all string fields" do
|
949
|
-
|
950
|
-
before do
|
951
|
-
klass.index({ "$**" => "text" })
|
952
|
-
end
|
953
|
-
|
954
|
-
let(:options) do
|
955
|
-
klass.index_specification(:"$**" => "text").options
|
956
|
-
end
|
957
|
-
|
958
|
-
it "allows the set of the text index" do
|
959
|
-
expect(options).to be_empty
|
960
|
-
end
|
961
|
-
end
|
962
|
-
|
963
|
-
context "when providing a default language" do
|
964
|
-
|
965
|
-
before do
|
966
|
-
klass.index({ description: "text" }, default_language: "english")
|
967
|
-
end
|
968
|
-
|
969
|
-
let(:options) do
|
970
|
-
klass.index_specification(description: "text").options
|
971
|
-
end
|
972
|
-
|
973
|
-
it "allows the set of the text index" do
|
974
|
-
expect(options).to eq(default_language: "english")
|
975
|
-
end
|
976
|
-
end
|
977
|
-
|
978
|
-
context "when providing a name" do
|
979
|
-
|
980
|
-
before do
|
981
|
-
klass.index({ description: "text" }, name: "text_index")
|
982
|
-
end
|
983
|
-
|
984
|
-
let(:options) do
|
985
|
-
klass.index_specification(description: "text").options
|
986
|
-
end
|
987
|
-
|
988
|
-
it "allows the set of the text index" do
|
989
|
-
expect(options).to eq(name: "text_index")
|
990
|
-
end
|
991
|
-
end
|
992
|
-
end
|
993
|
-
|
994
|
-
context "when providing a hashed index" do
|
995
|
-
|
996
|
-
before do
|
997
|
-
klass.index({ a: "hashed" })
|
998
|
-
end
|
999
|
-
|
1000
|
-
let(:options) do
|
1001
|
-
klass.index_specification(a: "hashed").options
|
1002
|
-
end
|
1003
|
-
|
1004
|
-
it "sets the hashed index" do
|
1005
|
-
expect(options).to be_empty
|
1006
|
-
end
|
1007
|
-
end
|
1008
|
-
|
1009
|
-
context "when providing a text index" do
|
1010
|
-
|
1011
|
-
before do
|
1012
|
-
klass.index({ content: "text" })
|
1013
|
-
end
|
1014
|
-
|
1015
|
-
let(:options) do
|
1016
|
-
klass.index_specification(content: "text").options
|
1017
|
-
end
|
1018
|
-
|
1019
|
-
it "sets the text index" do
|
1020
|
-
expect(options).to be_empty
|
1021
|
-
end
|
1022
|
-
end
|
1023
|
-
|
1024
|
-
context "when providing a compound text index" do
|
1025
|
-
|
1026
|
-
before do
|
1027
|
-
klass.index({ content: "text", title: "text" }, { weights: { content: 1, title: 2 } })
|
1028
|
-
end
|
1029
|
-
|
1030
|
-
let(:options) do
|
1031
|
-
klass.index_specification(content: "text", title: "text").options
|
1032
|
-
end
|
1033
|
-
|
1034
|
-
it "sets the compound text index" do
|
1035
|
-
expect(options).to eq(weights: { content: 1, title: 2 })
|
1036
|
-
end
|
1037
|
-
end
|
1038
|
-
|
1039
|
-
context "when providing an expire_after_seconds option" do
|
1040
|
-
|
1041
|
-
before do
|
1042
|
-
klass.index({ name: 1 }, { expire_after_seconds: 3600 })
|
1043
|
-
end
|
1044
|
-
|
1045
|
-
let(:options) do
|
1046
|
-
klass.index_specification(name: 1).options
|
1047
|
-
end
|
1048
|
-
|
1049
|
-
it "sets the index with sparse options" do
|
1050
|
-
expect(options).to eq(expireAfterSeconds: 3600)
|
1051
|
-
end
|
1052
|
-
end
|
1053
|
-
|
1054
|
-
context "when providing an invalid option" do
|
1055
|
-
|
1056
|
-
it "raises an error" do
|
1057
|
-
expect {
|
1058
|
-
klass.index({ name: 1 }, { invalid: true })
|
1059
|
-
}.to raise_error(Mongoid::Errors::InvalidIndex)
|
1060
|
-
end
|
1061
|
-
end
|
1062
|
-
|
1063
|
-
context "when providing an invalid spec" do
|
1064
|
-
|
1065
|
-
context "when the spec is not a hash" do
|
1066
|
-
|
1067
|
-
it "raises an error" do
|
1068
|
-
expect {
|
1069
|
-
klass.index(:name)
|
1070
|
-
}.to raise_error(Mongoid::Errors::InvalidIndex)
|
1071
|
-
end
|
1072
|
-
end
|
1073
|
-
|
1074
|
-
context "when the spec key is invalid" do
|
1075
|
-
|
1076
|
-
it "raises an error" do
|
1077
|
-
expect {
|
1078
|
-
klass.index({ name: "something" })
|
1079
|
-
}.to raise_error(Mongoid::Errors::InvalidIndex)
|
1080
|
-
end
|
1081
|
-
end
|
1082
|
-
end
|
1083
|
-
end
|
1084
|
-
end
|
1085
|
-
end
|
1086
|
-
end
|