1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

moving more column renaming tests

This commit is contained in:
Aaron Patterson 2012-01-12 13:35:09 -08:00
parent e6f796031f
commit 977df64721
2 changed files with 75 additions and 71 deletions

View file

@ -111,6 +111,81 @@ module ActiveRecord
change_column "test_models", "updated_at", :datetime, :null => false change_column "test_models", "updated_at", :datetime, :null => false
change_column "test_models", "updated_at", :datetime, :null => true change_column "test_models", "updated_at", :datetime, :null => true
end end
def test_change_column_nullability
add_column "test_models", "funny", :boolean
assert TestModel.columns_hash["funny"].null, "Column 'funny' must initially allow nulls"
change_column "test_models", "funny", :boolean, :null => false, :default => true
TestModel.reset_column_information
refute TestModel.columns_hash["funny"].null, "Column 'funny' must *not* allow nulls at this point"
change_column "test_models", "funny", :boolean, :null => true
TestModel.reset_column_information
assert TestModel.columns_hash["funny"].null, "Column 'funny' must allow nulls again at this point"
end
def test_change_column
add_column 'test_models', 'age', :integer
add_column 'test_models', 'approved', :boolean, :default => true
label = "test_change_column Columns"
old_columns = connection.columns(TestModel.table_name, label)
assert old_columns.find { |c| c.name == 'age' && c.type == :integer }
change_column "test_models", "age", :string
new_columns = connection.columns(TestModel.table_name, label)
refute new_columns.find { |c| c.name == 'age' and c.type == :integer }
assert new_columns.find { |c| c.name == 'age' and c.type == :string }
old_columns = connection.columns(TestModel.table_name, label)
assert old_columns.find { |c|
c.name == 'approved' && c.type == :boolean && c.default == true
}
change_column :test_models, :approved, :boolean, :default => false
new_columns = connection.columns(TestModel.table_name, label)
refute new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
change_column :test_models, :approved, :boolean, :default => true
end
def test_change_column_with_nil_default
add_column "test_models", "contributor", :boolean, :default => true
assert TestModel.new.contributor?
change_column "test_models", "contributor", :boolean, :default => nil
TestModel.reset_column_information
refute TestModel.new.contributor?
assert_nil TestModel.new.contributor
end
def test_change_column_with_new_default
add_column "test_models", "administrator", :boolean, :default => true
assert TestModel.new.administrator?
change_column "test_models", "administrator", :boolean, :default => false
TestModel.reset_column_information
refute TestModel.new.administrator?
end
def test_change_column_default
add_column "test_models", "first_name", :string
connection.change_column_default "test_models", "first_name", "Tester"
assert_equal "Tester", TestModel.new.first_name
end
def test_change_column_default_to_null
add_column "test_models", "first_name", :string
connection.change_column_default "test_models", "first_name", nil
assert_nil TestModel.new.first_name
end
end end
end end
end end

View file

@ -142,19 +142,6 @@ class MigrationTest < ActiveRecord::TestCase
end end
end end
def test_change_column_nullability
Person.delete_all
Person.connection.add_column "people", "funny", :boolean
Person.reset_column_information
assert Person.columns_hash["funny"].null, "Column 'funny' must initially allow nulls"
Person.connection.change_column "people", "funny", :boolean, :null => false, :default => true
Person.reset_column_information
assert !Person.columns_hash["funny"].null, "Column 'funny' must *not* allow nulls at this point"
Person.connection.change_column "people", "funny", :boolean, :null => true
Person.reset_column_information
assert Person.columns_hash["funny"].null, "Column 'funny' must allow nulls again at this point"
end
def test_rename_table_with_an_index def test_rename_table_with_an_index
begin begin
ActiveRecord::Base.connection.create_table :octopuses do |t| ActiveRecord::Base.connection.create_table :octopuses do |t|
@ -178,64 +165,6 @@ class MigrationTest < ActiveRecord::TestCase
end end
end end
def test_change_column
Person.connection.add_column 'people', 'age', :integer
label = "test_change_column Columns"
old_columns = Person.connection.columns(Person.table_name, label)
assert old_columns.find { |c| c.name == 'age' and c.type == :integer }
assert_nothing_raised { Person.connection.change_column "people", "age", :string }
new_columns = Person.connection.columns(Person.table_name, label)
assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
assert new_columns.find { |c| c.name == 'age' and c.type == :string }
old_columns = Topic.connection.columns(Topic.table_name, label)
assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false }
new_columns = Topic.connection.columns(Topic.table_name, label)
assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => true }
end
def test_change_column_with_nil_default
Person.connection.add_column "people", "contributor", :boolean, :default => true
Person.reset_column_information
assert Person.new.contributor?
assert_nothing_raised { Person.connection.change_column "people", "contributor", :boolean, :default => nil }
Person.reset_column_information
assert !Person.new.contributor?
assert_nil Person.new.contributor
ensure
Person.connection.remove_column("people", "contributor") rescue nil
end
def test_change_column_with_new_default
Person.connection.add_column "people", "administrator", :boolean, :default => true
Person.reset_column_information
assert Person.new.administrator?
assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => false }
Person.reset_column_information
assert !Person.new.administrator?
ensure
Person.connection.remove_column("people", "administrator") rescue nil
end
def test_change_column_default
Person.connection.change_column_default "people", "first_name", "Tester"
Person.reset_column_information
assert_equal "Tester", Person.new.first_name
end
def test_change_column_default_to_null
Person.connection.change_column_default "people", "first_name", nil
Person.reset_column_information
assert_nil Person.new.first_name
end
def test_add_table def test_add_table
assert !Reminder.table_exists? assert !Reminder.table_exists?