File: instance_hooks_spec.rb

package info (click to toggle)
ruby-sequel 5.41.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,548 kB
  • sloc: ruby: 104,241; makefile: 3
file content (246 lines) | stat: -rw-r--r-- 6,953 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
require_relative "spec_helper"

describe "InstanceHooks plugin" do
  def r(x=nil)
    @r << x
    yield if block_given?
    x
  end
  
  before do
    @c = Class.new(Sequel::Model(:items))
    @c.plugin :instance_hooks
    @c.raise_on_save_failure = false
    @o = @c.new
    @x = @c.load({:id=>1})
    @r = []
  end
  
  it "should support before_create_hook and after_create_hook" do
    @o.after_create_hook{r 1}
    @o.before_create_hook{r 2}
    @o.after_create_hook{r 3}
    @o.before_create_hook{r 4}
    @o.save.wont_equal nil
    @r.must_equal [4, 2, 1, 3]
  end

  it "should cancel the save if before_create_hook block calls cancel_action" do
    @o.after_create_hook{r 1}
    @o.before_create_hook{r{@o.cancel_action}}
    @o.before_create_hook{r 4}
    @o.save.must_be_nil
    @r.must_equal [4, nil]
    @r.clear
    @o.save.must_be_nil
    @r.must_equal [4, nil]
  end

  it "should support before_update_hook and after_update_hook" do
    @x.after_update_hook{r 1}
    @x.before_update_hook{r 2}
    @x.after_update_hook{r 3}
    @x.before_update_hook{r 4}
    @x.save.wont_equal nil
    @r.must_equal [4, 2, 1, 3]
    @x.save.wont_equal nil
    @r.must_equal [4, 2, 1, 3]
  end

  it "should cancel the save if before_update_hook block calls cancel_action" do
    @x.after_update_hook{r 1}
    @x.before_update_hook{r{@x.cancel_action}}
    @x.before_update_hook{r 4}
    @x.save.must_be_nil
    @r.must_equal [4, nil]
    @r.clear
    @x.save.must_be_nil
    @r.must_equal [4, nil]
  end

  it "should support before_save_hook and after_save_hook" do
    @o.after_save_hook{r 1}
    @o.before_save_hook{r 2}
    @o.after_save_hook{r 3}
    @o.before_save_hook{r 4}
    @o.save.wont_equal nil
    @r.must_equal [4, 2, 1, 3]
    @r.clear
    
    @x.after_save_hook{r 1}
    @x.before_save_hook{r 2}
    @x.after_save_hook{r 3}
    @x.before_save_hook{r 4}
    @x.save.wont_equal nil
    @r.must_equal [4, 2, 1, 3]
    @x.save.wont_equal nil
    @r.must_equal [4, 2, 1, 3]
  end

  it "should cancel the save if before_save_hook block calls cancel_action" do
    @x.after_save_hook{r 1}
    @x.before_save_hook{r{@x.cancel_action}}
    @x.before_save_hook{r 4}
    @x.save.must_be_nil
    @r.must_equal [4, nil]
    @r.clear
    
    @x.after_save_hook{r 1}
    @x.before_save_hook{r{@x.cancel_action}}
    @x.before_save_hook{r 4}
    @x.save.must_be_nil
    @r.must_equal [4, nil]
    @r.clear
    @x.save.must_be_nil
    @r.must_equal [4, nil]
  end

  it "should support before_destroy_hook and after_destroy_hook" do
    @x.after_destroy_hook{r 1}
    @x.before_destroy_hook{r 2}
    @x.after_destroy_hook{r 3}
    @x.before_destroy_hook{r 4}
    @x.destroy.wont_equal nil
    @r.must_equal [4, 2, 1, 3]
  end

  it "should cancel the destroy if before_destroy_hook block calls cancel_action" do
    @x.after_destroy_hook{r 1}
    @x.before_destroy_hook{r{@x.cancel_action}}
    @x.before_destroy_hook{r 4}
    @x.destroy.must_be_nil
    @r.must_equal [4, nil]
  end

  it "should support before_validation_hook and after_validation_hook" do
    @o.after_validation_hook{r 1}
    @o.before_validation_hook{r 2}
    @o.after_validation_hook{r 3}
    @o.before_validation_hook{r 4}
    @o.valid?.must_equal true
    @r.must_equal [4, 2, 1, 3]
  end

  it "should cancel the save if before_validation_hook block calls cancel_action" do
    @o.after_validation_hook{r 1}
    @o.before_validation_hook{r{@o.cancel_action}}
    @o.before_validation_hook{r 4}
    @o.valid?.must_equal false
    @r.must_equal [4, nil]
    @r.clear
    @o.valid?.must_equal false
    @r.must_equal [4, nil]
  end

  it "should clear only related hooks on successful create" do
    @o.after_destroy_hook{r 1}
    @o.before_destroy_hook{r 2}
    @o.after_update_hook{r 3}
    @o.before_update_hook{r 4}
    @o.before_save_hook{r 5}
    @o.after_save_hook{r 6}
    @o.before_create_hook{r 7}
    @o.after_create_hook{r 8}
    @o.save.wont_equal nil
    @r.must_equal [5, 7, 8, 6]
    @o.instance_variable_set(:@new, false)
    @o.save.wont_equal nil
    @r.must_equal [5, 7, 8, 6, 4, 3]
    @o.save.wont_equal nil
    @r.must_equal [5, 7, 8, 6, 4, 3]
    @o.destroy
    @r.must_equal [5, 7, 8, 6, 4, 3, 2, 1]
  end

  it "should clear only related hooks on successful update" do
    @x.after_destroy_hook{r 1}
    @x.before_destroy_hook{r 2}
    @x.before_update_hook{r 3}
    @x.after_update_hook{r 4}
    @x.before_save_hook{r 5}
    @x.after_save_hook{r 6}
    @x.save.wont_equal nil
    @r.must_equal [5, 3, 4, 6]
    @x.save.wont_equal nil
    @r.must_equal [5, 3, 4, 6]
    @x.destroy
    @r.must_equal [5, 3, 4, 6, 2, 1]
  end

  it "should clear only related hooks on successful destroy" do
    @x.after_destroy_hook{r 1}
    @x.before_destroy_hook{r 2}
    @x.before_update_hook{r 3}
    @x.before_save_hook{r 4}
    @x.destroy
    @r.must_equal [2, 1]
    @x.save.wont_equal nil
    @r.must_equal [2, 1, 4, 3]
  end

  it "should not clear validations hooks on successful save" do
    @x.after_validation_hook{@x.errors.add(:id, 'a') if @x.id == 1; r 1}
    @x.before_validation_hook{r 2}
    @x.save.must_be_nil
    @r.must_equal [2, 1]
    @x.save.must_be_nil
    @r.must_equal [2, 1, 2, 1]
    @x.id = 2
    @x.save.must_equal @x
    @r.must_equal [2, 1, 2, 1, 2, 1]
    @x.save.must_equal @x
    @r.must_equal [2, 1, 2, 1, 2, 1]
  end

  it "should not allow addition of instance hooks to frozen instances" do
    @x.after_destroy_hook{r 1}
    @x.before_destroy_hook{r 2}
    @x.before_update_hook{r 3}
    @x.before_save_hook{r 4}
    @x.freeze
    proc{@x.after_destroy_hook{r 1}}.must_raise(Sequel::Error)
    proc{@x.before_destroy_hook{r 2}}.must_raise(Sequel::Error)
    proc{@x.before_update_hook{r 3}}.must_raise(Sequel::Error)
    proc{@x.before_save_hook{r 4}}.must_raise(Sequel::Error)
  end
end

describe "InstanceHooks plugin with transactions" do
  before do
    @db = Sequel.mock(:numrows=>1)
    @c = Class.new(Sequel::Model(@db[:items])) do
      attr_accessor :rb
      def after_save
        super
        db.execute('as')
        raise Sequel::Rollback if rb
      end
      def after_destroy
        super
        db.execute('ad')
        raise Sequel::Rollback if rb
      end
    end
    @c.use_transactions = true
    @c.plugin :instance_hooks
    @o = @c.load({:id=>1})
    @or = @c.load({:id=>1})
    @or.rb = true
    @r = []
    @db.sqls
  end
  
  it "should have *_hook methods return self "do
    @o.before_destroy_hook{r 1}.must_be_same_as(@o)
    @o.before_validation_hook{r 1}.must_be_same_as(@o)
    @o.before_save_hook{r 1}.must_be_same_as(@o)
    @o.before_update_hook{r 1}.must_be_same_as(@o)
    @o.before_create_hook{r 1}.must_be_same_as(@o)

    @o.after_destroy_hook{r 1}.must_be_same_as(@o)
    @o.after_validation_hook{r 1}.must_be_same_as(@o)
    @o.after_save_hook{r 1}.must_be_same_as(@o)
    @o.after_update_hook{r 1}.must_be_same_as(@o)
    @o.after_create_hook{r 1}.must_be_same_as(@o)
  end
end