PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/state_machine/test/unit/event_test.rb

https://github.com/tiaohai/spree
Ruby | 235 lines | 190 code | 45 blank | 0 comment | 0 complexity | 9dea1a0f8a523d77095edfd4d9b06391 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class EventTest < Test::Unit::TestCase
  3. def setup
  4. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state')
  5. @event = PluginAWeek::StateMachine::Event.new(@machine, 'turn_on')
  6. @switch = new_switch
  7. end
  8. def test_should_have_a_machine
  9. assert_equal @machine, @event.machine
  10. end
  11. def test_should_have_a_name
  12. assert_equal 'turn_on', @event.name
  13. end
  14. def test_should_not_have_any_transitions
  15. assert @event.transitions.empty?
  16. end
  17. def test_should_define_an_event_action_on_the_owner_class
  18. assert @switch.respond_to?(:turn_on)
  19. end
  20. def test_should_define_an_event_bang_action_on_the_owner_class
  21. assert @switch.respond_to?(:turn_on!)
  22. end
  23. def test_should_define_an_event_predicate_on_the_owner_class
  24. assert @switch.respond_to?(:can_turn_on?)
  25. end
  26. def test_should_raise_exception_if_invalid_option_specified
  27. assert_raise(ArgumentError) {PluginAWeek::StateMachine::Event.new(@machine, 'turn_on', :invalid => true)}
  28. end
  29. end
  30. class EventDefiningTransitionsTest < Test::Unit::TestCase
  31. def setup
  32. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state', :initial => 'off')
  33. @event = PluginAWeek::StateMachine::Event.new(@machine, 'turn_on')
  34. end
  35. def test_should_raise_exception_if_invalid_option_specified
  36. assert_raise(ArgumentError) {@event.transition(:invalid => true)}
  37. end
  38. def test_should_raise_exception_if_to_option_not_specified
  39. assert_raise(ArgumentError) {@event.transition(:from => 'off')}
  40. end
  41. def test_should_not_raise_exception_if_from_option_not_specified
  42. assert_nothing_raised {@event.transition(:to => 'on')}
  43. end
  44. def test_should_allow_transitioning_without_a_from_state
  45. assert @event.transition(:to => 'on')
  46. end
  47. def test_should_allow_transitioning_from_a_single_state
  48. assert @event.transition(:to => 'on', :from => 'off')
  49. end
  50. def test_should_allow_transitioning_from_multiple_states
  51. assert @event.transition(:to => 'on', :from => %w(off on))
  52. end
  53. def test_should_have_transitions
  54. transition = @event.transition(:to => 'on')
  55. assert_equal [transition], @event.transitions
  56. end
  57. end
  58. class EventAfterBeingCopiedTest < Test::Unit::TestCase
  59. def setup
  60. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state', :initial => 'off')
  61. @event = PluginAWeek::StateMachine::Event.new(@machine, 'turn_on')
  62. @copied_event = @event.dup
  63. end
  64. def test_should_not_have_the_same_collection_of_transitions
  65. assert_not_same @copied_event.transitions, @event.transitions
  66. end
  67. end
  68. class EventWithoutTransitionsTest < Test::Unit::TestCase
  69. def setup
  70. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state', :initial => 'off')
  71. @event = PluginAWeek::StateMachine::Event.new(@machine, 'turn_on')
  72. @switch = create_switch(:state => 'off')
  73. end
  74. def test_should_not_be_able_to_fire
  75. assert !@event.can_fire?(@switch)
  76. end
  77. def test_should_not_fire
  78. assert !@event.fire(@switch)
  79. end
  80. def test_should_not_change_the_current_state
  81. @event.fire(@switch)
  82. assert_equal 'off', @switch.state
  83. end
  84. def test_should_raise_exception_during_fire!
  85. assert_raise(PluginAWeek::StateMachine::InvalidTransition) {@event.fire!(@switch)}
  86. end
  87. end
  88. class EventWithTransitionsTest < Test::Unit::TestCase
  89. def setup
  90. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state', :initial => 'off')
  91. @event = PluginAWeek::StateMachine::Event.new(@machine, 'turn_on')
  92. @event.transition :to => 'error', :from => 'on'
  93. @switch = create_switch(:state => 'off')
  94. end
  95. def test_should_not_be_able_to_fire_if_no_transitions_are_matched
  96. assert !@event.can_fire?(@switch)
  97. end
  98. def test_should_not_fire_if_no_transitions_are_matched
  99. assert !@event.fire(@switch)
  100. assert_equal 'off', @switch.state
  101. end
  102. def test_should_raise_exception_if_no_transitions_are_matched_during_fire!
  103. assert_raise(PluginAWeek::StateMachine::InvalidTransition) {@event.fire!(@switch)}
  104. assert_equal 'off', @switch.state
  105. end
  106. def test_should_be_able_to_fire_if_transition_is_matched
  107. @event.transition :to => 'on'
  108. assert @event.can_fire?(@switch)
  109. end
  110. def test_should_fire_if_transition_is_matched
  111. @event.transition :to => 'on'
  112. assert @event.fire(@switch)
  113. assert_equal 'on', @switch.state
  114. end
  115. def test_should_fire_if_transition_with_from_state_is_matched
  116. @event.transition :to => 'on', :from => 'off'
  117. assert @event.fire(@switch)
  118. assert_equal 'on', @switch.state
  119. end
  120. def test_should_fire_if_transition_with_multiple_from_states_is_matched
  121. @event.transition :to => 'on', :from => %w(off on)
  122. assert @event.fire(@switch)
  123. assert_equal 'on', @switch.state
  124. end
  125. def test_should_not_fire_if_validation_failed
  126. @event.transition :to => 'on', :from => 'off'
  127. @switch.fail_validation = true
  128. assert !@event.fire(@switch)
  129. @switch.reload
  130. assert_equal 'off', @switch.state
  131. end
  132. def test_should_raise_exception_if_validation_failed_during_fire!
  133. @event.transition :to => 'on', :from => 'off'
  134. @switch.fail_validation = true
  135. assert_raise(ActiveRecord::RecordInvalid) {@event.fire!(@switch)}
  136. end
  137. def test_should_not_fire_if_save_failed
  138. @event.transition :to => 'on', :from => 'off'
  139. @switch.fail_save = true
  140. assert !@event.fire(@switch)
  141. @switch.reload
  142. assert_equal 'off', @switch.state
  143. end
  144. def test_should_raise_exception_if_save_failed_during_fire!
  145. @event.transition :to => 'on', :from => 'off'
  146. @switch.fail_save = true
  147. assert_raise(ActiveRecord::RecordNotSaved) {@event.fire!(@switch)}
  148. end
  149. def test_should_not_raise_exception_if_transition_is_matched_during_fire!
  150. @event.transition :to => 'on', :from => 'off'
  151. assert @event.fire!(@switch)
  152. assert_equal 'on', @switch.state
  153. end
  154. end
  155. class EventWithinTransactionTest < Test::Unit::TestCase
  156. def setup
  157. @machine = PluginAWeek::StateMachine::Machine.new(Switch, 'state', :initial => 'off')
  158. @event = PluginAWeek::StateMachine::Event.new(@machine, 'turn_on')
  159. @event.transition :to => 'on', :from => 'off'
  160. @switch = create_switch(:state => 'off')
  161. Switch.define_callbacks :before_transition_state
  162. end
  163. def test_should_save_all_records_within_transaction_if_performed
  164. Switch.before_transition_state lambda {|record| Switch.create(:state => 'pending'); true}, :from => 'off'
  165. assert @event.fire(@switch)
  166. assert_equal 'on', @switch.state
  167. assert_equal 'pending', Switch.find(:all).last.state
  168. end
  169. uses_transaction :test_should_rollback_all_records_within_transaction_if_not_performed
  170. def test_should_rollback_all_records_within_transaction_if_not_performed
  171. Switch.before_transition_state lambda {|record| Switch.create(:state => 'pending'); false}, :from => 'off'
  172. assert !@event.fire(@switch)
  173. assert_equal 1, Switch.count
  174. ensure
  175. Switch.destroy_all
  176. end
  177. uses_transaction :test_should_rollback_all_records_within_transaction_if_not_performed!
  178. def test_should_rollback_all_records_within_transaction_if_not_performed!
  179. Switch.before_transition_state lambda {|record| Switch.create(:state => 'pending'); false}, :from => 'off'
  180. assert_raise(PluginAWeek::StateMachine::InvalidTransition) {@event.fire!(@switch)}
  181. assert_equal 1, Switch.count
  182. ensure
  183. Switch.destroy_all
  184. end
  185. def teardown
  186. Switch.class_eval do
  187. @before_transition_state_callbacks = nil
  188. end
  189. end
  190. end