mongoid 7.0.0.beta → 7.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/config/locales/en.yml +21 -0
- data/lib/mongoid.rb +1 -1
- data/lib/mongoid/association/embedded/batchable.rb +39 -13
- data/lib/mongoid/association/many.rb +4 -0
- data/lib/mongoid/association/referenced/has_many.rb +2 -0
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +38 -7
- data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -2
- data/lib/mongoid/association/referenced/syncable.rb +1 -1
- data/lib/mongoid/association/touchable.rb +1 -1
- data/lib/mongoid/atomic.rb +3 -3
- data/lib/mongoid/atomic/modifiers.rb +12 -8
- data/lib/mongoid/attributes.rb +20 -12
- data/lib/mongoid/attributes/dynamic.rb +2 -2
- data/lib/mongoid/changeable.rb +1 -1
- data/lib/mongoid/clients.rb +2 -0
- data/lib/mongoid/clients/sessions.rb +113 -0
- data/lib/mongoid/clients/storage_options.rb +1 -0
- data/lib/mongoid/composable.rb +1 -0
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +6 -3
- data/lib/mongoid/contextual/geo_near.rb +1 -1
- data/lib/mongoid/contextual/map_reduce.rb +6 -2
- data/lib/mongoid/contextual/memory.rb +25 -2
- data/lib/mongoid/contextual/mongo.rb +33 -14
- data/lib/mongoid/copyable.rb +2 -2
- data/lib/mongoid/criteria.rb +1 -0
- data/lib/mongoid/criteria/queryable/extensions/string.rb +1 -1
- data/lib/mongoid/criteria/queryable/mergeable.rb +3 -1
- data/lib/mongoid/errors.rb +1 -0
- data/lib/mongoid/errors/invalid_session_use.rb +24 -0
- data/lib/mongoid/extensions.rb +0 -4
- data/lib/mongoid/extensions/hash.rb +5 -2
- data/lib/mongoid/factory.rb +14 -5
- data/lib/mongoid/fields.rb +2 -2
- data/lib/mongoid/indexable.rb +4 -4
- data/lib/mongoid/matchable/and.rb +1 -1
- data/lib/mongoid/matchable/elem_match.rb +9 -3
- data/lib/mongoid/persistable.rb +2 -2
- data/lib/mongoid/persistable/creatable.rb +4 -2
- data/lib/mongoid/persistable/deletable.rb +4 -2
- data/lib/mongoid/persistable/destroyable.rb +1 -5
- data/lib/mongoid/persistable/updatable.rb +2 -2
- data/lib/mongoid/persistable/upsertable.rb +2 -1
- data/lib/mongoid/persistence_context.rb +5 -4
- data/lib/mongoid/query_cache.rb +5 -3
- data/lib/mongoid/reloadable.rb +1 -1
- data/lib/mongoid/serializable.rb +1 -1
- data/lib/mongoid/shardable.rb +1 -1
- data/lib/mongoid/tasks/database.rb +3 -2
- data/lib/mongoid/threaded.rb +38 -0
- data/lib/mongoid/traversable.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +4 -0
- data/spec/app/models/band.rb +1 -0
- data/spec/app/models/shipment_address.rb +1 -0
- data/spec/mongoid/association/macros_spec.rb +20 -0
- data/spec/mongoid/association/referenced/has_many/eager_spec.rb +15 -0
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +229 -0
- data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
- data/spec/mongoid/atomic_spec.rb +17 -17
- data/spec/mongoid/attributes_spec.rb +38 -2
- data/spec/mongoid/clients/sessions_spec.rb +325 -0
- data/spec/mongoid/contextual/memory_spec.rb +19 -0
- data/spec/mongoid/contextual/mongo_spec.rb +133 -0
- data/spec/mongoid/copyable_spec.rb +34 -0
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +43 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +32 -3
- data/spec/mongoid/extensions/hash_spec.rb +18 -1
- data/spec/mongoid/factory_spec.rb +11 -0
- data/spec/mongoid/interceptable_spec.rb +3 -1
- data/spec/mongoid/matchable/elem_match_spec.rb +20 -0
- data/spec/mongoid/persistable/deletable_spec.rb +19 -0
- data/spec/mongoid/persistable/destroyable_spec.rb +19 -0
- data/spec/mongoid/persistable/savable_spec.rb +2 -2
- data/spec/mongoid/persistable/updatable_spec.rb +2 -2
- data/spec/mongoid/persistable_spec.rb +31 -16
- data/spec/mongoid/persistence_context_spec.rb +14 -0
- data/spec/mongoid/positional_spec.rb +10 -10
- data/spec/mongoid/query_cache_spec.rb +2 -16
- data/spec/mongoid/shardable_spec.rb +32 -12
- data/spec/spec_helper.rb +74 -0
- metadata +456 -446
- metadata.gz.sig +0 -0
@@ -42,7 +42,7 @@ describe Mongoid::Attributes do
|
|
42
42
|
context "accessing via []" do
|
43
43
|
|
44
44
|
it "does not raise an error" do
|
45
|
-
expect(from_db["desc"]).to eq("
|
45
|
+
expect(from_db["desc"]).to eq("test")
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -150,6 +150,17 @@ describe Mongoid::Attributes do
|
|
150
150
|
|
151
151
|
describe "#[]" do
|
152
152
|
|
153
|
+
context 'when the document has a custom attribute type' do
|
154
|
+
|
155
|
+
let(:bar) do
|
156
|
+
Bar.create(lat_lng: LatLng.new(52.30, 13.25))
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'returns the demongoized version of the attribute' do
|
160
|
+
expect(bar.reload[:lat_lng]).to be_a(LatLng)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
153
164
|
context "when the document is a new record" do
|
154
165
|
|
155
166
|
let(:person) do
|
@@ -165,7 +176,7 @@ describe Mongoid::Attributes do
|
|
165
176
|
context "when passing just the name" do
|
166
177
|
|
167
178
|
it "returns the full value" do
|
168
|
-
expect(person[:desc]).to eq("
|
179
|
+
expect(person[:desc]).to eq("testing")
|
169
180
|
end
|
170
181
|
end
|
171
182
|
|
@@ -268,6 +279,20 @@ describe Mongoid::Attributes do
|
|
268
279
|
|
269
280
|
describe "#[]=" do
|
270
281
|
|
282
|
+
context 'when the document has a custom attribute type' do
|
283
|
+
|
284
|
+
let(:bar) do
|
285
|
+
Bar.new.tap do |b|
|
286
|
+
b[:lat_lng] = LatLng.new(52.30, 13.25)
|
287
|
+
b.save
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'sets the demongoized version of the attribute' do
|
292
|
+
expect(bar.reload.lat_lng).to be_a(LatLng)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
271
296
|
let(:person) do
|
272
297
|
Person.new
|
273
298
|
end
|
@@ -768,6 +793,17 @@ describe Mongoid::Attributes do
|
|
768
793
|
|
769
794
|
describe "#read_attribute" do
|
770
795
|
|
796
|
+
context 'when the document has a custom attribute type' do
|
797
|
+
|
798
|
+
let(:bar) do
|
799
|
+
Bar.create(lat_lng: LatLng.new(52.30, 13.25))
|
800
|
+
end
|
801
|
+
|
802
|
+
it 'returns the demongoized version of the attribute' do
|
803
|
+
expect(bar.reload.read_attribute(:lat_lng)).to be_a(LatLng)
|
804
|
+
end
|
805
|
+
end
|
806
|
+
|
771
807
|
context "when the document is a new record" do
|
772
808
|
|
773
809
|
let(:person) do
|
@@ -0,0 +1,325 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Clients::Sessions do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
CONFIG[:clients][:other] = CONFIG[:clients][:default].dup
|
7
|
+
CONFIG[:clients][:other][:database] = 'other'
|
8
|
+
Mongoid::Clients.clients.values.each(&:close)
|
9
|
+
Mongoid::Config.send(:clients=, CONFIG[:clients])
|
10
|
+
Mongoid::Clients.with_name(:other).subscribe(Mongo::Monitoring::COMMAND, EventSubscriber.new)
|
11
|
+
end
|
12
|
+
|
13
|
+
after(:all) do
|
14
|
+
Mongoid::Clients.with_name(:other).close
|
15
|
+
Mongoid::Clients.clients.delete(:other)
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:subscriber) do
|
19
|
+
Mongoid::Clients.with_name(:other).instance_variable_get(:@monitoring).subscribers['Command'].find do |s|
|
20
|
+
s.is_a?(EventSubscriber)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:insert_events) do
|
25
|
+
subscriber.started_events.select { |event| event.command_name == :insert }
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:update_events) do
|
29
|
+
subscriber.started_events.select { |event| event.command_name == :update }
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when a session is used on a model class' do
|
33
|
+
|
34
|
+
context 'when sessions are supported', if: sessions_supported? do
|
35
|
+
|
36
|
+
around do |example|
|
37
|
+
Mongoid::Clients.with_name(:other).database.collections.each(&:drop)
|
38
|
+
subscriber.clear_events!
|
39
|
+
Person.with(client: :other) do
|
40
|
+
example.run
|
41
|
+
end
|
42
|
+
Mongoid::Clients.with_name(:other).database.collections.each(&:drop)
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when another thread is started' do
|
46
|
+
|
47
|
+
let!(:last_use_diff) do
|
48
|
+
Person.with_session do |session|
|
49
|
+
Person.create
|
50
|
+
Person.create
|
51
|
+
last_use = session.instance_variable_get(:@server_session).last_use
|
52
|
+
Thread.new { Person.create }.value
|
53
|
+
session.instance_variable_get(:@server_session).last_use - last_use
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'does not use the session for that thread' do
|
58
|
+
expect(Person.count).to be(2)
|
59
|
+
expect(Person.with(client: :default) { Person.count }).to be(1)
|
60
|
+
lsids_sent = insert_events.collect { |event| event.command['lsid'] }
|
61
|
+
expect(lsids_sent.size).to eq(2)
|
62
|
+
expect(lsids_sent.uniq.size).to eq(1)
|
63
|
+
expect(last_use_diff).to eq(0)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when the operations in the session block are all on the class' do
|
68
|
+
|
69
|
+
before do
|
70
|
+
Person.with_session do
|
71
|
+
Person.create
|
72
|
+
Person.create
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'uses a single session id for all operations on the class' do
|
77
|
+
expect(Person.count).to be(2)
|
78
|
+
lsids_sent = insert_events.collect { |event| event.command['lsid'] }
|
79
|
+
expect(lsids_sent.size).to eq(2)
|
80
|
+
expect(lsids_sent.uniq.size).to eq(1)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'when the operations in the session block are also on another class' do
|
85
|
+
|
86
|
+
context 'when the other class uses the same client' do
|
87
|
+
|
88
|
+
before do
|
89
|
+
Post.with(client: :other) do
|
90
|
+
Person.with_session do
|
91
|
+
Person.create
|
92
|
+
Person.create
|
93
|
+
Post.create
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'uses a single session id for all operations on the class' do
|
99
|
+
expect(Post.with(client: :other) { |klass| klass.count }).to be(1)
|
100
|
+
lsids_sent = insert_events.collect { |event| event.command['lsid'] }
|
101
|
+
expect(lsids_sent.size).to eq(3)
|
102
|
+
expect(lsids_sent.uniq.size).to eq(1)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'when the other class uses a different client' do
|
107
|
+
|
108
|
+
let!(:error) do
|
109
|
+
e = nil
|
110
|
+
begin
|
111
|
+
Person.with_session do
|
112
|
+
Person.create
|
113
|
+
Person.create
|
114
|
+
Post.create
|
115
|
+
end
|
116
|
+
rescue => ex
|
117
|
+
e = ex
|
118
|
+
end
|
119
|
+
e
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'raises an error' do
|
123
|
+
expect(error).to be_a(Mongoid::Errors::InvalidSessionUse)
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'uses a single session id for all operations on the class' do
|
127
|
+
expect(Person.count).to be(2)
|
128
|
+
lsids_sent = insert_events.collect { |event| event.command['lsid'] }
|
129
|
+
expect(lsids_sent.size).to eq(2)
|
130
|
+
expect(lsids_sent.uniq.size).to eq(1)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'when sessions are nested' do
|
135
|
+
|
136
|
+
let!(:error) do
|
137
|
+
e = nil
|
138
|
+
begin
|
139
|
+
Person.with_session do
|
140
|
+
Person.with_session do
|
141
|
+
Person.create
|
142
|
+
Post.create
|
143
|
+
end
|
144
|
+
end
|
145
|
+
rescue => ex
|
146
|
+
e = ex
|
147
|
+
end
|
148
|
+
e
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'raises an error' do
|
152
|
+
expect(error).to be_a(Mongoid::Errors::InvalidSessionUse)
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'does not execute any operations' do
|
156
|
+
expect(Person.count).to be(0)
|
157
|
+
expect(insert_events).to be_empty
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'when sessions are not supported', unless: sessions_supported? do
|
164
|
+
|
165
|
+
let!(:error) do
|
166
|
+
e = nil
|
167
|
+
begin
|
168
|
+
Person.with_session {}
|
169
|
+
rescue => ex
|
170
|
+
e = ex
|
171
|
+
end
|
172
|
+
e
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'raises a sessions not supported error' do
|
176
|
+
expect(error).to be_a(Mongoid::Errors::InvalidSessionUse)
|
177
|
+
expect(error.message).to include('not supported')
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context 'when a session is used on a model instance' do
|
183
|
+
|
184
|
+
let!(:person) do
|
185
|
+
Person.with(client: :other) do |klass|
|
186
|
+
klass.create
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'when sessions are supported', if: sessions_supported? do
|
191
|
+
|
192
|
+
around do |example|
|
193
|
+
Mongoid::Clients.with_name(:other).database.collections.each(&:drop)
|
194
|
+
subscriber.clear_events!
|
195
|
+
person.with(client: :other) do
|
196
|
+
example.run
|
197
|
+
end
|
198
|
+
Mongoid::Clients.with_name(:other).database.collections.each(&:drop)
|
199
|
+
end
|
200
|
+
|
201
|
+
context 'when the operations in the session block are all on the instance' do
|
202
|
+
|
203
|
+
before do
|
204
|
+
person.with_session do
|
205
|
+
person.username = 'Emily'
|
206
|
+
person.save
|
207
|
+
person.age = 80
|
208
|
+
person.save
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'uses a single session id for all operations on the class' do
|
213
|
+
expect(person.reload.username).to eq('Emily')
|
214
|
+
expect(person.reload.age).to eq(80)
|
215
|
+
lsids_sent = update_events.collect { |event| event.command['lsid'] }
|
216
|
+
expect(lsids_sent.size).to eq(2)
|
217
|
+
expect(lsids_sent.uniq.size).to eq(1)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context 'when the operations in the session block are also on another class' do
|
222
|
+
|
223
|
+
context 'when the other class uses the same client' do
|
224
|
+
|
225
|
+
before do
|
226
|
+
Post.with(client: :other) do
|
227
|
+
person.with_session do
|
228
|
+
person.username = 'Emily'
|
229
|
+
person.save
|
230
|
+
person.posts << Post.create
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
it 'uses a single session id for all operations on the class' do
|
236
|
+
expect(person.reload.username).to eq('Emily')
|
237
|
+
expect(Post.with(client: :other) { Post.count }).to be(1)
|
238
|
+
update_lsids_sent = update_events.collect { |event| event.command['lsid'] }
|
239
|
+
expect(update_lsids_sent.size).to eq(3) # person update, counter cache, post assignment
|
240
|
+
expect(update_lsids_sent.uniq.size).to eq(1) # person update, counter cache, post assignment
|
241
|
+
insert_lsids_sent = insert_events.collect { |event| event.command['lsid'] }
|
242
|
+
expect(insert_lsids_sent.size).to eq(2)
|
243
|
+
expect(insert_lsids_sent.uniq.size).to eq(1)
|
244
|
+
expect(update_lsids_sent.uniq).to eq(insert_lsids_sent.uniq)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
context 'when the other class uses a different client' do
|
249
|
+
|
250
|
+
let!(:error) do
|
251
|
+
e = nil
|
252
|
+
begin
|
253
|
+
person.with_session do
|
254
|
+
person.username = 'Emily'
|
255
|
+
person.save
|
256
|
+
person.posts << Post.create
|
257
|
+
end
|
258
|
+
rescue => ex
|
259
|
+
e = ex
|
260
|
+
end
|
261
|
+
e
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'raises an error' do
|
265
|
+
expect(error).to be_a(Mongoid::Errors::InvalidSessionUse)
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'uses a single session id for all operations on the class' do
|
269
|
+
expect(person.reload.username).to eq('Emily')
|
270
|
+
expect(Post.count).to be(0)
|
271
|
+
update_lsids_sent = update_events.collect { |event| event.command['lsid'] }
|
272
|
+
expect(update_lsids_sent.size).to eq(1)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
context 'when sessions are nested' do
|
277
|
+
|
278
|
+
let!(:error) do
|
279
|
+
e = nil
|
280
|
+
begin
|
281
|
+
person.with_session do
|
282
|
+
person.with_session do
|
283
|
+
person.username = 'Emily'
|
284
|
+
person.save
|
285
|
+
person.posts << Post.create
|
286
|
+
end
|
287
|
+
end
|
288
|
+
rescue => ex
|
289
|
+
e = ex
|
290
|
+
end
|
291
|
+
e
|
292
|
+
end
|
293
|
+
|
294
|
+
it 'raises an error' do
|
295
|
+
expect(error).to be_a(Mongoid::Errors::InvalidSessionUse)
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'does not execute any operations' do
|
299
|
+
expect(person.reload.username).not_to eq('Emily')
|
300
|
+
expect(Post.count).to be(0)
|
301
|
+
expect(update_events).to be_empty
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
context 'when sessions are not supported', unless: sessions_supported? do
|
308
|
+
|
309
|
+
let!(:error) do
|
310
|
+
e = nil
|
311
|
+
begin
|
312
|
+
person.with_session {}
|
313
|
+
rescue => ex
|
314
|
+
e = ex
|
315
|
+
end
|
316
|
+
e
|
317
|
+
end
|
318
|
+
|
319
|
+
it 'raises a sessions not supported error' do
|
320
|
+
expect(error).to be_a(Mongoid::Errors::InvalidSessionUse)
|
321
|
+
expect(error.message).to include('not supported')
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
@@ -987,6 +987,25 @@ describe Mongoid::Contextual::Memory do
|
|
987
987
|
end
|
988
988
|
end
|
989
989
|
|
990
|
+
describe '#inc' do
|
991
|
+
|
992
|
+
let(:criteria) do
|
993
|
+
Address.all.tap do |crit|
|
994
|
+
crit.documents = [ Address.new(number: 1),
|
995
|
+
Address.new(number: 2),
|
996
|
+
Address.new(number: 3) ]
|
997
|
+
end
|
998
|
+
end
|
999
|
+
|
1000
|
+
let(:context) do
|
1001
|
+
described_class.new(criteria)
|
1002
|
+
end
|
1003
|
+
|
1004
|
+
it 'increases each member' do
|
1005
|
+
expect(context.inc(number: 10).collect(&:number)).to eql([11, 12, 13])
|
1006
|
+
end
|
1007
|
+
end
|
1008
|
+
|
990
1009
|
describe "#skip" do
|
991
1010
|
|
992
1011
|
let(:hobrecht) do
|
@@ -275,6 +275,23 @@ describe Mongoid::Contextual::Mongo do
|
|
275
275
|
expect(Band.count).to eq(0)
|
276
276
|
end
|
277
277
|
end
|
278
|
+
|
279
|
+
context 'when the write concern is unacknowledged' do
|
280
|
+
|
281
|
+
let(:criteria) do
|
282
|
+
Band.all
|
283
|
+
end
|
284
|
+
|
285
|
+
let!(:deleted) do
|
286
|
+
criteria.with(write: { w: 0 }) do |crit|
|
287
|
+
crit.send(method)
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'returns 0' do
|
292
|
+
expect(deleted).to eq(0)
|
293
|
+
end
|
294
|
+
end
|
278
295
|
end
|
279
296
|
end
|
280
297
|
|
@@ -363,6 +380,27 @@ describe Mongoid::Contextual::Mongo do
|
|
363
380
|
end
|
364
381
|
end
|
365
382
|
end
|
383
|
+
|
384
|
+
context 'when the write concern is unacknowledged' do
|
385
|
+
|
386
|
+
before do
|
387
|
+
2.times { Band.create }
|
388
|
+
end
|
389
|
+
|
390
|
+
let(:criteria) do
|
391
|
+
Band.all
|
392
|
+
end
|
393
|
+
|
394
|
+
let!(:deleted) do
|
395
|
+
criteria.with(write: { w: 0 }) do |crit|
|
396
|
+
crit.send(method)
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
it 'returns 0' do
|
401
|
+
expect(deleted).to eq(0)
|
402
|
+
end
|
403
|
+
end
|
366
404
|
end
|
367
405
|
|
368
406
|
describe "#distinct" do
|
@@ -539,6 +577,31 @@ describe Mongoid::Contextual::Mongo do
|
|
539
577
|
end
|
540
578
|
end
|
541
579
|
end
|
580
|
+
|
581
|
+
context 'when the criteria has a parent document' do
|
582
|
+
|
583
|
+
before do
|
584
|
+
Post.create(person: person)
|
585
|
+
Post.create(person: person)
|
586
|
+
Post.create(person: person)
|
587
|
+
end
|
588
|
+
|
589
|
+
let(:person) do
|
590
|
+
Person.new
|
591
|
+
end
|
592
|
+
|
593
|
+
let(:criteria) do
|
594
|
+
person.posts.all
|
595
|
+
end
|
596
|
+
|
597
|
+
let(:persons) do
|
598
|
+
criteria.collect(&:person)
|
599
|
+
end
|
600
|
+
|
601
|
+
it 'sets the same parent object on each related object' do
|
602
|
+
expect(persons.uniq.size).to eq(1)
|
603
|
+
end
|
604
|
+
end
|
542
605
|
end
|
543
606
|
|
544
607
|
describe "#eager_load" do
|
@@ -2094,6 +2157,41 @@ describe Mongoid::Contextual::Mongo do
|
|
2094
2157
|
expect(context.update).to be false
|
2095
2158
|
end
|
2096
2159
|
end
|
2160
|
+
|
2161
|
+
context 'when provided array filters', if: array_filters_supported? do
|
2162
|
+
|
2163
|
+
before do
|
2164
|
+
Band.delete_all
|
2165
|
+
b = Band.new(name: 'Depeche Mode')
|
2166
|
+
b.labels << Label.new(name: 'Warner')
|
2167
|
+
b.labels << Label.new(name: 'Sony')
|
2168
|
+
b.labels << Label.new(name: 'Cbs')
|
2169
|
+
b.save
|
2170
|
+
|
2171
|
+
b = Band.new(name: 'FKA Twigs')
|
2172
|
+
b.labels << Label.new(name: 'Warner')
|
2173
|
+
b.labels << Label.new(name: 'Cbs')
|
2174
|
+
b.save
|
2175
|
+
end
|
2176
|
+
|
2177
|
+
|
2178
|
+
let(:criteria) do
|
2179
|
+
Band.where(name: 'Depeche Mode')
|
2180
|
+
end
|
2181
|
+
|
2182
|
+
let!(:update) do
|
2183
|
+
context.update({ '$set' => { 'labels.$[i].name' => 'Sony' } },
|
2184
|
+
array_filters: [{ 'i.name' => 'Cbs' }])
|
2185
|
+
end
|
2186
|
+
|
2187
|
+
it 'applies the array filters' do
|
2188
|
+
expect(Band.where(name: 'Depeche Mode').first.labels.collect(&:name)).to match_array(['Warner', 'Sony', 'Sony'])
|
2189
|
+
end
|
2190
|
+
|
2191
|
+
it 'does not affect other documents' do
|
2192
|
+
expect(Band.where(name: 'FKA Twigs').first.labels.collect(&:name)).to match_array(['Warner', 'Cbs'])
|
2193
|
+
end
|
2194
|
+
end
|
2097
2195
|
end
|
2098
2196
|
|
2099
2197
|
describe "#update_all" do
|
@@ -2231,6 +2329,41 @@ describe Mongoid::Contextual::Mongo do
|
|
2231
2329
|
expect(context.update_all).to be false
|
2232
2330
|
end
|
2233
2331
|
end
|
2332
|
+
|
2333
|
+
context 'when provided array filters', if: array_filters_supported? do
|
2334
|
+
|
2335
|
+
before do
|
2336
|
+
Band.delete_all
|
2337
|
+
b = Band.new(name: 'Depeche Mode')
|
2338
|
+
b.labels << Label.new(name: 'Warner')
|
2339
|
+
b.labels << Label.new(name: 'Sony')
|
2340
|
+
b.labels << Label.new(name: 'Cbs')
|
2341
|
+
b.save
|
2342
|
+
|
2343
|
+
b = Band.new(name: 'FKA Twigs')
|
2344
|
+
b.labels << Label.new(name: 'Warner')
|
2345
|
+
b.labels << Label.new(name: 'Cbs')
|
2346
|
+
b.save
|
2347
|
+
end
|
2348
|
+
|
2349
|
+
|
2350
|
+
let(:criteria) do
|
2351
|
+
Band.all
|
2352
|
+
end
|
2353
|
+
|
2354
|
+
let!(:update) do
|
2355
|
+
context.update_all({ '$set' => { 'labels.$[i].name' => 'Sony' } },
|
2356
|
+
array_filters: [{ 'i.name' => 'Cbs' }])
|
2357
|
+
end
|
2358
|
+
|
2359
|
+
it 'applies the array filters' do
|
2360
|
+
expect(Band.where(name: 'Depeche Mode').first.labels.collect(&:name)).to match_array(['Warner', 'Sony', 'Sony'])
|
2361
|
+
end
|
2362
|
+
|
2363
|
+
it 'updates all documents' do
|
2364
|
+
expect(Band.where(name: 'FKA Twigs').first.labels.collect(&:name)).to match_array(['Warner', 'Sony'])
|
2365
|
+
end
|
2366
|
+
end
|
2234
2367
|
end
|
2235
2368
|
|
2236
2369
|
describe '#pipeline' do
|