migration_generator_test.rb 7.1 KB
Newer Older
1
require 'generators/generators_test_helper'
2
require 'rails/generators/rails/migration/migration_generator'
3

4 5 6
class MigrationGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper

7
  def test_migration
8 9 10
    migration = "change_title_body_from_posts"
    run_generator [migration]
    assert_migration "db/migrate/#{migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/
11 12
  end

13 14 15 16 17 18 19 20 21 22 23 24 25
  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

26
  def test_migration_with_class_name
27 28 29
    migration = "ChangeTitleBodyFromPosts"
    run_generator [migration]
    assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/
30 31 32
  end

  def test_add_migration_with_attributes
33 34
    migration = "add_title_body_to_posts"
    run_generator [migration, "title:string", "body:text"]
35

36
    assert_migration "db/migrate/#{migration}.rb" do |content|
37
      assert_method :change, content do |up|
38 39
        assert_match(/add_column :posts, :title, :string/, up)
        assert_match(/add_column :posts, :body, :text/, up)
40
      end
41 42 43
    end
  end

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  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

62
  def test_remove_migration_with_attributes
63 64
    migration = "remove_title_body_from_posts"
    run_generator [migration, "title:string", "body:text"]
65

66
    assert_migration "db/migrate/#{migration}.rb" do |content|
J
José Valim 已提交
67
      assert_method :up, content do |up|
68 69
        assert_match(/remove_column :posts, :title/, up)
        assert_match(/remove_column :posts, :body/, up)
70
      end
71

J
José Valim 已提交
72
      assert_method :down, content do |down|
73 74
        assert_match(/add_column :posts, :title, :string/, down)
        assert_match(/add_column :posts, :body, :text/, down)
75
      end
76 77
    end
  end
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  def test_remove_migration_with_references_options
    migration = "remove_references_from_books"
    run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]

    assert_migration "db/migrate/#{migration}.rb" do |content|
      assert_method :up, content do |up|
        assert_match(/remove_reference :books, :author/, up)
        assert_match(/remove_reference :books, :distributor, polymorphic: true/, up)
      end

      assert_method :down, content do |down|
        assert_match(/add_reference :books, :author, index: true/, down)
        assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, down)
      end
    end
  end

96 97
  def test_add_migration_with_attributes_and_indices
    migration = "add_title_with_index_and_body_to_posts"
J
José Valim 已提交
98
    run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"]
99 100 101 102 103 104 105 106

    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)
J
José Valim 已提交
107
      assert_match(/add_index :posts, :user_id, unique: true/, content)
108 109 110 111 112 113 114 115 116 117 118 119 120
    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
J
José Valim 已提交
121 122
      assert_no_match(/add_index :books, :title/, content)
      assert_no_match(/add_index :books, :user_id/, content)
123 124 125 126 127 128 129 130 131 132 133 134 135 136
    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)
J
José Valim 已提交
137
      assert_match(/add_index :posts, :user_uuid, unique: true/, content)
138 139 140 141 142
    end
  end

  def test_add_migration_with_attributes_index_declaration_and_attribute_options
    migration = "add_title_and_content_to_books"
143
    run_generator [migration, "title:string{40}:index", "content:string{255}", "price:decimal{1,2}:index", "discount:decimal{3.4}:uniq"]
144 145 146

    assert_migration "db/migrate/#{migration}.rb" do |content|
      assert_method :change, content do |up|
J
José Valim 已提交
147 148
        assert_match(/add_column :books, :title, :string, limit: 40/, up)
        assert_match(/add_column :books, :content, :string, limit: 255/, up)
149 150
        assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, up)
        assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, up)
151 152 153
      end
      assert_match(/add_index :books, :title/, content)
      assert_match(/add_index :books, :price/, content)
J
José Valim 已提交
154
      assert_match(/add_index :books, :discount, unique: true/, content)
155 156 157
    end
  end

158 159 160 161 162 163 164 165 166 167 168 169
  def test_add_migration_with_references_options
    migration = "add_references_to_books"
    run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]

    assert_migration "db/migrate/#{migration}.rb" do |content|
      assert_method :change, content do |up|
        assert_match(/add_reference :books, :author, index: true/, up)
        assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, up)
      end
    end
  end

170 171 172 173 174
  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|
J
José Valim 已提交
175
      assert_method :up, content do |up|
176
        assert_match(/^\s*$/, up)
177 178
      end

J
José Valim 已提交
179
      assert_method :down, content do |down|
180
        assert_match(/^\s*$/, down)
181 182 183
      end
    end
  end
184 185 186 187

  def test_properly_identifies_usage_file
    assert generator_class.send(:usage_path)
  end
188
end