File: machine_with_static_initial_state_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 (167 lines) | stat: -rw-r--r-- 4,741 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require_relative 'test_helper'

class MachineWithStaticInitialStateTest < BaseTestCase
  def setup
    @model = new_model(:vehicle) do
      attr_accessor :value
    end
    @machine = StateMachines::Machine.new(@model, :initial => :parked)
  end

  def test_should_set_initial_state_on_created_object
    record = @model.new
    assert_equal 'parked', record.state
  end

  def test_should_set_initial_state_with_nil_attributes
    record = @model.new(nil)
    assert_equal 'parked', record.state
  end

  def test_should_still_set_attributes
    record = @model.new(:value => 1)
    assert_equal 1, record.value
  end

  def test_should_still_allow_initialize_blocks
    block_args = nil
    record = @model.new do |*args|
      block_args = args
    end

    assert_equal [record], block_args
  end

  def test_should_set_attributes_prior_to_initialize_block
    state = nil
    @model.new do |record|
      state = record.state
    end

    assert_equal 'parked', state
  end

  def test_should_set_attributes_prior_to_after_initialize_hook
    state = nil
    @model.after_initialize do |record|
      state = record.state
    end
    @model.new
    assert_equal 'parked', state
  end

  def test_should_set_initial_state_before_setting_attributes
    @model.class_eval do
      attr_accessor :state_during_setter

      remove_method :value=
      define_method(:value=) do |value|
        self.state_during_setter = state
      end
    end
    record = @model.new
    record.value = 1
    assert_equal 'parked', record.state_during_setter
  end

  def test_should_not_set_initial_state_after_already_initialized
    record = @model.new(:value => 1)
    assert_equal 'parked', record.state

    record.state = 'idling'
    record.attributes = {}
    assert_equal 'idling', record.state
  end

  def test_should_persist_initial_state
    record = @model.new
    record.save
    record.reload
    assert_equal 'parked', record.state
  end

  def test_should_persist_initial_state_on_dup
    record = @model.create.dup
    record.save
    record.reload
    assert_equal 'parked', record.state
  end

  def test_should_use_stored_values_when_loading_from_database
    @machine.state :idling

    record = @model.find(@model.create(:state => 'idling').id)
    assert_equal 'idling', record.state
  end

  def test_should_use_stored_values_when_loading_from_database_with_nil_state
    @machine.state nil

    record = @model.find(@model.create(:state => nil).id)
    assert_nil record.state
  end

  def test_should_use_stored_values_when_loading_for_many_association
    @machine.state :idling

    @model.connection.add_column @model.table_name, :owner_id, :integer
    @model.reset_column_information
    MachineWithStaticInitialStateTest.const_set('Vehicle', @model)

    owner_model = new_model(:owner) do
      has_many :vehicles, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
    end
    MachineWithStaticInitialStateTest.const_set('Owner', owner_model)

    owner = owner_model.create
    @model.create(:state => 'idling', :owner_id => owner.id)
    assert_equal 'idling', owner.vehicles[0].state
  end

  def test_should_use_stored_values_when_loading_for_one_association
    @machine.state :idling

    @model.connection.add_column @model.table_name, :owner_id, :integer
    @model.reset_column_information
    MachineWithStaticInitialStateTest.const_set('Vehicle', @model)

    owner_model = new_model(:owner) do
      has_one :vehicle, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
    end
    MachineWithStaticInitialStateTest.const_set('Owner', owner_model)

    owner = owner_model.create
    @model.create(:state => 'idling', :owner_id => owner.id)
    assert_equal 'idling', owner.vehicle.state
  end

  def test_should_use_stored_values_when_loading_for_belongs_to_association
    @machine.state :idling

    MachineWithStaticInitialStateTest.const_set('Vehicle', @model)

    driver_model = new_model(:driver) do
      connection.add_column table_name, :vehicle_id, :integer

      belongs_to :vehicle, :class_name => 'MachineWithStaticInitialStateTest::Vehicle'
    end

    MachineWithStaticInitialStateTest.const_set('Driver', driver_model)

    record = @model.create(:state => 'idling')
    driver = driver_model.create(:vehicle_id => record.id)
    assert_equal 'idling', driver.vehicle.state
  end

  def teardown
    MachineWithStaticInitialStateTest.class_eval do
      remove_const('Vehicle') if defined?(MachineWithStaticInitialStateTest::Vehicle)
      remove_const('Owner') if defined?(MachineWithStaticInitialStateTest::Owner)
      remove_const('Driver') if defined?(MachineWithStaticInitialStateTest::Driver)
    end

    clear_active_support_dependencies
    super
  end
end