aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-06-26 01:08:57 +0200
committerJosé Valim <jose.valim@gmail.com>2010-06-26 01:08:57 +0200
commit026cec339046c6ff4629b445d8284d7ad3da5085 (patch)
treedba8d6796e9453c34d7bc7c402f291b5eb8ee4fa /activerecord/test
parent4464d10e68045e6723d4eb62734213c4296ef339 (diff)
parent11ff3da5f4fe71a8d93180ab9fa69c6190a2e26e (diff)
downloadrails-026cec339046c6ff4629b445d8284d7ad3da5085.tar.gz
rails-026cec339046c6ff4629b445d8284d7ad3da5085.tar.bz2
rails-026cec339046c6ff4629b445d8284d7ad3da5085.zip
Merge branch 'tago'
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/adapters/mysql/active_schema_test.rb6
-rw-r--r--activerecord/test/cases/migration_test.rb108
2 files changed, 107 insertions, 7 deletions
diff --git a/activerecord/test/cases/adapters/mysql/active_schema_test.rb b/activerecord/test/cases/adapters/mysql/active_schema_test.rb
index d7431e5158..6e6645511c 100644
--- a/activerecord/test/cases/adapters/mysql/active_schema_test.rb
+++ b/activerecord/test/cases/adapters/mysql/active_schema_test.rb
@@ -17,8 +17,8 @@ class ActiveSchemaTest < ActiveRecord::TestCase
end
def test_add_index
- # add_index calls index_exists? which can't work since execute is stubbed
- ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:define_method, :index_exists?) do |*|
+ # add_index calls index_name_exists? which can't work since execute is stubbed
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:define_method, :index_name_exists?) do |*|
false
end
expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`)"
@@ -35,7 +35,7 @@ class ActiveSchemaTest < ActiveRecord::TestCase
expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(10))"
assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15, :first_name => 10})
- ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:remove_method, :index_exists?)
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:remove_method, :index_name_exists?)
end
def test_drop_table
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 9ece2fbab1..99a3a12a8b 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -128,9 +128,9 @@ if ActiveRecord::Base.connection.supports_migrations?
good_index_name = 'x' * Person.connection.index_name_length
too_long_index_name = good_index_name + 'x'
assert_nothing_raised { Person.connection.add_index("people", "first_name", :name => too_long_index_name) }
- assert !Person.connection.index_exists?("people", too_long_index_name, false)
+ assert !Person.connection.index_name_exists?("people", too_long_index_name, false)
assert_nothing_raised { Person.connection.add_index("people", "first_name", :name => good_index_name) }
- assert Person.connection.index_exists?("people", good_index_name, false)
+ assert Person.connection.index_name_exists?("people", good_index_name, false)
end
def test_remove_nonexistent_index
@@ -146,8 +146,8 @@ if ActiveRecord::Base.connection.supports_migrations?
Person.connection.add_index('people', [:first_name], :name => 'old_idx')
assert_nothing_raised { Person.connection.rename_index('people', 'old_idx', 'new_idx') }
# if the adapter doesn't support the indexes call, pick defaults that let the test pass
- assert !Person.connection.index_exists?('people', 'old_idx', false)
- assert Person.connection.index_exists?('people', 'new_idx', true)
+ assert !Person.connection.index_name_exists?('people', 'old_idx', false)
+ assert Person.connection.index_name_exists?('people', 'new_idx', true)
end
end
@@ -158,6 +158,53 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
+ def test_index_exists
+ Person.connection.create_table :testings do |t|
+ t.column :foo, :string, :limit => 100
+ t.column :bar, :string, :limit => 100
+ end
+ Person.connection.add_index :testings, :foo
+
+ assert Person.connection.index_exists?(:testings, :foo)
+ assert !Person.connection.index_exists?(:testings, :bar)
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+
+ def test_index_exists_on_multiple_columns
+ Person.connection.create_table :testings do |t|
+ t.column :foo, :string, :limit => 100
+ t.column :bar, :string, :limit => 100
+ end
+ Person.connection.add_index :testings, [:foo, :bar]
+
+ assert Person.connection.index_exists?(:testings, [:foo, :bar])
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+
+ def test_unique_index_exists
+ Person.connection.create_table :testings do |t|
+ t.column :foo, :string, :limit => 100
+ end
+ Person.connection.add_index :testings, :foo, :unique => true
+
+ assert Person.connection.index_exists?(:testings, :foo, :unique => true)
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+
+ def test_named_index_exists
+ Person.connection.create_table :testings do |t|
+ t.column :foo, :string, :limit => 100
+ end
+ Person.connection.add_index :testings, :foo, :name => "custom_index_name"
+
+ assert Person.connection.index_exists?(:testings, :foo, :name => "custom_index_name")
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+
def testing_table_with_only_foo_attribute
Person.connection.create_table :testings, :id => false do |t|
t.column :foo, :string
@@ -974,6 +1021,45 @@ if ActiveRecord::Base.connection.supports_migrations?
assert_nil Person.new.first_name
end
+ def test_column_exists
+ Person.connection.create_table :testings do |t|
+ t.column :foo, :string
+ end
+
+ assert Person.connection.column_exists?(:testings, :foo)
+ assert !Person.connection.column_exists?(:testings, :bar)
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+
+ def test_column_exists_with_type
+ Person.connection.create_table :testings do |t|
+ t.column :foo, :string
+ t.column :bar, :decimal, :precision => 8, :scale => 2
+ end
+
+ assert Person.connection.column_exists?(:testings, :foo, :string)
+ assert !Person.connection.column_exists?(:testings, :foo, :integer)
+ assert Person.connection.column_exists?(:testings, :bar, :decimal)
+ assert !Person.connection.column_exists?(:testings, :bar, :integer)
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+
+ def test_column_exists_with_definition
+ Person.connection.create_table :testings do |t|
+ t.column :foo, :string, :limit => 100
+ t.column :bar, :decimal, :precision => 8, :scale => 2
+ end
+
+ assert Person.connection.column_exists?(:testings, :foo, :string, :limit => 100)
+ assert !Person.connection.column_exists?(:testings, :foo, :string, :limit => 50)
+ assert Person.connection.column_exists?(:testings, :bar, :decimal, :precision => 8, :scale => 2)
+ assert !Person.connection.column_exists?(:testings, :bar, :decimal, :precision => 10, :scale => 2)
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
+
def test_add_table
assert !Reminder.table_exists?
@@ -1684,6 +1770,20 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
+ def test_index_exists
+ with_change_table do |t|
+ @connection.expects(:index_exists?).with(:delete_me, :bar, {})
+ t.index_exists?(:bar)
+ end
+ end
+
+ def test_index_exists_with_options
+ with_change_table do |t|
+ @connection.expects(:index_exists?).with(:delete_me, :bar, {:unique => true})
+ t.index_exists?(:bar, :unique => true)
+ end
+ end
+
def test_change_changes_column
with_change_table do |t|
@connection.expects(:change_column).with(:delete_me, :bar, :string, {})