File: machine_with_event_attributes_on_validation_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 (143 lines) | stat: -rw-r--r-- 3,347 bytes parent folder | download
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
require_relative 'test_helper'

class MachineWithEventAttributesOnValidationTest < 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'
    refute @record.valid?
    assert_equal ['State event is invalid'], @record.errors.full_messages
  end

  def test_should_fail_if_event_has_no_transition
    @record.state = 'idling'
    refute @record.valid?
    assert_equal ['State event cannot transition when idling'], @record.errors.full_messages
  end

  def test_should_be_successful_if_event_has_transition
    assert @record.valid?
  end

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

    @record.valid?
    assert ran_callback
  end

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

    begin
      @record.valid?
    rescue ArgumentError
      raise if StateMachines::Transition.pause_supported?
    end
    assert ran_callback
  end

  def test_should_persist_new_state
    @record.valid?
    assert_equal 'idling', @record.state
  end

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

    @record.valid?
    refute ran_callback
  end

  def test_should_not_run_after_callbacks_with_failures_disabled_if_validation_fails
    @model.class_eval do
      attr_accessor :seatbelt
      validates :seatbelt, presence: true
    end

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

    @record.valid?
    refute ran_callback
  end

  def test_should_run_after_callbacks_if_validation_fails
    @model.class_eval do
      attr_accessor :seatbelt
      validates :seatbelt, presence: true
    end

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

    @record.valid?
    assert ran_callback
  end

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

    begin
      @record.valid?
    rescue ArgumentError
      raise if StateMachines::Transition.pause_supported?
    end
    refute ran_callback
  end

  def test_should_not_run_around_callbacks_after_yield_with_failures_disabled_if_validation_fails
    @model.class_eval do
      attr_accessor :seatbelt
      validates :seatbelt, presence: true
    end

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

    @record.valid?
    refute ran_callback
  end

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

    begin
      @record.valid?
    rescue Exception
    end

    assert_equal 0, @model.count
  end

  def test_should_rollback_before_transitions_with_false
    @machine.before_transition {
      @model.create;
      false
    }

    begin
      @record.valid?
    rescue Exception
    end

    assert_equal 0, @model.count
  end
end