Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add tests validating association initialization behavior for all ORM …

…integrations
  • Loading branch information...
commit 5660706f2ddec8a111ce61cee2a655aa535a1eeb 1 parent ee08d10
Aaron Pfeifer obrie authored
64 test/unit/integrations/active_record_test.rb
View
@@ -170,7 +170,7 @@ def test_should_humanize_name
class MachineWithStaticInitialStateTest < BaseTestCase
def setup
- @model = new_model do
+ @model = new_model(:vehicle) do
attr_accessor :value
end
@machine = StateMachine::Machine.new(@model, :initial => :parked)
@@ -271,6 +271,68 @@ def test_should_use_stored_values_when_loading_from_database_with_nil_state
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
+ ActiveRecordTest.const_set('Vehicle', @model)
+
+ owner_model = new_model(:owner) do
+ has_many :vehicles, :class_name => 'ActiveRecordTest::Vehicle'
+ end
+ ActiveRecordTest.const_set('Owner', owner_model)
+
+ owner = owner_model.create
+ record = @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
+ ActiveRecordTest.const_set('Vehicle', @model)
+
+ owner_model = new_model(:owner) do
+ has_one :vehicle, :class_name => 'ActiveRecordTest::Vehicle'
+ end
+ ActiveRecordTest.const_set('Owner', owner_model)
+
+ owner = owner_model.create
+ record = @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
+
+ ActiveRecordTest.const_set('Vehicle', @model)
+
+ driver_model = new_model(:driver) do
+ connection.add_column table_name, :vehicle_id, :integer
+
+ belongs_to :vehicle, :class_name => 'ActiveRecordTest::Vehicle'
+ end
+
+ ActiveRecordTest.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
+ ActiveRecordTest.class_eval do
+ remove_const('Vehicle') if defined?(ActiveRecordTest::Vehicle)
+ remove_const('Owner') if defined?(ActiveRecordTest::Owner)
+ remove_const('Driver') if defined?(ActiveRecordTest::Driver)
+ end
+ ActiveSupport::Dependencies.clear if defined?(ActiveSupport::Dependencies)
+ super
+ end
end
class MachineWithDynamicInitialStateTest < BaseTestCase
48 test/unit/integrations/data_mapper_test.rb
View
@@ -184,7 +184,7 @@ def test_should_humanize_name
class MachineWithStaticInitialStateTest < BaseTestCase
def setup
- @resource = new_resource do
+ @resource = new_resource(:vehicle) do
attr_accessor :value
end
@machine = StateMachine::Machine.new(@resource, :initial => :parked)
@@ -270,6 +270,52 @@ def test_should_use_stored_values_when_loading_from_database_with_nil_state
record = @resource.get(@resource.create(:state => nil).id)
assert_nil record.state
end
+
+ def test_should_use_stored_values_when_loading_for_many_association
+ @machine.state :idling
+
+ @resource.property :owner_id, Integer
+ @resource.auto_migrate!
+
+ owner_resource = new_resource(:owner) do
+ has n, :vehicles
+ end
+
+ owner = owner_resource.create
+ record = @resource.new(:state => 'idling')
+ record.owner_id = owner.id
+ record.save
+ assert_equal 'idling', owner.vehicles[0].state
+ end
+
+ def test_should_use_stored_values_when_loading_for_one_association
+ @machine.state :idling
+
+ @resource.property :owner_id, Integer
+ @resource.auto_migrate!
+
+ owner_resource = new_resource(:owner) do
+ has 1, :vehicle
+ end
+
+ owner = owner_resource.create
+ record = @resource.new(:state => 'idling')
+ record.owner_id = owner.id
+ record.save
+ assert_equal 'idling', owner.vehicle.state
+ end
+
+ def test_should_use_stored_values_when_loading_for_belongs_to_association
+ @machine.state :idling
+
+ driver_resource = new_resource(:driver) do
+ belongs_to :vehicle
+ end
+
+ record = @resource.create(:state => 'idling')
+ driver = driver_resource.create(:vehicle_id => record.id)
+ assert_equal 'idling', driver.vehicle.state
+ end
end
class MachineWithDynamicInitialStateTest < BaseTestCase
61 test/unit/integrations/mongo_mapper_test.rb
View
@@ -144,7 +144,7 @@ def test_should_humanize_name
class MachineWithStaticInitialStateTest < BaseTestCase
def setup
- @model = new_model do
+ @model = new_model(:vehicle) do
attr_accessor :value
end
@machine = StateMachine::Machine.new(@model, :initial => :parked)
@@ -224,6 +224,65 @@ def test_should_use_stored_values_when_loading_from_database_with_nil_state
record = @model.find(@model.create(:state => nil).id)
assert_nil record.state
end
+
+ if defined?(MongoMapper::Version) && MongoMapper::Version !~ /^0\.[5-8]\./
+ def test_should_use_stored_values_when_loading_for_many_association
+ @machine.state :idling
+
+ @model.belongs_to :owner, :class_name => 'MongoMapperTest::Owner'
+ MongoMapperTest.const_set('Vehicle', @model)
+
+ owner_model = new_model(:owner) do
+ many :vehicles, :class_name => 'MongoMapperTest::Vehicle'
+ end
+ MongoMapperTest.const_set('Owner', owner_model)
+
+ owner = owner_model.create
+ record = @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.belongs_to :owner, :class_name => 'MongoMapperTest::Owner'
+ MongoMapperTest.const_set('Vehicle', @model)
+
+ owner_model = new_model(:owner) do
+ one :vehicle, :class_name => 'MongoMapperTest::Vehicle'
+ end
+ MongoMapperTest.const_set('Owner', owner_model)
+
+ owner = owner_model.create
+ record = @model.create(:state => 'idling', :owner_id => owner.id)
+ owner.reload
+ assert_equal 'idling', owner.vehicle.state
+ end
+
+ def test_should_use_stored_values_when_loading_for_belongs_to_association
+ @machine.state :idling
+
+ MongoMapperTest.const_set('Vehicle', @model)
+
+ driver_model = new_model(:driver) do
+ belongs_to :vehicle, :class_name => 'MongoMapperTest::Vehicle'
+ end
+ MongoMapperTest.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
+ end
+
+ def teardown
+ MongoMapperTest.class_eval do
+ remove_const('Vehicle') if defined?(MongoMapperTest::Vehicle)
+ remove_const('Owner') if defined?(MongoMapperTest::Owner)
+ remove_const('Driver') if defined?(MongoMapperTest::Driver)
+ end
+ super
+ end
end
class MachineWithDynamicInitialStateTest < BaseTestCase
73 test/unit/integrations/sequel_test.rb
View
@@ -128,7 +128,7 @@ def test_should_humanize_name
class MachineWithStaticInitialStateTest < BaseTestCase
def setup
- @model = new_model do
+ @model = new_model(:vehicle) do
attr_accessor :value
end
@machine = StateMachine::Machine.new(@model, :initial => :parked)
@@ -223,6 +223,77 @@ def test_should_use_stored_values_when_loading_from_database_with_nil_state
record = @model[@model.create(:state => nil).id]
assert_nil record.state
end
+
+ def test_should_use_stored_values_when_loading_for_many_association
+ @machine.state :idling
+
+ DB.alter_table(@model.table_identifier) do
+ add_column :owner_id, :integer
+ end
+ @model.class_eval { get_db_schema(true) }
+ SequelTest.const_set('Vehicle', @model)
+
+ owner_model = new_model(:owner) do
+ one_to_many :vehicles, :class_name => 'SequelTest::Vehicle'
+ end
+ SequelTest.const_set('Owner', owner_model)
+
+ owner = owner_model.create
+ record = @model.create(:state => 'idling', :owner_id => owner.id)
+ assert_equal 'idling', owner.vehicles[0].state
+ end
+
+ if defined?(Sequel::VERSION) && Gem::Version.new(Sequel::VERSION) >= Gem::Version.new('3.10.0')
+ def test_should_use_stored_values_when_loading_for_one_association
+ @machine.state :idling
+
+ DB.alter_table(@model.table_identifier) do
+ add_column :owner_id, :integer
+ end
+ @model.class_eval { get_db_schema(true) }
+ SequelTest.const_set('Vehicle', @model)
+
+ owner_model = new_model(:owner) do
+ one_to_one :vehicle, :class_name => 'SequelTest::Vehicle'
+ end
+ SequelTest.const_set('Owner', owner_model)
+
+ owner = owner_model.create
+ record = @model.create(:state => 'idling', :owner_id => owner.id)
+ owner.reload
+ assert_equal 'idling', owner.vehicle.state
+ end
+ end
+
+ def test_should_use_stored_values_when_loading_for_belongs_to_association
+ @machine.state :idling
+
+ SequelTest.const_set('Vehicle', @model)
+
+ driver_model = new_model(:driver) do
+ DB.alter_table(table_identifier) do
+ add_column :vehicle_id, :integer
+ end
+ get_db_schema(true)
+
+ many_to_one :vehicle, :class_name => 'SequelTest::Vehicle'
+ end
+
+ SequelTest.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
+ SequelTest.class_eval do
+ remove_const('Vehicle') if defined?(SequelTest::Vehicle)
+ remove_const('Owner') if defined?(SequelTest::Owner)
+ remove_const('Driver') if defined?(SequelTest::Driver)
+ end
+ super
+ end
end
class MachineWithDynamicInitialStateTest < BaseTestCase
Please sign in to comment.
Something went wrong with that request. Please try again.