Skip to content
This repository
Browse code

moving more renaming tests to the proper test case

  • Loading branch information...
commit e6f796031fc9204eba81a318830fcb0f0738ad7e 1 parent cd930c8
Aaron Patterson tenderlove authored
8 activerecord/test/cases/migration/helper.rb
@@ -36,6 +36,14 @@ def remove_column(*args)
36 36 def rename_column(*args)
37 37 connection.rename_column(*args)
38 38 end
  39 +
  40 + def add_index(*args)
  41 + connection.add_index(*args)
  42 + end
  43 +
  44 + def change_column(*args)
  45 + connection.change_column(*args)
  46 + end
39 47 end
40 48 end
41 49 end
63 activerecord/test/cases/migration/rename_column_test.rb
@@ -48,6 +48,69 @@ def test_rename_column
48 48 assert TestModel.column_names.include?("nick_name")
49 49 assert_equal ['foo'], TestModel.find(:all).map(&:nick_name)
50 50 end
  51 +
  52 + def test_rename_column_preserves_default_value_not_null
  53 + add_column 'test_models', 'salary', :integer, :default => 70000
  54 +
  55 + default_before = connection.columns("test_models").find { |c| c.name == "salary" }.default
  56 + assert_equal 70000, default_before
  57 +
  58 + rename_column "test_models", "salary", "anual_salary"
  59 +
  60 + assert TestModel.column_names.include?("anual_salary")
  61 + default_after = connection.columns("test_models").find { |c| c.name == "anual_salary" }.default
  62 + assert_equal 70000, default_after
  63 + end
  64 +
  65 + def test_rename_nonexistent_column
  66 + exception = if current_adapter?(:PostgreSQLAdapter, :OracleAdapter)
  67 + ActiveRecord::StatementInvalid
  68 + else
  69 + ActiveRecord::ActiveRecordError
  70 + end
  71 + assert_raise(exception) do
  72 + rename_column "test_models", "nonexistent", "should_fail"
  73 + end
  74 + end
  75 +
  76 + def test_rename_column_with_sql_reserved_word
  77 + add_column 'test_models', 'first_name', :string
  78 + rename_column "test_models", "first_name", "group"
  79 +
  80 + assert TestModel.column_names.include?("group")
  81 + end
  82 +
  83 + def test_rename_column_with_an_index
  84 + add_column "test_models", :hat_name, :string
  85 + add_index :test_models, :hat_name
  86 +
  87 + # FIXME: we should test that the index goes away
  88 + rename_column "test_models", "hat_name", "name"
  89 + end
  90 +
  91 + def test_remove_column_with_index
  92 + add_column "test_models", :hat_name, :string
  93 + add_index :test_models, :hat_name
  94 +
  95 + # FIXME: we should test that the index goes away
  96 + remove_column("test_models", "hat_size")
  97 + end
  98 +
  99 + def test_remove_column_with_multi_column_index
  100 + add_column "test_models", :hat_size, :integer
  101 + add_column "test_models", :hat_style, :string, :limit => 100
  102 + add_index "test_models", ["hat_style", "hat_size"], :unique => true
  103 +
  104 + # FIXME: we should test that the index goes away
  105 + remove_column("test_models", "hat_size")
  106 + end
  107 +
  108 + # FIXME: we need to test that these calls do something
  109 + def test_change_type_of_not_null_column
  110 + change_column "test_models", "updated_at", :datetime, :null => false
  111 + change_column "test_models", "updated_at", :datetime, :null => false
  112 + change_column "test_models", "updated_at", :datetime, :null => true
  113 + end
51 114 end
52 115 end
53 116 end
93 activerecord/test/cases/migration_test.rb
@@ -91,103 +91,10 @@ def drop_table; raise "no"; end
91 91 Person.connection.drop_table :testings2 rescue nil
92 92 end
93 93
94   - def test_rename_column_preserves_default_value_not_null
95   - begin
96   - default_before = Developer.connection.columns("developers").find { |c| c.name == "salary" }.default
97   - assert_equal 70000, default_before
98   - Developer.connection.rename_column "developers", "salary", "anual_salary"
99   - Developer.reset_column_information
100   - assert Developer.column_names.include?("anual_salary")
101   - default_after = Developer.connection.columns("developers").find { |c| c.name == "anual_salary" }.default
102   - assert_equal 70000, default_after
103   - ensure
104   - Developer.connection.rename_column "developers", "anual_salary", "salary"
105   - Developer.reset_column_information
106   - end
107   - end
108   -
109   - def test_rename_nonexistent_column
110   - ActiveRecord::Base.connection.create_table(:hats) do |table|
111   - table.column :hat_name, :string, :default => nil
112   - end
113   - exception = if current_adapter?(:PostgreSQLAdapter, :OracleAdapter)
114   - ActiveRecord::StatementInvalid
115   - else
116   - ActiveRecord::ActiveRecordError
117   - end
118   - assert_raise(exception) do
119   - Person.connection.rename_column "hats", "nonexistent", "should_fail"
120   - end
121   - ensure
122   - ActiveRecord::Base.connection.drop_table(:hats)
123   - end
124   -
125   - def test_rename_column_with_sql_reserved_word
126   - begin
127   - assert_nothing_raised { Person.connection.rename_column "people", "first_name", "group" }
128   - Person.reset_column_information
129   - assert Person.column_names.include?("group")
130   - ensure
131   - Person.connection.remove_column("people", "group") rescue nil
132   - Person.connection.add_column("people", "first_name", :string) rescue nil
133   - end
134   - end
135   -
136   - def test_rename_column_with_an_index
137   - ActiveRecord::Base.connection.create_table(:hats) do |table|
138   - table.column :hat_name, :string, :limit => 100
139   - table.column :hat_size, :integer
140   - end
141   - Person.connection.add_index :hats, :hat_name
142   - assert_nothing_raised do
143   - Person.connection.rename_column "hats", "hat_name", "name"
144   - end
145   - ensure
146   - ActiveRecord::Base.connection.drop_table(:hats)
147   - end
148   -
149   - def test_remove_column_with_index
150   - ActiveRecord::Base.connection.create_table(:hats) do |table|
151   - table.column :hat_name, :string, :limit => 100
152   - table.column :hat_size, :integer
153   - end
154   - ActiveRecord::Base.connection.add_index "hats", "hat_size"
155   -
156   - assert_nothing_raised { Person.connection.remove_column("hats", "hat_size") }
157   - ensure
158   - ActiveRecord::Base.connection.drop_table(:hats)
159   - end
160   -
161   - def test_remove_column_with_multi_column_index
162   - ActiveRecord::Base.connection.create_table(:hats) do |table|
163   - table.column :hat_name, :string, :limit => 100
164   - table.column :hat_size, :integer
165   - table.column :hat_style, :string, :limit => 100
166   - end
167   - ActiveRecord::Base.connection.add_index "hats", ["hat_style", "hat_size"], :unique => true
168   -
169   - assert_nothing_raised { Person.connection.remove_column("hats", "hat_size") }
170   - ensure
171   - ActiveRecord::Base.connection.drop_table(:hats)
172   - end
173   -
174 94 def test_remove_column_no_second_parameter_raises_exception
175 95 assert_raise(ArgumentError) { Person.connection.remove_column("funny") }
176 96 end
177 97
178   - def test_change_type_of_not_null_column
179   - assert_nothing_raised do
180   - Topic.connection.change_column "topics", "written_on", :datetime, :null => false
181   - Topic.reset_column_information
182   -
183   - Topic.connection.change_column "topics", "written_on", :datetime, :null => false
184   - Topic.reset_column_information
185   -
186   - Topic.connection.change_column "topics", "written_on", :datetime, :null => true
187   - Topic.reset_column_information
188   - end
189   - end
190   -
191 98 def test_rename_table_for_sqlite_should_work_with_reserved_words
192 99 skip "not supported" unless current_adapter?(:SQLite3Adapter)
193 100

0 comments on commit e6f7960

Please sign in to comment.
Something went wrong with that request. Please try again.