kakurenbo 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,270 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Kakurenbo::SoftDeleteCore do
4
- create_temp_table(:parent_models) { |t| t.integer :child_id; t.datetime :deleted_at }
5
- create_temp_table(:normal_child_models) { |t| t.integer :parent_model_id }
6
- create_temp_table(:paranoid_child_models) { |t| t.integer :parent_model_id; t.datetime :deleted_at }
7
- create_temp_table(:paranoid_single_child_models) { |t| t.integer :parent_model_id; t.datetime :deleted_at }
8
-
9
- before :each do
10
- class NormalChildModel < ActiveRecord::Base; end
11
- class ParanoidChildModel < ActiveRecord::Base; end
12
- class ParanoidSingleChildModel < ActiveRecord::Base; end
13
- class ParentModel < ActiveRecord::Base
14
- has_many :normal_child_models, :dependent => :destroy
15
- has_many :paranoid_child_models, :dependent => :destroy
16
-
17
- has_one :paranoid_single_child_model, :dependent => :destroy
18
- belongs_to :child, :class_name => :ParanoidSingleChildModel, :dependent => :destroy
19
- end
20
- end
21
-
22
- after :each do
23
- Object.class_eval do
24
- remove_const :NormalChildModel
25
- remove_const :ParanoidChildModel
26
- remove_const :ParanoidSingleChildModel
27
- remove_const :ParentModel
28
- end
29
- end
30
-
31
- describe 'NormalChildModel' do
32
- before :each do
33
- @parent = ParentModel.create!
34
- @first = @parent.normal_child_models.create!
35
- @second = @parent.normal_child_models.create!
36
-
37
- @hitori = NormalChildModel.create!
38
- end
39
-
40
- context 'when parent be destroyed' do
41
- it 'destroy children who parent has.' do
42
- expect{@parent.destroy}.to change(NormalChildModel, :count).by(-2)
43
- end
44
-
45
- it 'not destroy children who parent does not have.' do
46
- @parent.destroy
47
- expect(@hitori.reload.destroyed?).to be_false
48
- end
49
- end
50
- end
51
-
52
- describe 'ParanoidChildModel' do
53
- before :each do
54
- @parent = ParentModel.create!
55
- @first = @parent.paranoid_child_models.create!
56
- @second = @parent.paranoid_child_models.create!
57
- @third = @parent.paranoid_child_models.create!
58
-
59
- @hitori = ParanoidChildModel.create!
60
- end
61
-
62
- context 'when parent be destroyed' do
63
- it 'destroy children who parent has.' do
64
- expect{@parent.destroy}.to change(ParanoidChildModel, :count).by(-3)
65
- expect(@first.reload.destroyed?).to be_true
66
- expect(@second.reload.destroyed?).to be_true
67
- expect(@third.reload.destroyed?).to be_true
68
- end
69
-
70
- it 'not destroy children who parent does not have.' do
71
- @parent.destroy
72
- expect(@hitori.reload.destroyed?).to be_false
73
- end
74
- end
75
-
76
- context 'when parent be restored' do
77
- before :each do
78
- @parent.destroy
79
- @hitori.destroy
80
- end
81
-
82
- it 'restore children who parent has.' do
83
- expect{@parent.restore!}.to change(ParanoidChildModel, :count).by(3)
84
- expect(@first.reload.destroyed?).to be_false
85
- expect(@second.reload.destroyed?).to be_false
86
- expect(@third.reload.destroyed?).to be_false
87
- end
88
-
89
- it 'not restore children who parent does not have.' do
90
- @parent.restore!
91
- expect(@hitori.reload.destroyed?).to be_true
92
- end
93
- end
94
-
95
- context 'when delete child before parent was deleted' do
96
- before :each do
97
- # First, delete child.
98
- delete_at = 1.second.ago
99
- Time.stub(:now).and_return(delete_at)
100
- @first.destroy
101
-
102
- # Next, delete parent.
103
- Time.unstub(:now)
104
- @parent.destroy
105
- end
106
-
107
- it 'not restore child who deleted before parent was deleted' do
108
- expect{@parent.restore!}.to change(ParanoidChildModel, :count).by(2)
109
- expect(@first.reload.destroyed?).to be_true
110
- end
111
-
112
- it 'restore children who deleted after parent was deleted' do
113
- expect{@parent.restore!}.to change(ParanoidChildModel, :count).by(2)
114
- expect(@second.reload.destroyed?).to be_false
115
- expect(@third.reload.destroyed?).to be_false
116
- end
117
- end
118
- end
119
-
120
- describe 'ParanoidSingleChildModel(has_one)' do
121
- before :each do
122
- @parent = ParentModel.create!
123
- @first = ParanoidSingleChildModel.create!(parent_model_id: @parent.id)
124
-
125
- @hitori = ParanoidSingleChildModel.create!
126
- end
127
-
128
- context 'when parent be destroyed' do
129
- it 'destroy child who parent has.' do
130
- expect{@parent.destroy}.to change(ParanoidSingleChildModel, :count).by(-1)
131
- expect(@first.reload.destroyed?).to be_true
132
- end
133
-
134
- it 'not destroy child who parent does not have.' do
135
- @parent.destroy
136
- expect(@hitori.reload.destroyed?).to be_false
137
- end
138
- end
139
-
140
- context 'when parent be restored' do
141
- before :each do
142
- @parent.destroy
143
- @hitori.destroy
144
- end
145
-
146
- it 'restore child who parent has.' do
147
- expect{@parent.restore!}.to change(ParanoidSingleChildModel, :count).by(1)
148
- expect(@first.reload.destroyed?).to be_false
149
- end
150
-
151
- it 'not restore child who parent does not have.' do
152
- @parent.restore
153
- expect(@hitori.reload.destroyed?).to be_true
154
- end
155
- end
156
-
157
- context 'when delete child before parent was deleted' do
158
- before :each do
159
- # First, delete child.
160
- delete_at = 1.second.ago
161
- Time.stub(:now).and_return(delete_at)
162
- @first.destroy
163
-
164
- # Next, delete parent.
165
- Time.unstub(:now)
166
- @parent.destroy
167
- end
168
-
169
- it 'not restore child who deleted before parent was deleted' do
170
- expect{@parent.restore!}.to change(ParanoidSingleChildModel, :count).by(0)
171
- expect(@first.reload.destroyed?).to be_true
172
- end
173
- end
174
- end
175
-
176
- describe 'ParanoidSingleChildModel(belongs_to)' do
177
- before :each do
178
- @first = ParanoidSingleChildModel.create!
179
- @parent = ParentModel.create!(child_id: @first.id)
180
-
181
- @hitori = ParanoidSingleChildModel.create!
182
- end
183
-
184
- context 'when parent be destroyed' do
185
- it 'destroy child who parent has.' do
186
- expect{@parent.destroy}.to change(ParanoidSingleChildModel, :count).by(-1)
187
- expect(@first.reload.destroyed?).to be_true
188
- end
189
-
190
- it 'not destroy child who parent does not have.' do
191
- @parent.destroy
192
- expect(@hitori.reload.destroyed?).to be_false
193
- end
194
- end
195
-
196
- context 'when parent be restored' do
197
- before :each do
198
- @parent.destroy
199
- @hitori.destroy
200
- end
201
-
202
- it 'restore child who parent has.' do
203
- expect{@parent.restore!}.to change(ParanoidSingleChildModel, :count).by(1)
204
- expect(@first.reload.destroyed?).to be_false
205
- end
206
-
207
- it 'not restore child who parent does not have.' do
208
- @parent.restore
209
- expect(@hitori.reload.destroyed?).to be_true
210
- end
211
- end
212
-
213
- context 'when delete child before parent was deleted' do
214
- before :each do
215
- # First, delete child.
216
- delete_at = 1.second.ago
217
- Time.stub(:now).and_return(delete_at)
218
- @first.destroy
219
-
220
- # Next, delete parent.
221
- Time.unstub(:now)
222
- @parent.destroy
223
- end
224
-
225
- it 'not restore child who deleted before parent was deleted' do
226
- expect{@parent.restore!}.to change(ParanoidSingleChildModel, :count).by(0)
227
- expect(@first.reload.destroyed?).to be_true
228
- end
229
- end
230
- end
231
-
232
- describe 'ChildModels' do
233
- before :each do
234
- @parent = ParentModel.create!
235
-
236
- @normal_first = @parent.normal_child_models.create!
237
- @normal_second = @parent.normal_child_models.create!
238
-
239
- @paranoid_first = @parent.paranoid_child_models.create!
240
- @paranoid_second = @parent.paranoid_child_models.create!
241
-
242
- @single_first = ParanoidSingleChildModel.create!(parent_model_id: @parent.id)
243
-
244
- @normal_hitori = NormalChildModel.create!
245
- @paranoid_hitori = ParanoidChildModel.create!
246
- @single_hitori = ParanoidSingleChildModel.create!
247
- end
248
-
249
- context 'when parent be hard-destroyed' do
250
- it 'hard-destroy normal children who parent has.' do
251
- expect{@parent.destroy!}.to change(NormalChildModel, :count).by(-2)
252
- end
253
-
254
- it 'hard-destroy paranoid children who parent has.' do
255
- expect{@parent.destroy!}.to change{ParanoidChildModel.with_deleted.count}.by(-2)
256
- end
257
-
258
- it 'hard-destroy paranoid single child who parent has.' do
259
- expect{@parent.destroy!}.to change{ParanoidSingleChildModel.with_deleted.count}.by(-1)
260
- end
261
-
262
- it 'not destroy child who parent does not have.' do
263
- @parent.destroy!
264
- expect(@normal_hitori.reload.destroyed?).to be_false
265
- expect(@paranoid_hitori.reload.destroyed?).to be_false
266
- expect(@single_hitori.reload.destroyed?).to be_false
267
- end
268
- end
269
- end
270
- end
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Kakurenbo::SoftDeleteCore::Callbacks do
4
- create_temp_table(:paranoid_models){ |t| t.datetime :deleted_at }
5
-
6
- before :each do
7
- class ParanoidModel < ActiveRecord::Base; end
8
- end
9
-
10
- after :each do
11
- Object.class_eval{ remove_const :ParanoidModel }
12
- end
13
-
14
- context 'when run callback of restore' do
15
- before :each do
16
- @callback_model = ParanoidModel.create!
17
- end
18
-
19
- after :each do
20
- @callback_model.run_callbacks(:restore)
21
- end
22
-
23
- it 'call before_restore.' do
24
- ParanoidModel.before_restore :before_restore_callback
25
- @callback_model.should_receive(:before_restore_callback).once
26
- end
27
-
28
- it 'call around_restore.' do
29
- ParanoidModel.around_restore :around_restore_callback
30
- @callback_model.should_receive(:around_restore_callback).once
31
- end
32
-
33
- it 'call after_restore.' do
34
- ParanoidModel.after_restore :after_restore_callback
35
- @callback_model.should_receive(:after_restore_callback).once
36
- end
37
- end
38
- end
@@ -1,229 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Kakurenbo::SoftDeleteCore do
4
- create_temp_table(:paranoid_models){ |t| t.datetime :deleted_at }
5
-
6
- before :each do
7
- class ParanoidModel < ActiveRecord::Base; end
8
- end
9
-
10
- after :each do
11
- Object.class_eval{ remove_const :ParanoidModel }
12
- end
13
-
14
- describe 'Instance of paranoid model' do
15
- before :each do
16
- @model = ParanoidModel.create!
17
- end
18
-
19
- context 'when delete' do
20
- it 'soft-delete model.' do
21
- expect{
22
- @model.delete
23
- }.to change(ParanoidModel, :count).by(-1)
24
- end
25
-
26
- it 'not hard-delete model.' do
27
- expect{
28
- @model.delete
29
- }.to change{
30
- ParanoidModel.all.with_deleted.count
31
- }.by(0)
32
- end
33
-
34
- it 'call callbacks nothing.' do
35
- ParanoidModel.before_destroy :before_destroy_callback
36
- ParanoidModel.after_destroy :after_destroy_callback
37
- ParanoidModel.after_commit :after_commit_callback
38
-
39
- @model.should_receive(:before_destroy_callback).exactly(0).times
40
- @model.should_receive(:after_destroy_callback).exactly(0).times
41
- @model.should_receive(:after_commit_callback).exactly(0).times
42
-
43
- @model.delete
44
- end
45
-
46
- it 'not call callbacks other.' do
47
- ParanoidModel.before_update :before_update_callback
48
- ParanoidModel.before_save :before_save_callback
49
- ParanoidModel.validate :validate_callback
50
-
51
- @model.should_receive(:before_update_callback).exactly(0).times
52
- @model.should_receive(:before_save_callback).exactly(0).times
53
- @model.should_receive(:validate_callback).exactly(0).times
54
-
55
- @model.delete
56
- end
57
- end
58
-
59
- context 'when delete!' do
60
- it 'hard-delete model.' do
61
- expect{
62
- @model.delete!
63
- }.to change{
64
- ParanoidModel.all.with_deleted.count
65
- }.by(-1)
66
- end
67
- end
68
-
69
- context 'when destroy(class method)' do
70
- before :each do
71
- @model2 = ParanoidModel.create!
72
- end
73
-
74
- it 'with id, destroy model.' do
75
- expect{
76
- ParanoidModel.destroy(@model.id)
77
- }.to change(ParanoidModel, :count).by(-1)
78
-
79
- expect(@model.reload.destroyed?).to be_true
80
- end
81
-
82
- it 'with ids, destroy models.' do
83
- expect{
84
- ParanoidModel.destroy([@model.id, @model2.id])
85
- }.to change(ParanoidModel, :count).by(-2)
86
-
87
- expect(@model.reload.destroyed?).to be_true
88
- expect(@model2.reload.destroyed?).to be_true
89
- end
90
- end
91
-
92
- context 'when destroy(instance method)' do
93
- it 'soft-delete model.' do
94
- expect{
95
- @model.destroy
96
- }.to change(ParanoidModel, :count).by(-1)
97
- end
98
-
99
- it 'not hard-delete model.' do
100
- expect{
101
- @model.destroy
102
- }.to change{
103
- ParanoidModel.all.with_deleted.count
104
- }.by(0)
105
- end
106
-
107
- it 'call callbacks of destroy.' do
108
- ParanoidModel.before_destroy :before_destroy_callback
109
- ParanoidModel.after_destroy :after_destroy_callback
110
- ParanoidModel.after_commit :after_commit_callback
111
-
112
- @model.should_receive(:before_destroy_callback).once
113
- @model.should_receive(:after_destroy_callback).once
114
- @model.should_receive(:after_commit_callback).once.and_return(true)
115
-
116
- @model.destroy
117
- end
118
-
119
- it 'not call callbacks without destroy.' do
120
- ParanoidModel.before_update :before_update_callback
121
- ParanoidModel.before_save :before_save_callback
122
- ParanoidModel.validate :validate_callback
123
-
124
- @model.should_receive(:before_update_callback).exactly(0).times
125
- @model.should_receive(:before_save_callback).exactly(0).times
126
- @model.should_receive(:validate_callback).exactly(0).times
127
-
128
- @model.destroy
129
- end
130
- end
131
-
132
- context 'when destroy!' do
133
- it 'hard-delete model.' do
134
- expect{
135
- @model.destroy!
136
- }.to change{
137
- ParanoidModel.all.with_deleted.count
138
- }.by(-1)
139
- end
140
- end
141
-
142
- context 'when destroyed?' do
143
- it 'false if model not destroyed.' do
144
- expect(@model.destroyed?).to be_false
145
- end
146
-
147
- it 'false if model destroyed.' do
148
- @model.destroy
149
- expect(@model.destroyed?).to be_true
150
- end
151
-
152
- it 'alias deleted? to destroyed?' do
153
- expect(@model.deleted?).to be_false
154
- end
155
- end
156
-
157
- context 'when restore(class method)' do
158
- before :each do
159
- @model.destroy
160
-
161
- @model2 = ParanoidModel.create!
162
- @model2.destroy
163
- end
164
-
165
- it 'with id, restore model.' do
166
- expect{
167
- ParanoidModel.restore(@model.id)
168
- }.to change(ParanoidModel, :count).by(1)
169
-
170
- expect(@model.reload.destroyed?).to be_false
171
- end
172
-
173
- it 'with ids, restore models.' do
174
- expect{
175
- ParanoidModel.restore([@model.id, @model2.id])
176
- }.to change(ParanoidModel, :count).by(2)
177
-
178
- expect(@model.reload.destroyed?).to be_false
179
- expect(@model2.reload.destroyed?).to be_false
180
- end
181
- end
182
-
183
- context 'when restore(instance method)' do
184
- before :each do
185
- @model.destroy
186
- end
187
-
188
- it 'restore model' do
189
- expect{
190
- @model.restore!
191
- }.to change(ParanoidModel, :count).by(1)
192
-
193
- expect(@model.reload.destroyed?).to be_false
194
- end
195
-
196
- it 'call callbacks of restore.' do
197
- ParanoidModel.before_restore :before_restore_callback
198
- ParanoidModel.after_restore :after_restore_callback
199
- ParanoidModel.after_commit :after_commit_callback
200
-
201
- @model.should_receive(:before_restore_callback).once
202
- @model.should_receive(:after_restore_callback).once
203
- @model.should_receive(:after_commit_callback).once.and_return(true)
204
-
205
- @model.restore!
206
- end
207
-
208
- it 'not call callbacks without restore.' do
209
- ParanoidModel.before_update :before_update_callback
210
- ParanoidModel.before_save :before_save_callback
211
- ParanoidModel.validate :validate_callback
212
-
213
- @model.should_receive(:before_update_callback).exactly(0).times
214
- @model.should_receive(:before_save_callback).exactly(0).times
215
- @model.should_receive(:validate_callback).exactly(0).times
216
-
217
- @model.restore!
218
- end
219
-
220
- it 'alias recover to restore!' do
221
- expect{
222
- @model.restore!
223
- }.to change(ParanoidModel, :count).by(1)
224
-
225
- expect(@model.reload.destroyed?).to be_false
226
- end
227
- end
228
- end
229
- end