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,159 @@
1
+ require "spec_helper"
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe 'Mongoid::Persistable::Unsettable' do
6
+
7
+ describe "#unset" do
8
+
9
+ context "when the document is a root document" do
10
+
11
+ shared_examples_for "an unsettable root document" do
12
+
13
+ it "unsets the first field" do
14
+ expect(person.title).to be_nil
15
+ end
16
+
17
+ it "unsets the second field" do
18
+ expect(person.age).to be_nil
19
+ end
20
+
21
+ # it "returns self object" do
22
+ # expect(unset).to eq(person)
23
+ # end
24
+
25
+ it "persists the first unset" do
26
+ expect(person.reload.title).to be_nil
27
+ end
28
+
29
+ it "persists the last_unset" do
30
+ expect(person.reload.age).to eq(100)
31
+ end
32
+
33
+ it "clears out dirty changes for the fields" do
34
+ expect(person).to_not be_changed
35
+ end
36
+ end
37
+
38
+ let(:date) do
39
+ Date.new(1976, 11, 19)
40
+ end
41
+
42
+ let(:person) do
43
+ Person.create(title: "test", age: 30, dob: date)
44
+ end
45
+
46
+ context "when provided a splat of symbols" do
47
+
48
+ let!(:unset) do
49
+ person.unset(:title, :age)
50
+ end
51
+
52
+ it_behaves_like "an unsettable root document"
53
+ end
54
+
55
+ context "when provided a splat of strings" do
56
+
57
+ let!(:unset) do
58
+ person.unset("title", "age")
59
+ end
60
+
61
+ it_behaves_like "an unsettable root document"
62
+ end
63
+
64
+ context "when provided an array of symbols" do
65
+
66
+ let!(:unset) do
67
+ person.unset([ :title, :age ])
68
+ end
69
+
70
+ it_behaves_like "an unsettable root document"
71
+ end
72
+
73
+ context "when provided an array of strings" do
74
+
75
+ let!(:unset) do
76
+ person.unset([ "title", "age" ])
77
+ end
78
+
79
+ it_behaves_like "an unsettable root document"
80
+ end
81
+ end
82
+
83
+ context "when the document is embedded" do
84
+
85
+ shared_examples_for "an unsettable embedded document" do
86
+
87
+ it "unsets the first field" do
88
+ expect(address.number).to be_nil
89
+ end
90
+
91
+ it "unsets the second field" do
92
+ expect(address.city).to be_nil
93
+ end
94
+
95
+ # it "returns self object" do
96
+ # expect(unset).to eq(address)
97
+ # end
98
+
99
+ it "persists the first unset" do
100
+ expect(address.reload.number).to be_nil
101
+ end
102
+
103
+ it "persists the last_unset" do
104
+ expect(address.reload.city).to be_nil
105
+ end
106
+
107
+ it "clears out dirty changes for the fields" do
108
+ expect(address).to_not be_changed
109
+ end
110
+ end
111
+
112
+ let(:person) do
113
+ Person.create
114
+ end
115
+
116
+ let(:address) do
117
+ person.addresses.create(street: "kreuzberg", number: 40, city: "Berlin")
118
+ end
119
+
120
+ context "when provided a splat of symbols" do
121
+
122
+ let!(:unset) do
123
+ address.unset(:number, :city)
124
+ end
125
+
126
+ it_behaves_like "an unsettable embedded document"
127
+ end
128
+
129
+ context "when provided a splat of strings" do
130
+
131
+ let!(:unset) do
132
+ address.unset("number", "city")
133
+ end
134
+
135
+ it_behaves_like "an unsettable embedded document"
136
+ end
137
+
138
+ context "when provided an array of symbols" do
139
+
140
+ let!(:unset) do
141
+ address.unset([ :number, :city ])
142
+ end
143
+
144
+ it_behaves_like "an unsettable embedded document"
145
+ end
146
+
147
+ context "when provided an array of strings" do
148
+
149
+ let!(:unset) do
150
+ address.unset([ "number", "city" ])
151
+ end
152
+
153
+ it_behaves_like "an unsettable embedded document"
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ 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
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ if Mongoid::VERSION =~ /\A3\./
4
+
5
+ describe "A document loaded from rails" do
6
+ before(:all) do
7
+ require 'mongoid/railties/document'
8
+ end
9
+
10
+ after(:all) do
11
+ Mongoid::Document.class_eval do
12
+ undef _destroy
13
+ end
14
+ end
15
+
16
+ it "defines a _destroy method" do
17
+ expect(Person.new).to respond_to(:_destroy)
18
+ end
19
+
20
+ describe "#_destroy" do
21
+
22
+ it "always returns false" do
23
+ expect(Person.new._destroy).to be false
24
+ end
25
+ end
26
+ end
27
+
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_monkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - johnnyshields
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-06 00:00:00.000000000 Z
11
+ date: 2016-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -72,6 +72,7 @@ files:
72
72
  - spec/app/models/account.rb
73
73
  - spec/app/models/address.rb
74
74
  - spec/app/models/draft.rb
75
+ - spec/app/models/name.rb
75
76
  - spec/app/models/person.rb
76
77
  - spec/app/models/user.rb
77
78
  - spec/config/mongodb-mmapv1.conf
@@ -80,7 +81,27 @@ files:
80
81
  - spec/gemfile/mongoid4.gemfile
81
82
  - spec/gemfile/mongoid5.gemfile
82
83
  - spec/spec_helper.rb
83
- - spec/unit/atomic_spec.rb
84
+ - spec/unit/atomic/atomic_contextual_spec.rb
85
+ - spec/unit/atomic/mongoid3_style/atomic/add_to_set_spec.rb
86
+ - spec/unit/atomic/mongoid3_style/atomic/bit_spec.rb
87
+ - spec/unit/atomic/mongoid3_style/atomic/inc_spec.rb
88
+ - spec/unit/atomic/mongoid3_style/atomic/pop_spec.rb
89
+ - spec/unit/atomic/mongoid3_style/atomic/pull_all_spec.rb
90
+ - spec/unit/atomic/mongoid3_style/atomic/pull_spec.rb
91
+ - spec/unit/atomic/mongoid3_style/atomic/push_all_spec.rb
92
+ - spec/unit/atomic/mongoid3_style/atomic/push_spec.rb
93
+ - spec/unit/atomic/mongoid3_style/atomic/rename_spec.rb
94
+ - spec/unit/atomic/mongoid3_style/atomic/sets_spec.rb
95
+ - spec/unit/atomic/mongoid3_style/atomic/unset_spec.rb
96
+ - spec/unit/atomic/mongoid3_style/atomic_spec.rb
97
+ - spec/unit/atomic/mongoid4_style/incrementable_spec.rb
98
+ - spec/unit/atomic/mongoid4_style/logical_spec.rb
99
+ - spec/unit/atomic/mongoid4_style/poppable_spec.rb
100
+ - spec/unit/atomic/mongoid4_style/pullable_spec.rb
101
+ - spec/unit/atomic/mongoid4_style/pushable_spec.rb
102
+ - spec/unit/atomic/mongoid4_style/renamable_spec.rb
103
+ - spec/unit/atomic/mongoid4_style/settable_spec.rb
104
+ - spec/unit/atomic/mongoid4_style/unsettable_spec.rb
84
105
  - spec/unit/big_decimal_spec.rb
85
106
  - spec/unit/db_commands/mongoid3_indexes_spec.rb
86
107
  - spec/unit/db_commands/mongoid3_tasks_spec.rb
@@ -118,6 +139,7 @@ test_files:
118
139
  - spec/app/models/account.rb
119
140
  - spec/app/models/address.rb
120
141
  - spec/app/models/draft.rb
142
+ - spec/app/models/name.rb
121
143
  - spec/app/models/person.rb
122
144
  - spec/app/models/user.rb
123
145
  - spec/config/mongodb-mmapv1.conf
@@ -126,7 +148,27 @@ test_files:
126
148
  - spec/gemfile/mongoid4.gemfile
127
149
  - spec/gemfile/mongoid5.gemfile
128
150
  - spec/spec_helper.rb
129
- - spec/unit/atomic_spec.rb
151
+ - spec/unit/atomic/atomic_contextual_spec.rb
152
+ - spec/unit/atomic/mongoid3_style/atomic/add_to_set_spec.rb
153
+ - spec/unit/atomic/mongoid3_style/atomic/bit_spec.rb
154
+ - spec/unit/atomic/mongoid3_style/atomic/inc_spec.rb
155
+ - spec/unit/atomic/mongoid3_style/atomic/pop_spec.rb
156
+ - spec/unit/atomic/mongoid3_style/atomic/pull_all_spec.rb
157
+ - spec/unit/atomic/mongoid3_style/atomic/pull_spec.rb
158
+ - spec/unit/atomic/mongoid3_style/atomic/push_all_spec.rb
159
+ - spec/unit/atomic/mongoid3_style/atomic/push_spec.rb
160
+ - spec/unit/atomic/mongoid3_style/atomic/rename_spec.rb
161
+ - spec/unit/atomic/mongoid3_style/atomic/sets_spec.rb
162
+ - spec/unit/atomic/mongoid3_style/atomic/unset_spec.rb
163
+ - spec/unit/atomic/mongoid3_style/atomic_spec.rb
164
+ - spec/unit/atomic/mongoid4_style/incrementable_spec.rb
165
+ - spec/unit/atomic/mongoid4_style/logical_spec.rb
166
+ - spec/unit/atomic/mongoid4_style/poppable_spec.rb
167
+ - spec/unit/atomic/mongoid4_style/pullable_spec.rb
168
+ - spec/unit/atomic/mongoid4_style/pushable_spec.rb
169
+ - spec/unit/atomic/mongoid4_style/renamable_spec.rb
170
+ - spec/unit/atomic/mongoid4_style/settable_spec.rb
171
+ - spec/unit/atomic/mongoid4_style/unsettable_spec.rb
130
172
  - spec/unit/big_decimal_spec.rb
131
173
  - spec/unit/db_commands/mongoid3_indexes_spec.rb
132
174
  - spec/unit/db_commands/mongoid3_tasks_spec.rb