mongoid 4.0.2 → 5.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -1
- data/README.md +14 -13
- data/lib/config/locales/en.yml +28 -28
- data/lib/mongoid.rb +28 -21
- data/lib/mongoid/atomic.rb +2 -4
- data/lib/mongoid/attributes.rb +7 -7
- data/lib/mongoid/attributes/processing.rb +4 -1
- data/lib/mongoid/attributes/readonly.rb +2 -2
- data/lib/mongoid/changeable.rb +4 -6
- data/lib/mongoid/clients.rb +142 -0
- data/lib/mongoid/clients/factory.rb +78 -0
- data/lib/mongoid/{sessions → clients}/options.rb +30 -19
- data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
- data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
- data/lib/mongoid/clients/validators.rb +2 -0
- data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
- data/lib/mongoid/composable.rb +3 -3
- data/lib/mongoid/config.rb +39 -41
- data/lib/mongoid/config/environment.rb +1 -1
- data/lib/mongoid/config/validators.rb +1 -1
- data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +11 -11
- data/lib/mongoid/contextual/command.rb +9 -6
- data/lib/mongoid/contextual/geo_near.rb +17 -1
- data/lib/mongoid/contextual/map_reduce.rb +12 -11
- data/lib/mongoid/contextual/memory.rb +2 -5
- data/lib/mongoid/contextual/mongo.rb +92 -82
- data/lib/mongoid/contextual/none.rb +13 -0
- data/lib/mongoid/copyable.rb +6 -1
- data/lib/mongoid/criteria.rb +36 -3
- data/lib/mongoid/document.rb +3 -4
- data/lib/mongoid/errors.rb +6 -6
- data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
- data/lib/mongoid/errors/no_client_config.rb +22 -0
- data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
- data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
- data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
- data/lib/mongoid/errors/no_default_client.rb +23 -0
- data/lib/mongoid/extensions/hash.rb +5 -1
- data/lib/mongoid/extensions/object.rb +3 -2
- data/lib/mongoid/extensions/set.rb +5 -5
- data/lib/mongoid/factory.rb +4 -2
- data/lib/mongoid/fields.rb +7 -2
- data/lib/mongoid/findable.rb +4 -1
- data/lib/mongoid/indexable.rb +15 -9
- data/lib/mongoid/persistable.rb +1 -2
- data/lib/mongoid/persistable/creatable.rb +2 -2
- data/lib/mongoid/persistable/deletable.rb +3 -3
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/poppable.rb +1 -1
- data/lib/mongoid/persistable/pullable.rb +2 -2
- data/lib/mongoid/persistable/pushable.rb +2 -2
- data/lib/mongoid/persistable/renamable.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +1 -1
- data/lib/mongoid/persistable/unsettable.rb +1 -1
- data/lib/mongoid/persistable/updatable.rb +2 -2
- data/lib/mongoid/persistable/upsertable.rb +1 -1
- data/lib/mongoid/query_cache.rb +98 -104
- data/lib/mongoid/railtie.rb +1 -21
- data/lib/mongoid/railties/database.rake +1 -1
- data/lib/mongoid/relations/builders.rb +3 -1
- data/lib/mongoid/relations/counter_cache.rb +1 -1
- data/lib/mongoid/relations/embedded/batchable.rb +3 -10
- data/lib/mongoid/relations/embedded/many.rb +4 -2
- data/lib/mongoid/relations/many.rb +1 -0
- data/lib/mongoid/relations/proxy.rb +6 -6
- data/lib/mongoid/relations/referenced/many.rb +2 -1
- data/lib/mongoid/relations/targets/enumerable.rb +11 -11
- data/lib/mongoid/relations/touchable.rb +1 -1
- data/lib/mongoid/reloadable.rb +2 -2
- data/lib/mongoid/scopable.rb +6 -17
- data/lib/mongoid/selectable.rb +1 -36
- data/lib/mongoid/serializable.rb +2 -2
- data/lib/mongoid/stateful.rb +0 -1
- data/lib/mongoid/tasks/database.rake +2 -2
- data/lib/mongoid/tasks/database.rb +23 -16
- data/lib/mongoid/threaded.rb +54 -33
- data/lib/mongoid/threaded/lifecycle.rb +21 -16
- data/lib/mongoid/traversable.rb +16 -1
- data/lib/mongoid/validatable.rb +1 -1
- data/lib/mongoid/validatable/queryable.rb +1 -1
- data/lib/mongoid/validatable/uniqueness.rb +3 -20
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
- data/lib/rails/mongoid.rb +2 -2
- data/spec/app/models/audio.rb +1 -1
- data/spec/app/models/band.rb +1 -0
- data/spec/app/models/company.rb +5 -0
- data/spec/app/models/label.rb +7 -0
- data/spec/app/models/pub.rb +6 -0
- data/spec/app/models/staff.rb +7 -0
- data/spec/app/models/store_as_dup_test1.rb +5 -0
- data/spec/app/models/store_as_dup_test2.rb +5 -0
- data/spec/config/mongoid.yml +7 -25
- data/spec/mongoid/atomic/paths_spec.rb +3 -11
- data/spec/mongoid/attributes/nested_spec.rb +16 -16
- data/spec/mongoid/attributes/readonly_spec.rb +80 -18
- data/spec/mongoid/attributes_spec.rb +3 -3
- data/spec/mongoid/changeable_spec.rb +70 -0
- data/spec/mongoid/clients/factory_spec.rb +284 -0
- data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
- data/spec/mongoid/clients_spec.rb +739 -0
- data/spec/mongoid/config/environment_spec.rb +14 -11
- data/spec/mongoid/config_spec.rb +33 -48
- data/spec/mongoid/contextual/atomic_spec.rb +1 -17
- data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
- data/spec/mongoid/contextual/mongo_spec.rb +26 -83
- data/spec/mongoid/contextual/none_spec.rb +15 -0
- data/spec/mongoid/copyable_spec.rb +35 -1
- data/spec/mongoid/criteria/findable_spec.rb +197 -0
- data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
- data/spec/mongoid/criteria_spec.rb +74 -91
- data/spec/mongoid/document_spec.rb +1 -1
- data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
- data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
- data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
- data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
- data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
- data/spec/mongoid/fields/localized_spec.rb +1 -0
- data/spec/mongoid/fields_spec.rb +1 -0
- data/spec/mongoid/findable_spec.rb +2 -23
- data/spec/mongoid/indexable_spec.rb +12 -8
- data/spec/mongoid/interceptable_spec.rb +15 -0
- data/spec/mongoid/persistable/settable_spec.rb +16 -0
- data/spec/mongoid/persistable/updatable_spec.rb +3 -2
- data/spec/mongoid/persistable_spec.rb +4 -4
- data/spec/mongoid/query_cache_spec.rb +13 -8
- data/spec/mongoid/relations/auto_save_spec.rb +1 -1
- data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
- data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
- data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
- data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
- data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
- data/spec/mongoid/relations/proxy_spec.rb +28 -0
- data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
- data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
- data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
- data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
- data/spec/mongoid/reloadable_spec.rb +6 -6
- data/spec/mongoid/scopable_spec.rb +41 -28
- data/spec/mongoid/selectable_spec.rb +6 -16
- data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
- data/spec/mongoid/tasks/database_spec.rb +2 -2
- data/spec/mongoid/threaded_spec.rb +0 -7
- data/spec/mongoid/traversable_spec.rb +2 -2
- data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
- data/spec/mongoid_spec.rb +13 -15
- data/spec/rails/mongoid_spec.rb +13 -4
- data/spec/spec_helper.rb +44 -27
- data/spec/support/authorization.rb +12 -0
- data/spec/support/expectations.rb +14 -0
- metadata +52 -59
- data/lib/mongoid/contextual/find_and_modify.rb +0 -69
- data/lib/mongoid/contextual/text_search.rb +0 -178
- data/lib/mongoid/criteria/#findable.rb# +0 -141
- data/lib/mongoid/errors/no_default_session.rb +0 -23
- data/lib/mongoid/errors/no_session_config.rb +0 -22
- data/lib/mongoid/log_subscriber.rb +0 -55
- data/lib/mongoid/positional.rb +0 -71
- data/lib/mongoid/sessions.rb +0 -125
- data/lib/mongoid/sessions/factory.rb +0 -131
- data/lib/mongoid/sessions/mongo_uri.rb +0 -93
- data/lib/mongoid/sessions/validators.rb +0 -2
- data/lib/mongoid/support/query_counter.rb +0 -23
- data/spec/helpers.rb +0 -18
- data/spec/mongoid/#atomic_spec.rb# +0 -365
- data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
- data/spec/mongoid/contextual/text_search_spec.rb +0 -209
- data/spec/mongoid/log_subscriber_spec.rb +0 -75
- data/spec/mongoid/positional_spec.rb +0 -222
- data/spec/mongoid/sessions/factory_spec.rb +0 -333
- data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
- data/spec/mongoid/sessions_spec.rb +0 -1252
@@ -11,21 +11,24 @@ describe Mongoid::Config::Environment do
|
|
11
11
|
|
12
12
|
context "when using rails" do
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
context "when an environment exists" do
|
15
|
+
|
16
|
+
before do
|
17
|
+
module Rails
|
18
|
+
class << self
|
19
|
+
def env; "production"; end
|
20
|
+
end
|
18
21
|
end
|
19
22
|
end
|
20
|
-
end
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
after do
|
25
|
+
RailsTemp = Rails
|
26
|
+
Object.send(:remove_const, :Rails)
|
27
|
+
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
+
it "returns the rails environment" do
|
30
|
+
expect(described_class.env_name).to eq("production")
|
31
|
+
end
|
29
32
|
end
|
30
33
|
end
|
31
34
|
|
data/spec/mongoid/config_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe Mongoid::Config do
|
|
15
15
|
described_class.connect_to(database_id, read: :primary)
|
16
16
|
end
|
17
17
|
|
18
|
-
context "when a default
|
18
|
+
context "when a default client config exists" do
|
19
19
|
|
20
20
|
context "when a default database is configured" do
|
21
21
|
|
@@ -23,13 +23,13 @@ describe Mongoid::Config do
|
|
23
23
|
{
|
24
24
|
default: {
|
25
25
|
database: database_id,
|
26
|
-
hosts: [ "
|
26
|
+
hosts: [ "127.0.0.1:27017" ]
|
27
27
|
}
|
28
28
|
}
|
29
29
|
end
|
30
30
|
|
31
31
|
before do
|
32
|
-
described_class.
|
32
|
+
described_class.send(:clients=, config)
|
33
33
|
end
|
34
34
|
|
35
35
|
it "returns true" do
|
@@ -38,10 +38,10 @@ describe Mongoid::Config do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
context "when no default
|
41
|
+
context "when no default client config exists" do
|
42
42
|
|
43
43
|
before do
|
44
|
-
described_class.
|
44
|
+
described_class.clients.clear
|
45
45
|
end
|
46
46
|
|
47
47
|
it "returns false" do
|
@@ -73,19 +73,19 @@ describe Mongoid::Config do
|
|
73
73
|
File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
|
74
74
|
end
|
75
75
|
|
76
|
-
context "when existing
|
76
|
+
context "when existing clients exist in the configuration" do
|
77
77
|
|
78
|
-
let(:
|
79
|
-
|
78
|
+
let(:client) do
|
79
|
+
Mongo::Client.new([ "127.0.0.1:27017" ])
|
80
80
|
end
|
81
81
|
|
82
82
|
before do
|
83
|
-
Mongoid::
|
83
|
+
Mongoid::Clients.clients[:test] = client
|
84
84
|
described_class.load!(file, :test)
|
85
85
|
end
|
86
86
|
|
87
|
-
it "clears the previous
|
88
|
-
expect(Mongoid::
|
87
|
+
it "clears the previous clients" do
|
88
|
+
expect(Mongoid::Clients.clients[:test]).to be_nil
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
@@ -174,7 +174,7 @@ describe Mongoid::Config do
|
|
174
174
|
end
|
175
175
|
end
|
176
176
|
|
177
|
-
context "when
|
177
|
+
context "when client configurations are provided" do
|
178
178
|
|
179
179
|
context "when a default is provided" do
|
180
180
|
|
@@ -183,7 +183,7 @@ describe Mongoid::Config do
|
|
183
183
|
end
|
184
184
|
|
185
185
|
let(:default) do
|
186
|
-
described_class.
|
186
|
+
described_class.clients[:default]
|
187
187
|
end
|
188
188
|
|
189
189
|
it "sets the default hosts" do
|
@@ -197,25 +197,10 @@ describe Mongoid::Config do
|
|
197
197
|
end
|
198
198
|
|
199
199
|
it "sets the read option" do
|
200
|
-
expect(options["read"]).to eq("primary")
|
200
|
+
expect(options["read"]).to eq({ "mode" => "primary" })
|
201
201
|
end
|
202
202
|
end
|
203
203
|
end
|
204
|
-
|
205
|
-
context "when a secondary is provided", config: :mongohq do
|
206
|
-
|
207
|
-
before do
|
208
|
-
described_class.load!(file)
|
209
|
-
end
|
210
|
-
|
211
|
-
let(:secondary) do
|
212
|
-
described_class.sessions[:mongohq_single]
|
213
|
-
end
|
214
|
-
|
215
|
-
it "sets the secondary host" do
|
216
|
-
expect(secondary["hosts"]).to eq([ ENV["MONGOHQ_SINGLE_URL"] ])
|
217
|
-
end
|
218
|
-
end
|
219
204
|
end
|
220
205
|
end
|
221
206
|
end
|
@@ -243,66 +228,66 @@ describe Mongoid::Config do
|
|
243
228
|
end
|
244
229
|
end
|
245
230
|
|
246
|
-
describe "#
|
231
|
+
describe "#clients=" do
|
247
232
|
|
248
|
-
context "when no
|
233
|
+
context "when no clients configuration exists" do
|
249
234
|
|
250
235
|
it "raises an error" do
|
251
236
|
expect {
|
252
|
-
described_class.
|
253
|
-
}.to raise_error(Mongoid::Errors::
|
237
|
+
described_class.send(:clients=, nil)
|
238
|
+
}.to raise_error(Mongoid::Errors::NoClientsConfig)
|
254
239
|
end
|
255
240
|
end
|
256
241
|
|
257
|
-
context "when no default
|
242
|
+
context "when no default client exists" do
|
258
243
|
|
259
244
|
it "raises an error" do
|
260
245
|
expect {
|
261
|
-
described_class.
|
262
|
-
}.to raise_error(Mongoid::Errors::
|
246
|
+
described_class.send(:clients=, {})
|
247
|
+
}.to raise_error(Mongoid::Errors::NoDefaultClient)
|
263
248
|
end
|
264
249
|
end
|
265
250
|
|
266
|
-
context "when a default
|
251
|
+
context "when a default client exists" do
|
267
252
|
|
268
253
|
context "when no hosts are provided" do
|
269
254
|
|
270
|
-
let(:
|
255
|
+
let(:clients) do
|
271
256
|
{ "default" => { database: database_id }}
|
272
257
|
end
|
273
258
|
|
274
259
|
it "raises an error" do
|
275
260
|
expect {
|
276
|
-
described_class.
|
277
|
-
}.to raise_error(Mongoid::Errors::
|
261
|
+
described_class.send(:clients=, clients)
|
262
|
+
}.to raise_error(Mongoid::Errors::NoClientHosts)
|
278
263
|
end
|
279
264
|
end
|
280
265
|
|
281
266
|
context "when no database is provided" do
|
282
267
|
|
283
|
-
let(:
|
284
|
-
{ "default" => { hosts: [ "
|
268
|
+
let(:clients) do
|
269
|
+
{ "default" => { hosts: [ "127.0.0.1:27017" ] }}
|
285
270
|
end
|
286
271
|
|
287
272
|
it "raises an error" do
|
288
273
|
expect {
|
289
|
-
described_class.
|
290
|
-
}.to raise_error(Mongoid::Errors::
|
274
|
+
described_class.send(:clients=, clients)
|
275
|
+
}.to raise_error(Mongoid::Errors::NoClientDatabase)
|
291
276
|
end
|
292
277
|
end
|
293
278
|
|
294
279
|
context "when a uri and standard options are provided" do
|
295
280
|
|
296
|
-
let(:
|
281
|
+
let(:clients) do
|
297
282
|
{ "default" =>
|
298
|
-
{ hosts: [ "
|
283
|
+
{ hosts: [ "127.0.0.1:27017" ], uri: "mongodb://127.0.0.1:27017" }
|
299
284
|
}
|
300
285
|
end
|
301
286
|
|
302
287
|
it "raises an error" do
|
303
288
|
expect {
|
304
|
-
described_class.
|
305
|
-
}.to raise_error(Mongoid::Errors::
|
289
|
+
described_class.send(:clients=, clients)
|
290
|
+
}.to raise_error(Mongoid::Errors::MixedClientConfiguration)
|
306
291
|
end
|
307
292
|
end
|
308
293
|
end
|
@@ -81,10 +81,6 @@ describe Mongoid::Contextual::Atomic do
|
|
81
81
|
Band.create(likes: 60)
|
82
82
|
end
|
83
83
|
|
84
|
-
let!(:smiths) do
|
85
|
-
Band.create
|
86
|
-
end
|
87
|
-
|
88
84
|
let(:criteria) do
|
89
85
|
Band.all
|
90
86
|
end
|
@@ -102,10 +98,6 @@ describe Mongoid::Contextual::Atomic do
|
|
102
98
|
it "performs the bitwise operation on initialized fields" do
|
103
99
|
expect(depeche_mode.reload.likes).to eq(12)
|
104
100
|
end
|
105
|
-
|
106
|
-
it "does not error on non initialized fields" do
|
107
|
-
expect(smiths.reload.likes).to eq(0)
|
108
|
-
end
|
109
101
|
end
|
110
102
|
|
111
103
|
context "when performing a bitwise or" do
|
@@ -117,10 +109,6 @@ describe Mongoid::Contextual::Atomic do
|
|
117
109
|
it "performs the bitwise operation on initialized fields" do
|
118
110
|
expect(depeche_mode.reload.likes).to eq(61)
|
119
111
|
end
|
120
|
-
|
121
|
-
it "does not error on non initialized fields" do
|
122
|
-
expect(smiths.reload.likes).to eq(13)
|
123
|
-
end
|
124
112
|
end
|
125
113
|
|
126
114
|
context "when chaining bitwise operations" do
|
@@ -132,12 +120,8 @@ describe Mongoid::Contextual::Atomic do
|
|
132
120
|
it "performs the bitwise operation on initialized fields" do
|
133
121
|
expect(depeche_mode.reload.likes).to eq(14)
|
134
122
|
end
|
135
|
-
|
136
|
-
it "does not error on non initialized fields" do
|
137
|
-
expect(smiths.reload.likes).to eq(10)
|
138
|
-
end
|
139
123
|
end
|
140
|
-
end
|
124
|
+
end
|
141
125
|
|
142
126
|
describe "#inc" do
|
143
127
|
|
@@ -317,6 +317,41 @@ describe Mongoid::Contextual::GeoNear do
|
|
317
317
|
end
|
318
318
|
end
|
319
319
|
|
320
|
+
describe "#min_distance" do
|
321
|
+
|
322
|
+
let!(:collection) do
|
323
|
+
Pub.collection
|
324
|
+
end
|
325
|
+
|
326
|
+
before do
|
327
|
+
Pub.create_indexes
|
328
|
+
end
|
329
|
+
|
330
|
+
let!(:bar_one) do
|
331
|
+
Pub.create(location: [ 52.30, 13.25 ])
|
332
|
+
end
|
333
|
+
|
334
|
+
let!(:bar_two) do
|
335
|
+
Pub.create(location: [ 52.30, 13.35 ])
|
336
|
+
end
|
337
|
+
|
338
|
+
context "when results are returned" do
|
339
|
+
|
340
|
+
let(:criteria) do
|
341
|
+
Pub.all
|
342
|
+
end
|
343
|
+
|
344
|
+
let(:geo_near) do
|
345
|
+
described_class.new(collection, criteria, [ 52, 13 ]).spherical.min_distance(0.0)
|
346
|
+
end
|
347
|
+
|
348
|
+
it "sets the min distance" do
|
349
|
+
expect(geo_near.to_a).to include(bar_one)
|
350
|
+
expect(geo_near.to_a).to include(bar_two)
|
351
|
+
end
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
320
355
|
describe "#inspect" do
|
321
356
|
|
322
357
|
let!(:collection) do
|
@@ -107,7 +107,7 @@ describe Mongoid::Contextual::Mongo do
|
|
107
107
|
end
|
108
108
|
|
109
109
|
before do
|
110
|
-
expect(context.
|
110
|
+
expect(context.view).to receive(:count).once.and_return(1)
|
111
111
|
end
|
112
112
|
|
113
113
|
it "returns the count cached value after first call" do
|
@@ -164,18 +164,18 @@ describe Mongoid::Contextual::Mongo do
|
|
164
164
|
end
|
165
165
|
end
|
166
166
|
|
167
|
-
context "when provided limit
|
167
|
+
context "when provided limit" do
|
168
168
|
|
169
169
|
before do
|
170
170
|
2.times { Band.create(name: "Depeche Mode") }
|
171
171
|
end
|
172
172
|
|
173
173
|
let(:context) do
|
174
|
-
described_class.new(criteria
|
174
|
+
described_class.new(criteria)
|
175
175
|
end
|
176
176
|
|
177
177
|
let(:count) do
|
178
|
-
context.count(
|
178
|
+
context.count(limit: 2)
|
179
179
|
end
|
180
180
|
|
181
181
|
it "returns the number of documents that match" do
|
@@ -497,7 +497,7 @@ describe Mongoid::Contextual::Mongo do
|
|
497
497
|
end
|
498
498
|
|
499
499
|
it "hits the database again" do
|
500
|
-
expect(context).to receive(:
|
500
|
+
expect(context).to receive(:view).once.and_call_original
|
501
501
|
expect(context).to be_exists
|
502
502
|
end
|
503
503
|
end
|
@@ -520,7 +520,7 @@ describe Mongoid::Contextual::Mongo do
|
|
520
520
|
end
|
521
521
|
|
522
522
|
it "does not hit the database" do
|
523
|
-
expect(context).to receive(:
|
523
|
+
expect(context).to receive(:view).never
|
524
524
|
expect(context).to be_exists
|
525
525
|
end
|
526
526
|
end
|
@@ -534,7 +534,7 @@ describe Mongoid::Contextual::Mongo do
|
|
534
534
|
end
|
535
535
|
|
536
536
|
it "does not hit the database" do
|
537
|
-
expect(context).to receive(:
|
537
|
+
expect(context).to receive(:view).never
|
538
538
|
expect(context).to be_exists
|
539
539
|
end
|
540
540
|
end
|
@@ -553,11 +553,11 @@ describe Mongoid::Contextual::Mongo do
|
|
553
553
|
end
|
554
554
|
|
555
555
|
it "returns the criteria explain path" do
|
556
|
-
expect(context.explain
|
556
|
+
expect(context.explain).to_not be_empty
|
557
557
|
end
|
558
558
|
end
|
559
559
|
|
560
|
-
describe "#
|
560
|
+
describe "#find_one_and_update" do
|
561
561
|
|
562
562
|
let!(:depeche) do
|
563
563
|
Band.create(name: "Depeche Mode")
|
@@ -580,7 +580,7 @@ describe Mongoid::Contextual::Mongo do
|
|
580
580
|
end
|
581
581
|
|
582
582
|
let!(:result) do
|
583
|
-
context.
|
583
|
+
context.find_one_and_update("$inc" => { likes: 1 })
|
584
584
|
end
|
585
585
|
|
586
586
|
it "returns the first matching document" do
|
@@ -603,7 +603,7 @@ describe Mongoid::Contextual::Mongo do
|
|
603
603
|
end
|
604
604
|
|
605
605
|
let!(:result) do
|
606
|
-
context.
|
606
|
+
context.find_one_and_update("$inc" => { likes: 1 })
|
607
607
|
end
|
608
608
|
|
609
609
|
it "returns the first matching document" do
|
@@ -626,7 +626,7 @@ describe Mongoid::Contextual::Mongo do
|
|
626
626
|
end
|
627
627
|
|
628
628
|
let!(:result) do
|
629
|
-
context.
|
629
|
+
context.find_one_and_update("$inc" => { likes: 1 })
|
630
630
|
end
|
631
631
|
|
632
632
|
it "returns the first matching document" do
|
@@ -653,7 +653,7 @@ describe Mongoid::Contextual::Mongo do
|
|
653
653
|
end
|
654
654
|
|
655
655
|
let!(:result) do
|
656
|
-
context.
|
656
|
+
context.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
|
657
657
|
end
|
658
658
|
|
659
659
|
it "returns the first matching document" do
|
@@ -676,7 +676,7 @@ describe Mongoid::Contextual::Mongo do
|
|
676
676
|
end
|
677
677
|
|
678
678
|
let!(:result) do
|
679
|
-
context.
|
679
|
+
context.find_one_and_delete
|
680
680
|
end
|
681
681
|
|
682
682
|
it "returns the first matching document" do
|
@@ -702,7 +702,7 @@ describe Mongoid::Contextual::Mongo do
|
|
702
702
|
end
|
703
703
|
|
704
704
|
let(:result) do
|
705
|
-
context.
|
705
|
+
context.find_one_and_update("$inc" => { likes: 1 })
|
706
706
|
end
|
707
707
|
|
708
708
|
it "returns nil" do
|
@@ -807,7 +807,7 @@ describe Mongoid::Contextual::Mongo do
|
|
807
807
|
end
|
808
808
|
|
809
809
|
it "returns the first document without touching the database" do
|
810
|
-
expect(context).to receive(:
|
810
|
+
expect(context).to receive(:view).never
|
811
811
|
expect(context.send(method)).to eq(depeche_mode)
|
812
812
|
end
|
813
813
|
end
|
@@ -819,7 +819,7 @@ describe Mongoid::Contextual::Mongo do
|
|
819
819
|
end
|
820
820
|
|
821
821
|
it "returns the first document without touching the database" do
|
822
|
-
expect(context).to receive(:
|
822
|
+
expect(context).to receive(:view).never
|
823
823
|
expect(context.send(method)).to eq(depeche_mode)
|
824
824
|
end
|
825
825
|
end
|
@@ -845,20 +845,16 @@ describe Mongoid::Contextual::Mongo do
|
|
845
845
|
expect(context.klass).to eq(Band)
|
846
846
|
end
|
847
847
|
|
848
|
-
it "sets the
|
849
|
-
expect(context.
|
850
|
-
end
|
851
|
-
|
852
|
-
it "sets the query selector" do
|
853
|
-
expect(context.query.selector).to eq({ "name" => "Depeche Mode" })
|
848
|
+
it "sets the view" do
|
849
|
+
expect(context.view).to be_a(Mongo::Collection::View)
|
854
850
|
end
|
855
851
|
|
856
|
-
it "sets
|
857
|
-
expect(context.
|
852
|
+
it "sets the view selector" do
|
853
|
+
expect(context.view.selector).to eq({ "name" => "Depeche Mode" })
|
858
854
|
end
|
859
855
|
end
|
860
856
|
|
861
|
-
|
857
|
+
pending "#last" do
|
862
858
|
|
863
859
|
context "when no default scope" do
|
864
860
|
|
@@ -957,7 +953,7 @@ describe Mongoid::Contextual::Mongo do
|
|
957
953
|
context "when calling more than once" do
|
958
954
|
|
959
955
|
before do
|
960
|
-
expect(context.
|
956
|
+
expect(context.view).to receive(:count).once.and_return(2)
|
961
957
|
end
|
962
958
|
|
963
959
|
it "returns the cached value for subsequent calls" do
|
@@ -969,7 +965,7 @@ describe Mongoid::Contextual::Mongo do
|
|
969
965
|
|
970
966
|
before do
|
971
967
|
context.entries
|
972
|
-
expect(context.
|
968
|
+
expect(context.view).to receive(:count).once.and_return(2)
|
973
969
|
end
|
974
970
|
|
975
971
|
it "returns the cached value for all calls" do
|
@@ -1006,7 +1002,7 @@ describe Mongoid::Contextual::Mongo do
|
|
1006
1002
|
context "when calling more than once" do
|
1007
1003
|
|
1008
1004
|
before do
|
1009
|
-
expect(context.
|
1005
|
+
expect(context.view).to receive(:count).once.and_return(1)
|
1010
1006
|
end
|
1011
1007
|
|
1012
1008
|
it "returns the cached value for subsequent calls" do
|
@@ -1018,7 +1014,7 @@ describe Mongoid::Contextual::Mongo do
|
|
1018
1014
|
|
1019
1015
|
before do
|
1020
1016
|
context.entries
|
1021
|
-
expect(context.
|
1017
|
+
expect(context.view).to receive(:count).once.and_return(1)
|
1022
1018
|
end
|
1023
1019
|
|
1024
1020
|
it "returns the cached value for all calls" do
|
@@ -1517,59 +1513,6 @@ describe Mongoid::Contextual::Mongo do
|
|
1517
1513
|
end
|
1518
1514
|
end
|
1519
1515
|
|
1520
|
-
describe "#text_search" do
|
1521
|
-
|
1522
|
-
let(:criteria) do
|
1523
|
-
Word.all
|
1524
|
-
end
|
1525
|
-
|
1526
|
-
let(:context) do
|
1527
|
-
described_class.new(criteria)
|
1528
|
-
end
|
1529
|
-
|
1530
|
-
before do
|
1531
|
-
Word.with(database: "admin").mongo_session.command(setParameter: 1, textSearchEnabled: true)
|
1532
|
-
Word.create_indexes
|
1533
|
-
Word.create!(name: "phase", origin: "latin")
|
1534
|
-
end
|
1535
|
-
|
1536
|
-
after(:all) do
|
1537
|
-
Word.remove_indexes
|
1538
|
-
end
|
1539
|
-
|
1540
|
-
context "when the search is projecting" do
|
1541
|
-
|
1542
|
-
let(:search) do
|
1543
|
-
context.text_search("phase").project(name: 1)
|
1544
|
-
end
|
1545
|
-
|
1546
|
-
let(:documents) do
|
1547
|
-
search.entries
|
1548
|
-
end
|
1549
|
-
|
1550
|
-
it "limits the fields to the projection" do
|
1551
|
-
expect {
|
1552
|
-
documents.first.origin
|
1553
|
-
}.to raise_error(ActiveModel::MissingAttributeError)
|
1554
|
-
end
|
1555
|
-
end
|
1556
|
-
|
1557
|
-
context "when the search is not projecting" do
|
1558
|
-
|
1559
|
-
let(:search) do
|
1560
|
-
context.text_search("phase")
|
1561
|
-
end
|
1562
|
-
|
1563
|
-
let(:documents) do
|
1564
|
-
search.entries
|
1565
|
-
end
|
1566
|
-
|
1567
|
-
it "returns all fields" do
|
1568
|
-
expect(documents.first.origin).to eq("latin")
|
1569
|
-
end
|
1570
|
-
end
|
1571
|
-
end
|
1572
|
-
|
1573
1516
|
describe "#update" do
|
1574
1517
|
|
1575
1518
|
let!(:depeche_mode) do
|