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-11-18 03:32:48 -05:00
|
|
|
assert_method :up, content do |up|
|
2009-06-28 13:46:34 -04:00
|
|
|
assert_match /add_column :posts, :title, :string/, up
|
|
|
|
assert_match /add_column :posts, :body, :text/, up
|
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-11-18 03:32:48 -05:00
|
|
|
assert_method :down, content do |down|
|
2009-06-28 13:46:34 -04:00
|
|
|
assert_match /remove_column :posts, :title/, down
|
|
|
|
assert_match /remove_column :posts, :body/, down
|
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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|
|
2009-06-28 13:46:34 -04:00
|
|
|
assert_match /remove_column :posts, :title/, up
|
|
|
|
assert_match /remove_column :posts, :body/, up
|
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-11-18 03:32:48 -05:00
|
|
|
assert_method :down, content do |down|
|
2009-06-28 13:46:34 -04:00
|
|
|
assert_match /add_column :posts, :title, :string/, down
|
|
|
|
assert_match /add_column :posts, :body, :text/, down
|
|
|
|
end
|
2009-06-28 07:16:14 -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|
|
2010-06-19 22:08:06 -04:00
|
|
|
assert_match /^\s*$/, up
|
|
|
|
end
|
|
|
|
|
2010-11-18 03:32:48 -05:00
|
|
|
assert_method :down, content do |down|
|
2010-06-19 22:08:06 -04:00
|
|
|
assert_match /^\s*$/, down
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|