1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
# frozen_string_literal: true
require "generators/generators_test_helper"
require "rails/generators/rails/migration/migration_generator"
require "active_record/migration"
class MigrationGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
def setup
@old_belongs_to_required_by_default = Rails.application.config.active_record.belongs_to_required_by_default
Rails.application.config.active_record.belongs_to_required_by_default = true
end
def teardown
Rails.application.config.active_record.belongs_to_required_by_default = @old_belongs_to_required_by_default
end
def test_migration
migration = "change_title_body_from_posts"
run_generator [migration]
assert_migration "db/migrate/#{migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration\[[0-9.]+\]/
end
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
def test_migration_with_class_name
migration = "ChangeTitleBodyFromPosts"
run_generator [migration]
assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration\[[0-9.]+\]/
end
def test_migration_with_invalid_file_name
migration = "add_something:datetime"
assert_raise ActiveRecord::IllegalMigrationNameError do
run_generator [migration]
end
end
def test_exit_on_failure
assert_equal true, generator_class.exit_on_failure?
end
def test_add_migration_with_attributes
migration = "add_title_body_to_posts"
run_generator [migration, "title:string", "body:text"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :posts, :title, :string/, change)
assert_match(/add_column :posts, :body, :text/, change)
end
end
end
def test_add_migration_with_table_having_from_in_title
migration = "add_email_address_to_excluded_from_campaign"
run_generator [migration, "email_address:string"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :excluded_from_campaigns, :email_address, :string/, change)
end
end
end
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 :change, content do |change|
assert_match(/remove_column :posts, :title, :string/, change)
assert_match(/remove_column :posts, :body, :text/, change)
assert_match(/remove_index :posts, :title/, change)
end
end
end
def test_remove_migration_with_attributes
migration = "remove_title_body_from_posts"
run_generator [migration, "title:string", "body:text"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/remove_column :posts, :title, :string/, change)
assert_match(/remove_column :posts, :body, :text/, change)
end
end
end
def test_remove_migration_with_table_having_to_in_title
migration = "remove_email_address_from_sent_to_user"
run_generator [migration, "email_address:string"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/remove_column :sent_to_users, :email_address, :string/, change)
end
end
end
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 :change, content do |change|
assert_match(/remove_reference :books, :author/, change)
assert_match(/remove_reference :books, :distributor, polymorphic: true/, change)
end
end
end
def test_remove_migration_with_references_removes_foreign_keys
migration = "remove_references_from_books"
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/remove_reference :books, :author,.*\sforeign_key: true/, change)
assert_match(/remove_reference :books, :distributor/, change) # Ensure the line isn't gone completely
assert_no_match(/remove_reference :books, :distributor,.*\sforeign_key: true/, change)
end
end
end
def test_remove_migration_with_references_removes_foreign_keys_when_primary_key_uuid
migration = "remove_references_from_books"
run_generator [migration, "author:belongs_to", "--primary_key_type=uuid"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/remove_reference :books, :author,.*\sforeign_key: true, type: :uuid/, change)
end
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:uniq"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :posts, :title, :string/, change)
assert_match(/add_column :posts, :body, :text/, change)
assert_match(/add_column :posts, :user_id, :integer/, change)
assert_match(/add_index :posts, :title/, change)
assert_match(/add_index :posts, :user_id, unique: true/, change)
end
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 |change|
assert_match(/add_column :posts, :title, :string/, change)
assert_match(/add_column :posts, :body, :text/, change)
assert_match(/add_column :posts, :user_uuid, :string/, change)
assert_match(/add_index :posts, :title/, change)
assert_match(/add_index :posts, :user_uuid, unique: true/, change)
end
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{1,2}:index", "discount:decimal{3.4}:uniq"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :books, :title, :string, limit: 40/, change)
assert_match(/add_column :books, :content, :string, limit: 255/, change)
assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, change)
assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, change)
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_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 |change|
assert_match(/add_reference :books, :author/, change)
assert_match(/add_reference :books, :distributor, polymorphic: true/, change)
end
end
end
def test_add_migration_with_references_adds_null_false_by_default
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 |change|
assert_match(/add_reference :books, :author, null: false/, change)
assert_match(/add_reference :books, :distributor, polymorphic: true, null: false/, change)
end
end
end
def test_add_migration_with_references_does_not_add_belongs_to_when_required_by_default_global_config_is_false
Rails.application.config.active_record.belongs_to_required_by_default = false
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 |change|
assert_match(/add_reference :books, :author/, change)
assert_match(/add_reference :books, :distributor, polymorphic: true/, change)
end
end
end
def test_add_migration_with_references_adds_foreign_keys
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 |change|
assert_match(/add_reference :books, :author,.*\sforeign_key: true/, change)
assert_match(/add_reference :books, :distributor/, change) # Ensure the line isn't gone completely
assert_no_match(/add_reference :books, :distributor,.*\sforeign_key: true/, change)
end
end
end
def test_create_join_table_migration
migration = "add_media_join_table"
run_generator [migration, "artist_id", "musics:uniq"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_join_table :artists, :musics/, change)
assert_match(/# t\.index \[:artist_id, :music_id\]/, change)
assert_match(/ t\.index \[:music_id, :artist_id\], unique: true/, change)
end
end
end
def test_create_table_migration
run_generator ["create_books", "title:string", "content:text"]
assert_migration "db/migrate/create_books.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :books/, change)
assert_match(/ t\.string :title/, change)
assert_match(/ t\.text :content/, change)
end
end
end
def test_create_table_migration_with_timestamps
run_generator ["create_books", "title:string", "content:text"]
assert_migration "db/migrate/create_books.rb", /t.timestamps/
end
def test_create_table_timestamps_are_skipped
run_generator ["create_books", "title:string", "content:text", "--no-timestamps"]
assert_migration "db/migrate/create_books.rb" do |m|
assert_method :change, m do |change|
assert_no_match(/t.timestamps/, change)
end
end
end
def test_add_uuid_to_create_table_migration
run_generator ["create_books", "--primary_key_type=uuid"]
assert_migration "db/migrate/create_books.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :books, id: :uuid/, change)
end
end
end
def test_add_migration_with_references_options_when_primary_key_uuid
migration = "add_references_to_books"
run_generator [migration, "author:belongs_to", "--primary_key_type=uuid"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_reference :books, :author,.*\sforeign_key: true, type: :uuid/, change)
end
end
end
def test_database_puts_migrations_in_configured_folder
with_database_configuration do
run_generator ["create_books", "--database=secondary"]
assert_migration "db/secondary_migrate/create_books.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :books/, change)
end
end
end
end
def test_database_puts_migrations_in_configured_folder_with_aliases
with_database_configuration do
run_generator ["create_books", "--db=secondary"]
assert_migration "db/secondary_migrate/create_books.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :books/, change)
end
end
end
end
def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_create
migration = "delete_books"
run_generator [migration, "title:string", "content:text"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/^\s*$/, change)
end
end
end
def test_properly_identifies_usage_file
assert generator_class.send(:usage_path)
end
def test_migration_with_singular_table_name
with_singular_table_name do
migration = "add_title_body_to_post"
run_generator [migration, "title:string"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :post, :title, :string/, change)
end
end
end
end
def test_create_join_table_migration_with_singular_table_name
with_singular_table_name do
migration = "add_media_join_table"
run_generator [migration, "artist_id", "music:uniq"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_join_table :artist, :music/, change)
assert_match(/# t\.index \[:artist_id, :music_id\]/, change)
assert_match(/ t\.index \[:music_id, :artist_id\], unique: true/, change)
end
end
end
end
def test_create_table_migration_with_singular_table_name
with_singular_table_name do
run_generator ["create_book", "title:string", "content:text"]
assert_migration "db/migrate/create_book.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :book/, change)
assert_match(/ t\.string :title/, change)
assert_match(/ t\.text :content/, change)
end
end
end
end
def test_create_table_migration_with_token_option
run_generator ["create_users", "token:token", "auth_token:token"]
assert_migration "db/migrate/create_users.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :users/, change)
assert_match(/ t\.string :token/, change)
assert_match(/ t\.string :auth_token/, change)
assert_match(/add_index :users, :token, unique: true/, change)
assert_match(/add_index :users, :auth_token, unique: true/, change)
end
end
end
def test_add_migration_with_token_option
migration = "add_token_to_users"
run_generator [migration, "auth_token:token"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :users, :auth_token, :string/, change)
assert_match(/add_index :users, :auth_token, unique: true/, change)
end
end
end
def test_add_migration_to_configured_path
old_paths = Rails.application.config.paths["db/migrate"]
Rails.application.config.paths.add "db/migrate", with: "db2/migrate"
migration = "migration_in_custom_path"
run_generator [migration]
assert_migration "db2/migrate/#{migration}.rb", /.*/
ensure
Rails.application.config.paths["db/migrate"] = old_paths
end
def test_add_migration_ignores_virtual_attributes
migration = "add_rich_text_content_to_messages"
run_generator [migration, "content:rich_text", "video:attachment", "photos:attachments"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_no_match(/add_column :messages, :content, :rich_text/, change)
assert_no_match(/add_column :messages, :video, :attachment/, change)
assert_no_match(/add_column :messages, :photos, :attachments/, change)
end
end
end
def test_create_table_migration_ignores_virtual_attributes
run_generator ["create_messages", "content:rich_text", "video:attachment", "photos:attachments"]
assert_migration "db/migrate/create_messages.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :messages/, change)
assert_no_match(/ t\.rich_text :content/, change)
assert_no_match(/ t\.attachment :video/, change)
assert_no_match(/ t\.attachments :photos/, change)
end
end
end
def test_remove_migration_with_virtual_attributes
migration = "remove_content_from_messages"
run_generator [migration, "content:rich_text", "video:attachment", "photos:attachments"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_no_match(/remove_column :messages, :content, :rich_text/, change)
assert_no_match(/remove_column :messages, :video, :attachment/, change)
assert_no_match(/remove_column :messages, :photos, :attachments/, change)
end
end
end
private
def with_singular_table_name
old_state = ActiveRecord::Base.pluralize_table_names
ActiveRecord::Base.pluralize_table_names = false
yield
ensure
ActiveRecord::Base.pluralize_table_names = old_state
end
end
|