File: machine_with_event_attributes_on_save_test.rb

package info (click to toggle)
ruby-state-machines-activerecord 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: ruby: 1,922; makefile: 5
file content (244 lines) | stat: -rw-r--r-- 6,321 bytes parent folder | download | duplicates (2)
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
require_relative 'test_helper'

class MachineWithEventAttributesOnSaveTest < BaseTestCase
  def setup
    @model = new_model
    @machine = StateMachines::Machine.new(@model)
    @machine.event :ignite do
      transition :parked => :idling
    end

    @record = @model.new
    @record.state = 'parked'
    @record.state_event = 'ignite'
  end

  def test_should_fail_if_event_is_invalid
    @record.state_event = 'invalid'
    assert_equal false, @record.save
  end

  def test_should_fail_if_event_has_no_transition
    @record.state = 'idling'
    assert_equal false, @record.save
  end

  def test_should_run_before_callbacks
    ran_callback = false
    @machine.before_transition { ran_callback = true }

    @record.save
    assert ran_callback
  end

  def test_should_run_before_callbacks_once
    before_count = 0
    @machine.before_transition { before_count += 1 }

    @record.save
    assert_equal 1, before_count
  end

  def test_should_run_around_callbacks_before_yield
    ran_callback = false
    @machine.around_transition { |block| ran_callback = true; block.call }

    @record.save
    assert ran_callback
  end

  def test_should_run_around_callbacks_before_yield_once
    around_before_count = 0
    @machine.around_transition { |block| around_before_count += 1; block.call }

    @record.save
    assert_equal 1, around_before_count
  end

  def test_should_persist_new_state
    @record.save
    assert_equal 'idling', @record.state
  end

  def test_should_run_after_callbacks
    ran_callback = false
    @machine.after_transition { ran_callback = true }

    @record.save
    assert ran_callback
  end

  def test_should_not_run_after_callbacks_with_failures_disabled_if_fails
    @model.before_create { |record| abort_from_callback }

    ran_callback = false
    @machine.after_transition { ran_callback = true }

    begin
      ; @record.save;
    rescue;
    end
    refute ran_callback
  end

  def test_should_run_failure_callbacks__if_fails
    @model.before_create { |record| abort_from_callback }

    ran_callback = false
    @machine.after_failure { ran_callback = true }

    begin
      ; @record.save;
    rescue;
    end
    assert ran_callback
  end

  def test_should_not_run_around_callbacks_if_fails
    @model.before_create { |record| abort_from_callback }

    ran_callback = false
    @machine.around_transition { |block| block.call; ran_callback = true }

    begin
      ; @record.save;
    rescue;
    end
    refute ran_callback
  end

  def test_should_run_around_callbacks_after_yield
    ran_callback = false
    @machine.around_transition { |block| block.call; ran_callback = true }

    @record.save
    assert ran_callback
  end

  def test_should_run_before_transitions_within_transaction
    @machine.before_transition { @model.create; raise ActiveRecord::Rollback }

    begin
      @record.save
    rescue Exception
    end

    assert_equal 0, @model.count
  end

  def test_should_run_after_transitions_within_transaction
    @machine.after_transition { @model.create; raise ActiveRecord::Rollback }

    begin
      @record.save
    rescue Exception
    end

    assert_equal 0, @model.count
  end

  def test_should_run_around_transition_within_transaction
    @machine.around_transition { @model.create; raise ActiveRecord::Rollback }

    begin
      @record.save
    rescue Exception
    end

    assert_equal 0, @model.count
  end

  def test_should_allow_additional_transitions_to_new_state_in_after_transitions
    @machine.event :park do
      transition :idling => :parked
    end

    @machine.after_transition(:on => :ignite) { @record.park }

    @record.save
    assert_equal 'parked', @record.state

    @record.reload
    assert_equal 'parked', @record.state
  end

  def test_should_allow_additional_transitions_to_previous_state_in_after_transitions
    @machine.event :shift_up do
      transition :idling => :first_gear
    end

    @machine.after_transition(:on => :ignite) { @record.shift_up }

    @record.save
    assert_equal 'first_gear', @record.state

    @record.reload
    assert_equal 'first_gear', @record.state
  end

  def test_should_yield_one_model!
    assert_equal true, @record.save!
    assert_equal 1, @model.count
  end

  # explicit tests of #save and #save! to ensure expected behavior
  def test_should_yield_two_models_with_before
    @machine.before_transition { @model.create! }
    assert_equal true, @record.save
    assert_equal 2, @model.count
  end

  def test_should_yield_two_models_with_before!
    @machine.before_transition { @model.create! }
    assert_equal true, @record.save!
    assert_equal 2, @model.count
  end

  def test_should_raise_on_around_transition_rollback!
    @machine.before_transition { @model.create! }
    @machine.around_transition { @model.create!; raise ActiveRecord::Rollback }

    raised = false
    begin
      @record.save!
    rescue Exception
      raised = true
    end

    assert_equal true, raised
    assert_equal 0, @model.count
  end

  def test_should_return_nil_on_around_transition_rollback
    @machine.before_transition { @model.create! }
    @machine.around_transition { @model.create!; raise ActiveRecord::Rollback }
    assert_nil @record.save
    assert_equal 0, @model.count
  end

  def test_should_return_nil_on_before_transition_rollback
    @machine.before_transition { raise ActiveRecord::Rollback }
    assert_nil @record.save
    assert_equal 0, @model.count
  end

  #
  # @rosskevin - This fails and I'm not sure why, it was existing behavior.
  #   see: https://github.com/state-machines/state_machines-activerecord/pull/26#issuecomment-112911886
  #
  # def test_should_yield_three_models_with_before_and_around_save
  #   @machine.before_transition { @model.create!; puts "before ran, now #{@model.count}" }
  #   @machine.around_transition { @model.create!; puts "around ran, now #{@model.count}" }
  #
  #   assert_equal true, @record.save
  #   assert_equal 3, @model.count
  # end
  #
  # def test_should_yield_three_models_with_before_and_around_save!
  #   @machine.before_transition { @model.create!; puts "before ran, now #{@model.count}" }
  #   @machine.around_transition { @model.create!; puts "around ran, now #{@model.count}" }
  #
  #   assert_equal true, @record.save!
  #   assert_equal 3, @model.count
  # end
end