mongoid 8.0.2 → 8.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +17 -15
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +4 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -0
- data/lib/mongoid/cacheable.rb +2 -2
- data/lib/mongoid/criteria/queryable/extensions/array.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/numeric.rb +0 -8
- data/lib/mongoid/criteria/queryable/extensions/string.rb +1 -11
- data/lib/mongoid/criteria/queryable/extensions/symbol.rb +0 -10
- data/lib/mongoid/criteria/translator.rb +45 -0
- data/lib/mongoid/criteria.rb +1 -0
- data/lib/mongoid/document.rb +50 -13
- data/lib/mongoid/extensions/big_decimal.rb +4 -0
- data/lib/mongoid/extensions/float.rb +6 -2
- data/lib/mongoid/extensions/integer.rb +6 -2
- data/lib/mongoid/factory.rb +21 -8
- data/lib/mongoid/fields/localized.rb +7 -2
- data/lib/mongoid/matcher.rb +21 -6
- data/lib/mongoid/persistence_context.rb +41 -5
- data/lib/mongoid/scopable.rb +9 -7
- data/lib/mongoid/shardable.rb +35 -11
- data/lib/mongoid/threaded.rb +33 -3
- data/lib/mongoid/traversable.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/spec/integration/i18n_fallbacks_spec.rb +1 -17
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +37 -32
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +143 -197
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +102 -114
- data/spec/mongoid/attributes_spec.rb +2 -2
- data/spec/mongoid/cacheable_spec.rb +3 -3
- data/spec/mongoid/clients_spec.rb +25 -0
- data/spec/mongoid/contextual/memory_spec.rb +4 -5
- data/spec/mongoid/contextual/mongo_spec.rb +2 -4
- data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +0 -59
- data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +0 -59
- data/spec/mongoid/criteria/queryable/optional_spec.rb +15 -0
- data/spec/mongoid/criteria/translator_spec.rb +132 -0
- data/spec/mongoid/criteria_projection_spec.rb +0 -1
- data/spec/mongoid/criteria_spec.rb +1 -1
- data/spec/mongoid/extensions/big_decimal_spec.rb +15 -0
- data/spec/mongoid/extensions/float_spec.rb +10 -3
- data/spec/mongoid/extensions/integer_spec.rb +10 -3
- data/spec/mongoid/fields/localized_spec.rb +37 -12
- data/spec/mongoid/shardable_models.rb +14 -0
- data/spec/mongoid/shardable_spec.rb +153 -61
- data/spec/mongoid/validatable/uniqueness_spec.rb +0 -1
- data/spec/support/macros.rb +16 -0
- data.tar.gz.sig +0 -0
- metadata +651 -643
- metadata.gz.sig +0 -0
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Mongoid::Criteria::Translator do
|
6
|
+
describe "#to_direction" do
|
7
|
+
context "when the value is a string" do
|
8
|
+
context "when ascending" do
|
9
|
+
it "returns 1" do
|
10
|
+
expect(described_class.to_direction("ascending")).to eq(1)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when asc" do
|
15
|
+
it "returns 1" do
|
16
|
+
expect(described_class.to_direction("asc")).to eq(1)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when ASCENDING" do
|
21
|
+
it "returns 1" do
|
22
|
+
expect(described_class.to_direction("ASCENDING")).to eq(1)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when ASC" do
|
27
|
+
it "returns 1" do
|
28
|
+
expect(described_class.to_direction("ASC")).to eq(1)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when descending" do
|
33
|
+
it "returns -1" do
|
34
|
+
expect(described_class.to_direction("descending")).to eq(-1)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "when desc" do
|
39
|
+
it "returns -1" do
|
40
|
+
expect(described_class.to_direction("desc")).to eq(-1)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when DESCENDING" do
|
45
|
+
it "returns -1" do
|
46
|
+
expect(described_class.to_direction("DESCENDING")).to eq(-1)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when DESC" do
|
51
|
+
it "returns -1" do
|
52
|
+
expect(described_class.to_direction("DESC")).to eq(-1)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when the value is a symbol" do
|
58
|
+
context "when ascending" do
|
59
|
+
it "returns 1" do
|
60
|
+
expect(described_class.to_direction(:ascending)).to eq(1)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "when asc" do
|
65
|
+
it "returns 1" do
|
66
|
+
expect(described_class.to_direction(:asc)).to eq(1)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "when ASCENDING" do
|
71
|
+
it "returns 1" do
|
72
|
+
expect(described_class.to_direction(:ASCENDING)).to eq(1)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when ASC" do
|
77
|
+
it "returns 1" do
|
78
|
+
expect(described_class.to_direction(:ASC)).to eq(1)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "when descending" do
|
83
|
+
it "returns -1" do
|
84
|
+
expect(described_class.to_direction(:descending)).to eq(-1)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "when desc" do
|
89
|
+
it "returns -1" do
|
90
|
+
expect(described_class.to_direction(:desc)).to eq(-1)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "when DESCENDING" do
|
95
|
+
it "returns -1" do
|
96
|
+
expect(described_class.to_direction(:DESCENDING)).to eq(-1)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "when DESC" do
|
101
|
+
it "returns -1" do
|
102
|
+
expect(described_class.to_direction(:DESC)).to eq(-1)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context "when the value is numeric" do
|
108
|
+
it "should pass the value through unchanged" do
|
109
|
+
expect(described_class.to_direction(1)).to eq(1)
|
110
|
+
expect(described_class.to_direction(-1)).to eq(-1)
|
111
|
+
expect(described_class.to_direction(Math::PI)).to eq(Math::PI)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "when the value is a hash" do
|
116
|
+
it "should pass the value through unchanged" do
|
117
|
+
expect(described_class.to_direction({})).to eq({})
|
118
|
+
expect(described_class.to_direction(scope: { "$meta" => "textScore" }))
|
119
|
+
.to eq(scope: { "$meta" => "textScore" })
|
120
|
+
expect(described_class.to_direction(a: 1, b: 2)).to eq(a: 1, b: 2)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "when the value is an unrecognized type" do
|
125
|
+
it "should raise ArgumentError" do
|
126
|
+
expect { described_class.to_direction([]) }.to raise_error(ArgumentError)
|
127
|
+
expect { described_class.to_direction(/a/) }.to raise_error(ArgumentError)
|
128
|
+
expect { described_class.to_direction(Object.new) }.to raise_error(ArgumentError)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -273,6 +273,21 @@ describe Mongoid::Extensions::BigDecimal do
|
|
273
273
|
end
|
274
274
|
end
|
275
275
|
|
276
|
+
context "when the value is castable" do
|
277
|
+
|
278
|
+
let(:value) do
|
279
|
+
2.hours
|
280
|
+
end
|
281
|
+
|
282
|
+
before do
|
283
|
+
expect(value).to be_a(ActiveSupport::Duration)
|
284
|
+
end
|
285
|
+
|
286
|
+
it "returns nil" do
|
287
|
+
expect(mongoized).to eq(7200)
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
276
291
|
context "when the value is nil" do
|
277
292
|
|
278
293
|
let(:value) do
|
@@ -100,10 +100,10 @@ describe Mongoid::Extensions::Float do
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
-
context "when the string
|
103
|
+
context "when the string starts with a number" do
|
104
104
|
|
105
|
-
it "returns
|
106
|
-
expect(Float.send(method, "
|
105
|
+
it "returns nil" do
|
106
|
+
expect(Float.send(method, "42bogus")).to be_nil
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
@@ -120,6 +120,13 @@ describe Mongoid::Extensions::Float do
|
|
120
120
|
expect(Float.send(method, nil)).to be_nil
|
121
121
|
end
|
122
122
|
end
|
123
|
+
|
124
|
+
context "when giving an object that is castable to an Float" do
|
125
|
+
|
126
|
+
it "returns the integer value" do
|
127
|
+
expect(Float.send(method, 2.hours)).to eq(7200)
|
128
|
+
end
|
129
|
+
end
|
123
130
|
end
|
124
131
|
end
|
125
132
|
end
|
@@ -94,10 +94,10 @@ describe Mongoid::Extensions::Integer do
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
-
context "when the string
|
97
|
+
context "when the string starts with a number" do
|
98
98
|
|
99
|
-
it "returns
|
100
|
-
expect(Integer.send(method, "
|
99
|
+
it "returns nil" do
|
100
|
+
expect(Integer.send(method, "42bogus")).to be_nil
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
@@ -114,6 +114,13 @@ describe Mongoid::Extensions::Integer do
|
|
114
114
|
expect(Integer.send(method, nil)).to be_nil
|
115
115
|
end
|
116
116
|
end
|
117
|
+
|
118
|
+
context "when giving an object that is castable to an Integer" do
|
119
|
+
|
120
|
+
it "returns the integer value" do
|
121
|
+
expect(Integer.send(method, 2.hours)).to eq(7200)
|
122
|
+
end
|
123
|
+
end
|
117
124
|
end
|
118
125
|
end
|
119
126
|
end
|
@@ -102,6 +102,29 @@ describe Mongoid::Fields::Localized do
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
+
context "when key is a symbol" do
|
106
|
+
|
107
|
+
let(:value) do
|
108
|
+
field.demongoize({ :de => "This is a test" })
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns the string from the set locale" do
|
112
|
+
expect(value).to eq("This is a test")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
context "passing a bogus value" do
|
118
|
+
|
119
|
+
let(:value) do
|
120
|
+
field.demongoize("bogus")
|
121
|
+
end
|
122
|
+
|
123
|
+
it "returns nil" do
|
124
|
+
expect(value).to be_nil
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
105
128
|
context "when the value does not exist" do
|
106
129
|
|
107
130
|
context "when not using fallbacks" do
|
@@ -117,10 +140,7 @@ describe Mongoid::Fields::Localized do
|
|
117
140
|
|
118
141
|
context "when using fallbacks" do
|
119
142
|
|
120
|
-
|
121
|
-
require "i18n/backend/fallbacks"
|
122
|
-
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
|
123
|
-
end
|
143
|
+
with_i18n_fallbacks
|
124
144
|
|
125
145
|
context "when fallbacks are defined" do
|
126
146
|
|
@@ -139,6 +159,17 @@ describe Mongoid::Fields::Localized do
|
|
139
159
|
end
|
140
160
|
end
|
141
161
|
|
162
|
+
context "when the fallback translation exists and is a symbol" do
|
163
|
+
|
164
|
+
let(:value) do
|
165
|
+
field.demongoize({ :es => "testing" })
|
166
|
+
end
|
167
|
+
|
168
|
+
it "returns the fallback translation" do
|
169
|
+
expect(value).to eq("testing")
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
142
173
|
context "when another fallback translation exists" do
|
143
174
|
|
144
175
|
let(:value) do
|
@@ -275,10 +306,7 @@ describe Mongoid::Fields::Localized do
|
|
275
306
|
|
276
307
|
context "when using fallbacks" do
|
277
308
|
|
278
|
-
|
279
|
-
require "i18n/backend/fallbacks"
|
280
|
-
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
|
281
|
-
end
|
309
|
+
with_i18n_fallbacks
|
282
310
|
|
283
311
|
context "when fallbacks are defined" do
|
284
312
|
|
@@ -476,10 +504,7 @@ describe Mongoid::Fields::Localized do
|
|
476
504
|
|
477
505
|
context "when fallbacks are defined" do
|
478
506
|
|
479
|
-
|
480
|
-
require "i18n/backend/fallbacks"
|
481
|
-
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
|
482
|
-
end
|
507
|
+
with_i18n_fallbacks
|
483
508
|
|
484
509
|
context "when the lookup does not need to use fallbacks" do
|
485
510
|
|
@@ -59,3 +59,17 @@ end
|
|
59
59
|
class SmNotSharded
|
60
60
|
include Mongoid::Document
|
61
61
|
end
|
62
|
+
|
63
|
+
class SmReviewAuthor
|
64
|
+
include Mongoid::Document
|
65
|
+
embedded_in :review, class_name: "SmReview", touch: false
|
66
|
+
field :name, type: String
|
67
|
+
end
|
68
|
+
|
69
|
+
class SmReview
|
70
|
+
include Mongoid::Document
|
71
|
+
|
72
|
+
embeds_one :author, class_name: "SmReviewAuthor"
|
73
|
+
|
74
|
+
shard_key "author.name" => 1
|
75
|
+
end
|
@@ -27,11 +27,11 @@ describe Mongoid::Shardable do
|
|
27
27
|
context 'when full syntax is used' do
|
28
28
|
context 'with symbol value' do
|
29
29
|
it 'sets shard key fields to symbol value' do
|
30
|
-
SmProducer.shard_key_fields.
|
30
|
+
expect(SmProducer.shard_key_fields).to be == %i(age gender)
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'sets shard config' do
|
34
|
-
SmProducer.shard_config.
|
34
|
+
expect(SmProducer.shard_config).to be == {
|
35
35
|
key: {age: 1, gender: 'hashed'},
|
36
36
|
options: {
|
37
37
|
unique: true,
|
@@ -41,37 +41,37 @@ describe Mongoid::Shardable do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'keeps hashed as string' do
|
44
|
-
SmProducer.shard_config[:key][:gender].
|
44
|
+
expect(SmProducer.shard_config[:key][:gender]).to be == 'hashed'
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
context 'with string value' do
|
49
49
|
it 'sets shard key fields to symbol value' do
|
50
|
-
SmActor.shard_key_fields.
|
50
|
+
expect(SmActor.shard_key_fields).to be == %i(age gender hello)
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'sets shard config' do
|
54
|
-
SmActor.shard_config.
|
54
|
+
expect(SmActor.shard_config).to be == {
|
55
55
|
key: {age: 1, gender: 'hashed', hello: 'hashed'},
|
56
56
|
options: {},
|
57
57
|
}
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'sets hashed to string' do
|
61
|
-
SmActor.shard_config[:key][:gender].
|
61
|
+
expect(SmActor.shard_config[:key][:gender]).to be == 'hashed'
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
65
|
context 'when passed association name' do
|
66
66
|
it 'uses foreign key as shard key in shard config' do
|
67
|
-
SmDriver.shard_config.
|
67
|
+
expect(SmDriver.shard_config).to be == {
|
68
68
|
key: {age: 1, agency_id: 'hashed'},
|
69
69
|
options: {},
|
70
70
|
}
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'uses foreign key as shard key in shard key fields' do
|
74
|
-
SmDriver.shard_key_fields.
|
74
|
+
expect(SmDriver.shard_key_fields).to be == %i(age agency_id)
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
@@ -79,26 +79,26 @@ describe Mongoid::Shardable do
|
|
79
79
|
context 'when shorthand syntax is used' do
|
80
80
|
context 'with symbol value' do
|
81
81
|
it 'sets shard key fields to symbol value' do
|
82
|
-
SmMovie.shard_key_fields.
|
82
|
+
expect(SmMovie.shard_key_fields).to be == %i(year)
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
86
|
context 'with string value' do
|
87
87
|
it 'sets shard key fields to symbol value' do
|
88
|
-
SmTrailer.shard_key_fields.
|
88
|
+
expect(SmTrailer.shard_key_fields).to be == %i(year)
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
92
|
context 'when passed association name' do
|
93
93
|
it 'uses foreign key as shard key in shard config' do
|
94
|
-
SmDirector.shard_config.
|
94
|
+
expect(SmDirector.shard_config).to be == {
|
95
95
|
key: {agency_id: 1},
|
96
96
|
options: {},
|
97
97
|
}
|
98
98
|
end
|
99
99
|
|
100
100
|
it 'uses foreign key as shard key in shard key fields' do
|
101
|
-
SmDirector.shard_key_fields.
|
101
|
+
expect(SmDirector.shard_key_fields).to be == %i(agency_id)
|
102
102
|
end
|
103
103
|
end
|
104
104
|
end
|
@@ -106,41 +106,80 @@ describe Mongoid::Shardable do
|
|
106
106
|
|
107
107
|
describe '#shard_key_selector' do
|
108
108
|
subject { instance.shard_key_selector }
|
109
|
-
|
110
|
-
|
109
|
+
|
110
|
+
context 'when key is an immediate attribute' do
|
111
|
+
let(:klass) { Band }
|
112
|
+
let(:value) { 'a-brand-name' }
|
111
113
|
|
112
|
-
|
114
|
+
before { klass.shard_key(:name) }
|
113
115
|
|
114
|
-
|
115
|
-
|
116
|
+
context 'when record is new' do
|
117
|
+
let(:instance) { klass.new(name: value) }
|
116
118
|
|
117
|
-
|
119
|
+
it { is_expected.to eq({ 'name' => value }) }
|
118
120
|
|
119
|
-
|
120
|
-
|
121
|
+
context 'changing shard key value' do
|
122
|
+
let(:new_value) { 'a-new-value' }
|
121
123
|
|
122
|
-
|
123
|
-
|
124
|
+
before do
|
125
|
+
instance.name = new_value
|
126
|
+
end
|
127
|
+
|
128
|
+
it { is_expected.to eq({ 'name' => new_value }) }
|
124
129
|
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'when record is persisted' do
|
133
|
+
let(:instance) { klass.create!(name: value) }
|
125
134
|
|
126
|
-
it { is_expected.to eq({ 'name' =>
|
135
|
+
it { is_expected.to eq({ 'name' => value }) }
|
136
|
+
|
137
|
+
context 'changing shard key value' do
|
138
|
+
let(:new_value) { 'a-new-value' }
|
139
|
+
|
140
|
+
before do
|
141
|
+
instance.name = new_value
|
142
|
+
end
|
143
|
+
|
144
|
+
it { is_expected.to eq({ 'name' => new_value }) }
|
145
|
+
end
|
127
146
|
end
|
128
147
|
end
|
129
148
|
|
130
|
-
context 'when
|
131
|
-
let(:
|
149
|
+
context 'when key is an embedded attribute' do
|
150
|
+
let(:klass) { SmReview }
|
151
|
+
let(:value) { 'Arthur Conan Doyle' }
|
152
|
+
let(:key) { 'author.name' }
|
132
153
|
|
133
|
-
|
154
|
+
context 'when record is new' do
|
155
|
+
let(:instance) { klass.new(author: { name: value }) }
|
134
156
|
|
135
|
-
|
136
|
-
let(:new_value) { 'a-new-value' }
|
157
|
+
it { is_expected.to eq({ key => value }) }
|
137
158
|
|
138
|
-
|
139
|
-
|
159
|
+
context 'changing shard key value' do
|
160
|
+
let(:new_value) { 'Jules Verne' }
|
161
|
+
|
162
|
+
before do
|
163
|
+
instance.author.name = new_value
|
164
|
+
end
|
165
|
+
|
166
|
+
it { is_expected.to eq({ key => new_value }) }
|
140
167
|
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'when record is persisted' do
|
171
|
+
let(:instance) { klass.create!(author: { name: value }) }
|
172
|
+
|
173
|
+
it { is_expected.to eq({ key => value }) }
|
174
|
+
|
175
|
+
context 'changing shard key value' do
|
176
|
+
let(:new_value) { 'Jules Verne' }
|
141
177
|
|
142
|
-
|
143
|
-
|
178
|
+
before do
|
179
|
+
instance.author.name = new_value
|
180
|
+
end
|
181
|
+
|
182
|
+
it { is_expected.to eq({ 'author.name' => new_value }) }
|
144
183
|
end
|
145
184
|
end
|
146
185
|
end
|
@@ -148,56 +187,109 @@ describe Mongoid::Shardable do
|
|
148
187
|
|
149
188
|
describe '#shard_key_selector_in_db' do
|
150
189
|
subject { instance.shard_key_selector_in_db }
|
151
|
-
let(:klass) { Band }
|
152
|
-
let(:value) { 'a-brand-name' }
|
153
190
|
|
154
|
-
|
191
|
+
context 'when key is an immediate attribute' do
|
192
|
+
let(:klass) { Band }
|
193
|
+
let(:value) { 'a-brand-name' }
|
155
194
|
|
156
|
-
|
157
|
-
let(:instance) { klass.new(name: value) }
|
195
|
+
before { klass.shard_key(:name) }
|
158
196
|
|
159
|
-
|
197
|
+
context 'when record is new' do
|
198
|
+
let(:instance) { klass.new(name: value) }
|
160
199
|
|
161
|
-
|
162
|
-
let(:new_value) { 'a-new-value' }
|
200
|
+
it { is_expected.to eq({ 'name' => value }) }
|
163
201
|
|
164
|
-
|
165
|
-
|
166
|
-
|
202
|
+
context 'changing shard key value' do
|
203
|
+
let(:new_value) { 'a-new-value' }
|
204
|
+
|
205
|
+
before do
|
206
|
+
instance.name = new_value
|
207
|
+
end
|
167
208
|
|
168
|
-
|
169
|
-
subject.should == { 'name' => new_value }
|
209
|
+
it { is_expected.to eq({ 'name' => new_value }) }
|
170
210
|
end
|
171
211
|
end
|
172
|
-
end
|
173
212
|
|
174
|
-
|
175
|
-
|
213
|
+
context 'when record is persisted' do
|
214
|
+
let(:instance) { klass.create!(name: value) }
|
215
|
+
|
216
|
+
it { is_expected.to eq({ 'name' => value }) }
|
217
|
+
|
218
|
+
context 'changing shard key value' do
|
219
|
+
let(:new_value) { 'a-new-value' }
|
176
220
|
|
177
|
-
|
221
|
+
before do
|
222
|
+
instance.name = new_value
|
223
|
+
end
|
178
224
|
|
179
|
-
|
180
|
-
|
225
|
+
it { is_expected.to eq({ 'name' => value }) }
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
context "when record is not found" do
|
230
|
+
let!(:instance) { klass.create!(name: value) }
|
181
231
|
|
182
232
|
before do
|
183
|
-
instance.
|
233
|
+
instance.destroy
|
184
234
|
end
|
185
235
|
|
186
|
-
it
|
236
|
+
it "raises a DocumentNotFound error with the shard key in the description on reload" do
|
237
|
+
expect do
|
238
|
+
instance.reload
|
239
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound, /Document not found for class Band with id #{instance.id.to_s} and shard key name: a-brand-name./)
|
240
|
+
end
|
187
241
|
end
|
188
242
|
end
|
189
243
|
|
190
|
-
context
|
191
|
-
let
|
244
|
+
context 'when key is an embedded attribute' do
|
245
|
+
let(:klass) { SmReview }
|
246
|
+
let(:value) { 'Arthur Conan Doyle' }
|
247
|
+
let(:key) { 'author.name' }
|
248
|
+
|
249
|
+
context 'when record is new' do
|
250
|
+
let(:instance) { klass.new(author: { name: value }) }
|
251
|
+
|
252
|
+
it { is_expected.to eq({ key => value }) }
|
192
253
|
|
193
|
-
|
194
|
-
|
254
|
+
context 'changing shard key value' do
|
255
|
+
let(:new_value) { 'Jules Verne' }
|
256
|
+
|
257
|
+
before do
|
258
|
+
instance.author.name = new_value
|
259
|
+
end
|
260
|
+
|
261
|
+
it { is_expected.to eq({ key => new_value }) }
|
262
|
+
end
|
195
263
|
end
|
196
264
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
265
|
+
context 'when record is persisted' do
|
266
|
+
let(:instance) { klass.create!(author: { name: value }) }
|
267
|
+
|
268
|
+
it { is_expected.to eq({ key => value }) }
|
269
|
+
|
270
|
+
context 'changing shard key value' do
|
271
|
+
let(:new_value) { 'Jules Verne' }
|
272
|
+
|
273
|
+
before do
|
274
|
+
instance.author.name = new_value
|
275
|
+
end
|
276
|
+
|
277
|
+
it { is_expected.to eq({ key => value }) }
|
278
|
+
end
|
279
|
+
|
280
|
+
context "when record is not found" do
|
281
|
+
let!(:instance) { klass.create!(author: { name: value }) }
|
282
|
+
|
283
|
+
before do
|
284
|
+
instance.destroy
|
285
|
+
end
|
286
|
+
|
287
|
+
it "raises a DocumentNotFound error with the shard key in the description on reload" do
|
288
|
+
expect do
|
289
|
+
instance.reload
|
290
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound, /Document not found for class SmReview with id #{instance.id.to_s} and shard key author.name: Arthur Conan Doyle./)
|
291
|
+
end
|
292
|
+
end
|
201
293
|
end
|
202
294
|
end
|
203
295
|
end
|