mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
moving rename column tests to their own class
This commit is contained in:
parent
28bb02a78f
commit
cd930c8515
4 changed files with 138 additions and 106 deletions
|
@ -1,29 +1,47 @@
|
|||
require "cases/helper"
|
||||
require "cases/migration/helper"
|
||||
|
||||
module ActiveRecord
|
||||
class Migration
|
||||
class ColumnAttributesTest < ActiveRecord::TestCase
|
||||
include ActiveRecord::Migration::TestHelper
|
||||
|
||||
self.use_transactional_fixtures = false
|
||||
|
||||
class TestModel < ActiveRecord::Base
|
||||
self.table_name = 'test_models'
|
||||
end
|
||||
def test_add_remove_single_field_using_string_arguments
|
||||
refute TestModel.column_methods_hash.key?(:last_name)
|
||||
|
||||
attr_reader :connection, :table_name
|
||||
|
||||
def setup
|
||||
super
|
||||
@connection = ActiveRecord::Base.connection
|
||||
connection.create_table :test_models do |t|
|
||||
t.timestamps
|
||||
end
|
||||
add_column 'test_models', 'last_name', :string
|
||||
|
||||
TestModel.reset_column_information
|
||||
|
||||
assert TestModel.column_methods_hash.key?(:last_name)
|
||||
|
||||
remove_column 'test_models', 'last_name'
|
||||
|
||||
TestModel.reset_column_information
|
||||
refute TestModel.column_methods_hash.key?(:last_name)
|
||||
end
|
||||
|
||||
def teardown
|
||||
super
|
||||
connection.drop_table :test_models rescue nil
|
||||
def test_add_remove_single_field_using_symbol_arguments
|
||||
refute TestModel.column_methods_hash.key?(:last_name)
|
||||
|
||||
add_column :test_models, :last_name, :string
|
||||
|
||||
TestModel.reset_column_information
|
||||
assert TestModel.column_methods_hash.key?(:last_name)
|
||||
|
||||
remove_column :test_models, :last_name
|
||||
|
||||
TestModel.reset_column_information
|
||||
refute TestModel.column_methods_hash.key?(:last_name)
|
||||
end
|
||||
|
||||
def test_unabstracted_database_dependent_types
|
||||
skip "not supported" unless current_adapter?(:MysqlAdapter, :Mysql2Adapter)
|
||||
|
||||
add_column :test_models, :intelligence_quotient, :tinyint
|
||||
TestModel.reset_column_information
|
||||
assert_match(/tinyint/, TestModel.columns_hash['intelligence_quotient'].sql_type)
|
||||
end
|
||||
|
||||
# We specifically do a manual INSERT here, and then test only the SELECT
|
||||
|
@ -165,10 +183,6 @@ module ActiveRecord
|
|||
assert_instance_of TrueClass, bob.male?
|
||||
assert_kind_of BigDecimal, bob.wealth
|
||||
end
|
||||
|
||||
def add_column(*args)
|
||||
connection.add_column(*args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
41
activerecord/test/cases/migration/helper.rb
Normal file
41
activerecord/test/cases/migration/helper.rb
Normal file
|
@ -0,0 +1,41 @@
|
|||
require "cases/helper"
|
||||
|
||||
module ActiveRecord
|
||||
class Migration
|
||||
module TestHelper
|
||||
attr_reader :connection, :table_name
|
||||
|
||||
class TestModel < ActiveRecord::Base
|
||||
self.table_name = 'test_models'
|
||||
end
|
||||
|
||||
def setup
|
||||
super
|
||||
@connection = ActiveRecord::Base.connection
|
||||
connection.create_table :test_models do |t|
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
TestModel.reset_column_information
|
||||
end
|
||||
|
||||
def teardown
|
||||
super
|
||||
connection.drop_table :test_models rescue nil
|
||||
end
|
||||
|
||||
private
|
||||
def add_column(*args)
|
||||
connection.add_column(*args)
|
||||
end
|
||||
|
||||
def remove_column(*args)
|
||||
connection.remove_column(*args)
|
||||
end
|
||||
|
||||
def rename_column(*args)
|
||||
connection.rename_column(*args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
53
activerecord/test/cases/migration/rename_column_test.rb
Normal file
53
activerecord/test/cases/migration/rename_column_test.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
require "cases/migration/helper"
|
||||
|
||||
module ActiveRecord
|
||||
class Migration
|
||||
class RenameColumnTest < ActiveRecord::TestCase
|
||||
include ActiveRecord::Migration::TestHelper
|
||||
|
||||
self.use_transactional_fixtures = false
|
||||
|
||||
# FIXME: this is more of an integration test with AR::Base and the
|
||||
# schema modifications. Maybe we should move this?
|
||||
def test_add_rename
|
||||
add_column "test_models", "girlfriend", :string
|
||||
TestModel.reset_column_information
|
||||
|
||||
TestModel.create :girlfriend => 'bobette'
|
||||
|
||||
rename_column "test_models", "girlfriend", "exgirlfriend"
|
||||
|
||||
TestModel.reset_column_information
|
||||
bob = TestModel.find(:first)
|
||||
|
||||
assert_equal "bobette", bob.exgirlfriend
|
||||
end
|
||||
|
||||
# FIXME: another integration test. We should decouple this from the
|
||||
# AR::Base implementation.
|
||||
def test_rename_column_using_symbol_arguments
|
||||
add_column :test_models, :first_name, :string
|
||||
|
||||
TestModel.create :first_name => 'foo'
|
||||
|
||||
rename_column :test_models, :first_name, :nick_name
|
||||
TestModel.reset_column_information
|
||||
assert TestModel.column_names.include?("nick_name")
|
||||
assert_equal ['foo'], TestModel.find(:all).map(&:nick_name)
|
||||
end
|
||||
|
||||
# FIXME: another integration test. We should decouple this from the
|
||||
# AR::Base implementation.
|
||||
def test_rename_column
|
||||
add_column "test_models", "first_name", "string"
|
||||
|
||||
TestModel.create :first_name => 'foo'
|
||||
|
||||
rename_column "test_models", "first_name", "nick_name"
|
||||
TestModel.reset_column_information
|
||||
assert TestModel.column_names.include?("nick_name")
|
||||
assert_equal ['foo'], TestModel.find(:all).map(&:nick_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -27,6 +27,17 @@ class ActiveRecord::Migration
|
|||
end
|
||||
end
|
||||
|
||||
module ActiveRecord
|
||||
class MigrationTest < ActiveRecord::TestCase
|
||||
attr_reader :connection
|
||||
|
||||
def setup
|
||||
super
|
||||
@connection = Base.connection
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class MigrationTest < ActiveRecord::TestCase
|
||||
self.use_transactional_fixtures = false
|
||||
|
||||
|
@ -80,93 +91,6 @@ class MigrationTest < ActiveRecord::TestCase
|
|||
Person.connection.drop_table :testings2 rescue nil
|
||||
end
|
||||
|
||||
def test_unabstracted_database_dependent_types
|
||||
skip "not supported" unless current_adapter?(:MysqlAdapter, :Mysql2Adapter)
|
||||
|
||||
Person.delete_all
|
||||
|
||||
ActiveRecord::Migration.add_column :people, :intelligence_quotient, :tinyint
|
||||
Person.reset_column_information
|
||||
assert_match(/tinyint/, Person.columns_hash['intelligence_quotient'].sql_type)
|
||||
ensure
|
||||
ActiveRecord::Migration.remove_column :people, :intelligence_quotient rescue nil
|
||||
end
|
||||
|
||||
def test_add_remove_single_field_using_string_arguments
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
|
||||
ActiveRecord::Migration.add_column 'people', 'last_name', :string
|
||||
|
||||
Person.reset_column_information
|
||||
assert Person.column_methods_hash.include?(:last_name)
|
||||
|
||||
ActiveRecord::Migration.remove_column 'people', 'last_name'
|
||||
|
||||
Person.reset_column_information
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
end
|
||||
|
||||
def test_add_remove_single_field_using_symbol_arguments
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
|
||||
ActiveRecord::Migration.add_column :people, :last_name, :string
|
||||
|
||||
Person.reset_column_information
|
||||
assert Person.column_methods_hash.include?(:last_name)
|
||||
|
||||
ActiveRecord::Migration.remove_column :people, :last_name
|
||||
|
||||
Person.reset_column_information
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
end
|
||||
|
||||
def test_add_rename
|
||||
Person.delete_all
|
||||
|
||||
begin
|
||||
Person.connection.add_column "people", "girlfriend", :string
|
||||
Person.reset_column_information
|
||||
Person.create :girlfriend => 'bobette'
|
||||
|
||||
Person.connection.rename_column "people", "girlfriend", "exgirlfriend"
|
||||
|
||||
Person.reset_column_information
|
||||
bob = Person.find(:first)
|
||||
|
||||
assert_equal "bobette", bob.exgirlfriend
|
||||
ensure
|
||||
Person.connection.remove_column("people", "girlfriend") rescue nil
|
||||
Person.connection.remove_column("people", "exgirlfriend") rescue nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_rename_column_using_symbol_arguments
|
||||
begin
|
||||
names_before = Person.find(:all).map(&:first_name)
|
||||
Person.connection.rename_column :people, :first_name, :nick_name
|
||||
Person.reset_column_information
|
||||
assert Person.column_names.include?("nick_name")
|
||||
assert_equal names_before, Person.find(:all).map(&:nick_name)
|
||||
ensure
|
||||
Person.connection.remove_column("people","nick_name")
|
||||
Person.connection.add_column("people","first_name", :string)
|
||||
end
|
||||
end
|
||||
|
||||
def test_rename_column
|
||||
begin
|
||||
names_before = Person.find(:all).map(&:first_name)
|
||||
Person.connection.rename_column "people", "first_name", "nick_name"
|
||||
Person.reset_column_information
|
||||
assert Person.column_names.include?("nick_name")
|
||||
assert_equal names_before, Person.find(:all).map(&:nick_name)
|
||||
ensure
|
||||
Person.connection.remove_column("people","nick_name")
|
||||
Person.connection.add_column("people","first_name", :string)
|
||||
end
|
||||
end
|
||||
|
||||
def test_rename_column_preserves_default_value_not_null
|
||||
begin
|
||||
default_before = Developer.connection.columns("developers").find { |c| c.name == "salary" }.default
|
||||
|
|
Loading…
Reference in a new issue