mongoid_monkey 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/lib/patches/atomic.rb +225 -1
  3. data/lib/version.rb +1 -1
  4. data/spec/app/models/address.rb +17 -0
  5. data/spec/app/models/name.rb +14 -0
  6. data/spec/app/models/person.rb +39 -0
  7. data/spec/unit/{atomic_spec.rb → atomic/atomic_contextual_spec.rb} +0 -0
  8. data/spec/unit/atomic/mongoid3_style/atomic/add_to_set_spec.rb +266 -0
  9. data/spec/unit/atomic/mongoid3_style/atomic/bit_spec.rb +92 -0
  10. data/spec/unit/atomic/mongoid3_style/atomic/inc_spec.rb +137 -0
  11. data/spec/unit/atomic/mongoid3_style/atomic/pop_spec.rb +115 -0
  12. data/spec/unit/atomic/mongoid3_style/atomic/pull_all_spec.rb +81 -0
  13. data/spec/unit/atomic/mongoid3_style/atomic/pull_spec.rb +84 -0
  14. data/spec/unit/atomic/mongoid3_style/atomic/push_all_spec.rb +81 -0
  15. data/spec/unit/atomic/mongoid3_style/atomic/push_spec.rb +81 -0
  16. data/spec/unit/atomic/mongoid3_style/atomic/rename_spec.rb +46 -0
  17. data/spec/unit/atomic/mongoid3_style/atomic/sets_spec.rb +158 -0
  18. data/spec/unit/atomic/mongoid3_style/atomic/unset_spec.rb +69 -0
  19. data/spec/unit/atomic/mongoid3_style/atomic_spec.rb +220 -0
  20. data/spec/unit/atomic/mongoid4_style/incrementable_spec.rb +232 -0
  21. data/spec/unit/atomic/mongoid4_style/logical_spec.rb +262 -0
  22. data/spec/unit/atomic/mongoid4_style/poppable_spec.rb +139 -0
  23. data/spec/unit/atomic/mongoid4_style/pullable_spec.rb +172 -0
  24. data/spec/unit/atomic/mongoid4_style/pushable_spec.rb +159 -0
  25. data/spec/unit/atomic/mongoid4_style/renamable_spec.rb +139 -0
  26. data/spec/unit/atomic/mongoid4_style/settable_spec.rb +172 -0
  27. data/spec/unit/atomic/mongoid4_style/unsettable_spec.rb +28 -0
  28. metadata +46 -4
@@ -0,0 +1,262 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe 'Mongoid::Persistable::Pushable' do
6
+
7
+ describe "#add_to_set" do
8
+
9
+ context "when the document is a root document" do
10
+
11
+ shared_examples_for "a unique pushable root document" do
12
+
13
+ it "adds single values" do
14
+ expect(person.aliases).to eq([ 1, 2, 4 ])
15
+ end
16
+
17
+ it "does not add duplicate values" do
18
+ expect(person.array).to eq([ 4, 5 ])
19
+ end
20
+
21
+ it "sets absent values" do
22
+ expect(person.test_array).to eq([ 1 ])
23
+ end
24
+
25
+ it "returns self objet" do
26
+ expect(add).to eq(person)
27
+ end
28
+
29
+ it "resets the dirty changes" do
30
+ expect(person).to_not be_changed
31
+ end
32
+
33
+ it "persists single adds" do
34
+ expect(person.reload.aliases).to eq([ 1, 2, 4 ])
35
+ end
36
+
37
+ it "persists absent values" do
38
+ expect(person.reload.test_array).to eq([ 1 ])
39
+ end
40
+
41
+ it "flattens only 1 level" do
42
+ expect(person.reload.arrays).to eq([[ 7, 8 ]])
43
+ end
44
+ end
45
+
46
+ let(:person) do
47
+ Person.create(aliases: [ 1, 2 ], array: [ 4, 5 ])
48
+ end
49
+
50
+ context "when provided string fields" do
51
+
52
+ let!(:add) do
53
+ person.add_to_set("aliases" => 4, "array" => [4, 5], "test_array" => 1, "arrays" => [[ 7, 8 ]])
54
+ end
55
+
56
+ it_behaves_like "a unique pushable root document"
57
+ end
58
+
59
+ context "when provided symbol fields" do
60
+
61
+ let!(:add) do
62
+ person.add_to_set(aliases: 4, array: [4, 5], test_array: 1, arrays: [[ 7, 8 ]])
63
+ end
64
+
65
+ it_behaves_like "a unique pushable root document"
66
+ end
67
+ end
68
+
69
+ context "when the document is embedded" do
70
+
71
+ shared_examples_for "a unique pushable embedded document" do
72
+
73
+ it "adds single values" do
74
+ expect(address.services).to eq([ 1, 4 ])
75
+ end
76
+
77
+ it "does not add duplicate values" do
78
+ expect(address.a).to eq([ 4, 5 ])
79
+ end
80
+
81
+ it "sets absent values" do
82
+ expect(address.test).to eq([ 1 ])
83
+ end
84
+
85
+ it "returns self object" do
86
+ expect(add).to eq(address)
87
+ end
88
+
89
+ it "resets the dirty changes" do
90
+ expect(address).to_not be_changed
91
+ end
92
+
93
+ it "persists single adds" do
94
+ expect(address.reload.services).to eq([ 1, 4 ])
95
+ end
96
+
97
+ it "persists absent values" do
98
+ expect(address.reload.test).to eq([ 1 ])
99
+ end
100
+ end
101
+
102
+ let(:person) do
103
+ Person.create
104
+ end
105
+
106
+ let(:address) do
107
+ person.addresses.create(street: "t", services: [ 1 ], a: [ 4, 5 ])
108
+ end
109
+
110
+ context "when provided string fields" do
111
+
112
+ let!(:add) do
113
+ address.add_to_set("services" => 4, "a" => 5, "test" => 1)
114
+ end
115
+
116
+ it_behaves_like "a unique pushable embedded document"
117
+ end
118
+
119
+ context "when provided symbol fields" do
120
+
121
+ let!(:add) do
122
+ address.add_to_set(services: 4, a: 5, test: 1)
123
+ end
124
+
125
+ it_behaves_like "a unique pushable embedded document"
126
+ end
127
+ end
128
+ end
129
+
130
+ describe "#push" do
131
+
132
+ context "when the document is a root document" do
133
+
134
+ shared_examples_for "a pushable root document" do
135
+
136
+ it "pushes single values" do
137
+ expect(person.aliases).to eq([ 1, 2, 3, 4 ])
138
+ end
139
+
140
+ it "pushes multiple values" do
141
+ expect(person.array).to eq([ 4, 5, 6, 7, 8 ])
142
+ end
143
+
144
+ it "sets absent values" do
145
+ expect(person.test_array).to eq([ 1 ])
146
+ end
147
+
148
+ it "returns self object" do
149
+ expect(push).to eq(person)
150
+ end
151
+
152
+ it "resets the dirty changes" do
153
+ expect(person).to_not be_changed
154
+ end
155
+
156
+ it "persists single pushes" do
157
+ expect(person.reload.aliases).to eq([ 1, 2, 3, 4 ])
158
+ end
159
+
160
+ it "persists multiple pushes" do
161
+ expect(person.reload.array).to eq([ 4, 5, 6, 7, 8 ])
162
+ end
163
+
164
+ it "persists absent values" do
165
+ expect(person.reload.test_array).to eq([ 1 ])
166
+ end
167
+
168
+ it "flattens only 1 level" do
169
+ expect(person.reload.arrays).to eq([[ 7, 8 ]])
170
+ end
171
+ end
172
+
173
+ let(:person) do
174
+ Person.create(aliases: [ 1, 2, 3 ], array: [ 4, 5, 6 ])
175
+ end
176
+
177
+ context "when provided string fields" do
178
+
179
+ let!(:push) do
180
+ person.push("aliases" => 4, "array" => [ 7, 8 ], "test_array" => 1, "arrays" => [[ 7, 8 ]])
181
+ end
182
+
183
+ it_behaves_like "a pushable root document"
184
+ end
185
+
186
+ context "when provided symbol fields" do
187
+
188
+ let!(:push) do
189
+ person.push(aliases: 4, array: [ 7, 8 ], test_array: 1, arrays: [[ 7, 8 ]])
190
+ end
191
+
192
+ it_behaves_like "a pushable root document"
193
+ end
194
+ end
195
+
196
+ context "when the document is embedded" do
197
+
198
+ shared_examples_for "a pushable embedded document" do
199
+
200
+ it "pushes single values" do
201
+ expect(address.services).to eq([ 1, 4 ])
202
+ end
203
+
204
+ it "pushes multiple values" do
205
+ expect(address.a).to eq([ 4, 5, 6, 7 ])
206
+ end
207
+
208
+ it "sets absent values" do
209
+ expect(address.test).to eq([ 1 ])
210
+ end
211
+
212
+ it "returns self object" do
213
+ expect(push).to eq(address)
214
+ end
215
+
216
+ it "resets the dirty changes" do
217
+ expect(address).to_not be_changed
218
+ end
219
+
220
+ it "persists single pushes" do
221
+ expect(address.reload.services).to eq([ 1, 4 ])
222
+ end
223
+
224
+ it "persists multiple pushes" do
225
+ expect(address.reload.a).to eq([ 4, 5, 6, 7 ])
226
+ end
227
+
228
+ it "persists absent values" do
229
+ expect(address.reload.test).to eq([ 1 ])
230
+ end
231
+ end
232
+
233
+ let(:person) do
234
+ Person.create
235
+ end
236
+
237
+ let(:address) do
238
+ person.addresses.create(street: "t", services: [ 1 ], a: [ 4, 5 ])
239
+ end
240
+
241
+ context "when provided string fields" do
242
+
243
+ let!(:push) do
244
+ address.push("services" => 4, "a" => [ 6, 7 ], "test" => 1)
245
+ end
246
+
247
+ it_behaves_like "a pushable embedded document"
248
+ end
249
+
250
+ context "when provided symbol fields" do
251
+
252
+ let!(:push) do
253
+ address.push(services: 4, a: [ 6, 7 ], test: 1)
254
+ end
255
+
256
+ it_behaves_like "a pushable embedded document"
257
+ end
258
+ end
259
+ end
260
+ end
261
+
262
+ end
@@ -0,0 +1,139 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe 'Mongoid::Persistable::Renamable' do
6
+
7
+ describe "#rename" do
8
+
9
+ context "when the document is a root document" do
10
+
11
+ shared_examples_for "a renamable root document" do
12
+
13
+ it "renames the first field" do
14
+ expect(person.salutation).to eq("sir")
15
+ end
16
+
17
+ it "removes the first original value" do
18
+ expect(person.title).to be_nil
19
+ end
20
+
21
+ it "renames the second field" do
22
+ expect(person.date_of_birth.to_date).to eq(date)
23
+ end
24
+
25
+ it "removes the second original value" do
26
+ expect(person.dob).to be_nil
27
+ end
28
+
29
+ it "returns self object" do
30
+ expect(rename).to eq(person)
31
+ end
32
+
33
+ it "persists the first rename" do
34
+ expect(person.reload.salutation).to eq("sir")
35
+ end
36
+
37
+ it "persists the first original removal" do
38
+ expect(person.reload.title).to be_nil
39
+ end
40
+
41
+ it "persists the second rename" do
42
+ expect(person.reload.date_of_birth.to_date).to eq(date)
43
+ end
44
+
45
+ it "persists the second original removal" do
46
+ expect(person.reload.dob).to be_nil
47
+ end
48
+
49
+ it "clears out the dirty changes" do
50
+ expect(person).to_not be_changed
51
+ end
52
+ end
53
+
54
+ let(:date) do
55
+ Date.new(2013, 1, 1)
56
+ end
57
+
58
+ let(:person) do
59
+ Person.create(title: "sir", dob: date)
60
+ end
61
+
62
+ context "when provided symbol names" do
63
+
64
+ let!(:rename) do
65
+ person.rename(title: :salutation, dob: :date_of_birth)
66
+ end
67
+
68
+ it_behaves_like "a renamable root document"
69
+ end
70
+
71
+ context "when provided string names" do
72
+
73
+ let!(:rename) do
74
+ person.rename(title: "salutation", dob: "date_of_birth")
75
+ end
76
+
77
+ it_behaves_like "a renamable root document"
78
+ end
79
+ end
80
+
81
+ context "when the document is embedded" do
82
+
83
+ shared_examples_for "a renamable embedded document" do
84
+
85
+ it "renames the first field" do
86
+ expect(name.mi).to eq("blah")
87
+ end
88
+
89
+ it "removes the first original value" do
90
+ expect(name.middle).to be_nil
91
+ end
92
+
93
+ it "returns self object" do
94
+ expect(rename).to eq(name)
95
+ end
96
+
97
+ it "persists the first rename" do
98
+ expect(name.reload.mi).to eq("blah")
99
+ end
100
+
101
+ it "persists the first original removal" do
102
+ expect(name.reload.middle).to be_nil
103
+ end
104
+
105
+ it "clears out the dirty changes" do
106
+ expect(name).to_not be_changed
107
+ end
108
+ end
109
+
110
+ let(:person) do
111
+ Person.create
112
+ end
113
+
114
+ let(:name) do
115
+ person.create_name(first_name: "test", last_name: "user", middle: "blah")
116
+ end
117
+
118
+ context "when provided symbol names" do
119
+
120
+ let!(:rename) do
121
+ name.rename(middle: :mi)
122
+ end
123
+
124
+ it_behaves_like "a renamable embedded document"
125
+ end
126
+
127
+ context "when provided string names" do
128
+
129
+ let!(:rename) do
130
+ name.rename(middle: "mi")
131
+ end
132
+
133
+ it_behaves_like "a renamable embedded document"
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ end
@@ -0,0 +1,172 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe 'Mongoid::Persistable::Settable' do
6
+
7
+ describe "#set" do
8
+
9
+ context "when the document is a root document" do
10
+
11
+ shared_examples_for "a settable root document" do
12
+
13
+ it "sets the normal field to the new value" do
14
+ expect(person.title).to eq("kaiser")
15
+ end
16
+
17
+ it "properly sets aliased fields" do
18
+ expect(person.test).to eq("alias-test")
19
+ end
20
+
21
+ it "casts fields that need typecasting" do
22
+ expect(person.dob).to eq(date)
23
+ end
24
+
25
+ it "returns self object" do
26
+ expect(set).to eq(person)
27
+ end
28
+
29
+ it "persists the normal field set" do
30
+ expect(person.reload.title).to eq("kaiser")
31
+ end
32
+
33
+ it "persists sets on aliased fields" do
34
+ expect(person.reload.test).to eq("alias-test")
35
+ end
36
+
37
+ it "persists fields that need typecasting" do
38
+ expect(person.reload.dob).to eq(date)
39
+ end
40
+
41
+ it "resets the dirty attributes for the sets" do
42
+ expect(person).to_not be_changed
43
+ end
44
+ end
45
+
46
+ let(:person) do
47
+ Person.create
48
+ end
49
+
50
+ let(:date) do
51
+ Date.new(1976, 11, 19)
52
+ end
53
+
54
+ context "when provided string fields" do
55
+
56
+ let!(:set) do
57
+ person.set("title" => "kaiser", "test" => "alias-test", "dob" => date)
58
+ end
59
+
60
+ it_behaves_like "a settable root document"
61
+ end
62
+
63
+ context "when provided symbol fields" do
64
+
65
+ let!(:set) do
66
+ person.set(title: "kaiser", test: "alias-test", dob: date)
67
+ end
68
+
69
+ it_behaves_like "a settable root document"
70
+ end
71
+ end
72
+
73
+ context "when the document is embedded" do
74
+
75
+ shared_examples_for "a settable embedded document" do
76
+
77
+ it "sets the normal field to the new value" do
78
+ expect(address.number).to eq(44)
79
+ end
80
+
81
+ it "properly sets aliased fields" do
82
+ expect(address.suite).to eq("400")
83
+ end
84
+
85
+ it "casts fields that need typecasting" do
86
+ expect(address.end_date).to eq(date)
87
+ end
88
+
89
+ it "returns self object" do
90
+ expect(set).to eq(address)
91
+ end
92
+
93
+ it "persists the normal field set" do
94
+ expect(address.reload.number).to eq(44)
95
+ end
96
+
97
+ it "persists the aliased field set" do
98
+ expect(address.reload.suite).to eq("400")
99
+ end
100
+
101
+ it "persists the fields that need typecasting" do
102
+ expect(address.reload.end_date).to eq(date)
103
+ end
104
+
105
+ it "resets the dirty attributes for the sets" do
106
+ expect(address).to_not be_changed
107
+ end
108
+ end
109
+
110
+ let(:person) do
111
+ Person.create
112
+ end
113
+
114
+ let(:address) do
115
+ person.addresses.create(street: "t")
116
+ end
117
+
118
+ let(:date) do
119
+ Date.new(1976, 11, 19)
120
+ end
121
+
122
+ context "when provided string fields" do
123
+
124
+ let!(:set) do
125
+ address.set("number" => 44, "suite" => "400", "end_date" => date)
126
+ end
127
+
128
+ it_behaves_like "a settable embedded document"
129
+ end
130
+
131
+ context "when provided symbol fields" do
132
+
133
+ let!(:set) do
134
+ address.set(number: 44, suite: "400", end_date: date)
135
+ end
136
+
137
+ it_behaves_like "a settable embedded document"
138
+ end
139
+ end
140
+ end
141
+
142
+ context "when dynamic attributes are not enabled" do
143
+ let(:account) do
144
+ Account.create
145
+ end
146
+
147
+ around do |example|
148
+ Mongoid.allow_dynamic_fields = false
149
+ example.run
150
+ Mongoid.allow_dynamic_fields = true
151
+ end
152
+
153
+ it "raises exception for an unknown attribute " do
154
+ expect {
155
+ account.set(somethingnew: "somethingnew")
156
+ }.to raise_error(Mongoid::Errors::UnknownAttribute)
157
+ end
158
+ end
159
+
160
+ context "when dynamic attributes enabled" do
161
+ let(:person) do
162
+ Person.create
163
+ end
164
+
165
+ it "updates non existing attribute" do
166
+ person.set(somethingnew: "somethingnew")
167
+ expect(person.reload.somethingnew).to eq "somethingnew"
168
+ end
169
+ end
170
+ end
171
+
172
+ end