aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/migration_generator_test.rb
diff options
context:
space:
mode:
authorDmitrii Samoilov <e2718281828@ya.ru>2011-08-17 12:16:04 +0300
committerJosé Valim <jose.valim@gmail.com>2011-12-24 10:52:45 +0100
commit7f5b51686c556e3a35a2e5320fe8e4d758ff70b5 (patch)
treed8069a1660de294afcc7602af3f496370aa43be5 /railties/test/generators/migration_generator_test.rb
parentc4ef3d34ee4b901a591af8b48012c9233e2fe4a3 (diff)
downloadrails-7f5b51686c556e3a35a2e5320fe8e4d758ff70b5.tar.gz
rails-7f5b51686c556e3a35a2e5320fe8e4d758ff70b5.tar.bz2
rails-7f5b51686c556e3a35a2e5320fe8e4d758ff70b5.zip
added ability to specify from cli when generating a model/migration whether particular property should be an index like this 'rails g model person name:string:index profile:string'
Diffstat (limited to 'railties/test/generators/migration_generator_test.rb')
-rw-r--r--railties/test/generators/migration_generator_test.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb
index 337257df7d..6b6c9dee7c 100644
--- a/railties/test/generators/migration_generator_test.rb
+++ b/railties/test/generators/migration_generator_test.rb
@@ -58,6 +58,68 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
end
end
+ def test_add_migration_with_attributes_and_indices
+ migration = "add_title_with_index_and_body_to_posts"
+ run_generator [migration, "title:string:index", "body:text", "user_id:integer:unique"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |up|
+ assert_match(/add_column :posts, :title, :string/, up)
+ assert_match(/add_column :posts, :body, :text/, up)
+ assert_match(/add_column :posts, :user_id, :integer/, up)
+ end
+ assert_match(/add_index :posts, :title/, content)
+ assert_match(/add_index :posts, :user_id, :unique => true/, content)
+ end
+ end
+
+ def test_add_migration_with_attributes_and_wrong_index_declaration
+ migration = "add_title_and_content_to_books"
+ run_generator [migration, "title:string:inex", "content:text", "user_id:integer:unik"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |up|
+ assert_match(/add_column :books, :title, :string/, up)
+ assert_match(/add_column :books, :content, :text/, up)
+ assert_match(/add_column :books, :user_id, :integer/, up)
+ end
+ assert_not_match(/add_index :books, :title/, content)
+ assert_not_match(/add_index :books, :user_id/, content)
+ end
+ end
+
+ def test_add_migration_with_attributes_without_type_and_index
+ migration = "add_title_with_index_and_body_to_posts"
+ run_generator [migration, "title:index", "body:text", "user_uuid:uniq"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |up|
+ assert_match(/add_column :posts, :title, :string/, up)
+ assert_match(/add_column :posts, :body, :text/, up)
+ assert_match(/add_column :posts, :user_uuid, :string/, up)
+ end
+ assert_match(/add_index :posts, :title/, content)
+ assert_match(/add_index :posts, :user_uuid, :unique => true/, content)
+ end
+ end
+
+ def test_add_migration_with_attributes_index_declaration_and_attribute_options
+ migration = "add_title_and_content_to_books"
+ run_generator [migration, "title:string{40}:index", "content:string{255}", "price:decimal{5,2}:index", "discount:decimal{3,2}:uniq"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |up|
+ assert_match(/add_column :books, :title, :string, :limit=>40/, up)
+ assert_match(/add_column :books, :content, :string, :limit=>255/, up)
+ assert_match(/add_column :books, :price, :decimal, :precision=>5, :scale=>2/, up)
+ assert_match(/add_column :books, :discount, :decimal, :precision=>3, :scale=>2/, up)
+ end
+ assert_match(/add_index :books, :title/, content)
+ assert_match(/add_index :books, :price/, content)
+ assert_match(/add_index :books, :discount, :unique => true/, content)
+ end
+ end
+
def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove
migration = "create_books"
run_generator [migration, "title:string", "content:text"]