2009-06-28 07:16:14 -04:00
|
|
|
require 'generators/generators_test_helper'
|
2010-03-23 08:40:19 -04:00
|
|
|
require 'rails/generators/rails/migration/migration_generator'
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class MigrationGeneratorTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
|
2009-06-28 07:16:14 -04:00
|
|
|
def test_migration
|
2010-01-03 10:34:32 -05:00
|
|
|
migration = "change_title_body_from_posts"
|
|
|
|
run_generator [migration]
|
|
|
|
assert_migration "db/migrate/#{migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
|
|
|
|
2010-04-16 01:31:15 -04:00
|
|
|
def test_migrations_generated_simultaneously
|
|
|
|
migrations = ["change_title_body_from_posts", "change_email_from_comments"]
|
|
|
|
|
|
|
|
first_migration_number, second_migration_number = migrations.collect do |migration|
|
|
|
|
run_generator [migration]
|
|
|
|
file_name = migration_file_name "db/migrate/#{migration}.rb"
|
|
|
|
|
|
|
|
File.basename(file_name).split('_').first
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_equal first_migration_number, second_migration_number
|
|
|
|
end
|
|
|
|
|
2009-06-28 07:16:14 -04:00
|
|
|
def test_migration_with_class_name
|
2010-01-03 10:34:32 -05:00
|
|
|
migration = "ChangeTitleBodyFromPosts"
|
|
|
|
run_generator [migration]
|
|
|
|
assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_migration_with_attributes
|
2010-01-03 10:34:32 -05:00
|
|
|
migration = "add_title_body_to_posts"
|
|
|
|
run_generator [migration, "title:string", "body:text"]
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-01-03 10:34:32 -05:00
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2010-12-31 13:19:56 -05:00
|
|
|
assert_method :change, content do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/add_column :posts, :title, :string/, up)
|
|
|
|
assert_match(/add_column :posts, :body, :text/, up)
|
2009-06-28 13:46:34 -04:00
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-20 21:20:35 -04:00
|
|
|
def test_remove_migration_with_indexed_attribute
|
|
|
|
migration = "remove_title_body_from_posts"
|
|
|
|
run_generator [migration, "title:string:index", "body:text"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
|
|
|
assert_method :up, content do |up|
|
|
|
|
assert_match(/remove_column :posts, :title/, up)
|
|
|
|
assert_match(/remove_column :posts, :body/, up)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_method :down, content do |down|
|
|
|
|
assert_match(/add_column :posts, :title, :string/, down)
|
|
|
|
assert_match(/add_column :posts, :body, :text/, down)
|
|
|
|
assert_match(/add_index :posts, :title/, down)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-28 07:16:14 -04:00
|
|
|
def test_remove_migration_with_attributes
|
2010-01-03 10:34:32 -05:00
|
|
|
migration = "remove_title_body_from_posts"
|
|
|
|
run_generator [migration, "title:string", "body:text"]
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-01-03 10:34:32 -05:00
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2010-11-18 03:32:48 -05:00
|
|
|
assert_method :up, content do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/remove_column :posts, :title/, up)
|
|
|
|
assert_match(/remove_column :posts, :body/, up)
|
2009-06-28 13:46:34 -04:00
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-11-18 03:32:48 -05:00
|
|
|
assert_method :down, content do |down|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/add_column :posts, :title, :string/, down)
|
|
|
|
assert_match(/add_column :posts, :body, :text/, down)
|
2009-06-28 13:46:34 -04:00
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-19 22:08:06 -04:00
|
|
|
|
2011-08-17 05:16:04 -04:00
|
|
|
def test_add_migration_with_attributes_and_indices
|
|
|
|
migration = "add_title_with_index_and_body_to_posts"
|
2011-12-24 04:38:19 -05:00
|
|
|
run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"]
|
2011-08-17 05:16:04 -04:00
|
|
|
|
|
|
|
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)
|
2011-12-24 16:06:25 -05:00
|
|
|
assert_match(/add_index :posts, :user_id, unique: true/, content)
|
2011-08-17 05:16:04 -04:00
|
|
|
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
|
2011-12-24 11:04:32 -05:00
|
|
|
assert_no_match(/add_index :books, :title/, content)
|
|
|
|
assert_no_match(/add_index :books, :user_id/, content)
|
2011-08-17 05:16:04 -04:00
|
|
|
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)
|
2011-12-24 16:06:25 -05:00
|
|
|
assert_match(/add_index :posts, :user_uuid, unique: true/, content)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_migration_with_attributes_index_declaration_and_attribute_options
|
|
|
|
migration = "add_title_and_content_to_books"
|
2012-01-22 09:45:18 -05:00
|
|
|
run_generator [migration, "title:string{40}:index", "content:string{255}", "price:decimal{1,2}:index", "discount:decimal{3.4}:uniq"]
|
2011-08-17 05:16:04 -04:00
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
|
|
|
assert_method :change, content do |up|
|
2011-12-24 16:06:25 -05:00
|
|
|
assert_match(/add_column :books, :title, :string, limit: 40/, up)
|
|
|
|
assert_match(/add_column :books, :content, :string, limit: 255/, up)
|
2012-01-22 09:45:18 -05:00
|
|
|
assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, up)
|
|
|
|
assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, up)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
|
|
|
assert_match(/add_index :books, :title/, content)
|
|
|
|
assert_match(/add_index :books, :price/, content)
|
2011-12-24 16:06:25 -05:00
|
|
|
assert_match(/add_index :books, :discount, unique: true/, content)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-19 22:08:06 -04:00
|
|
|
def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove
|
|
|
|
migration = "create_books"
|
|
|
|
run_generator [migration, "title:string", "content:text"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2010-11-18 03:32:48 -05:00
|
|
|
assert_method :up, content do |up|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/^\s*$/, up)
|
2010-06-19 22:08:06 -04:00
|
|
|
end
|
|
|
|
|
2010-11-18 03:32:48 -05:00
|
|
|
assert_method :down, content do |down|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/^\s*$/, down)
|
2010-06-19 22:08:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-31 19:12:49 -04:00
|
|
|
|
|
|
|
def test_properly_identifies_usage_file
|
|
|
|
assert generator_class.send(:usage_path)
|
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|