class ModelGeneratorTest

Public Instance Methods

test_add_migration_with_attributes_index_declaration_and_attribute_options() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 178
def test_add_migration_with_attributes_index_declaration_and_attribute_options
  run_generator ["product", "title:string{40}:index", "content:string{255}", "price:decimal{5,2}:index", "discount:decimal{5,2}:uniq", "supplier:references{polymorphic}"]

  assert_migration "db/migrate/create_products.rb" do |content|
    assert_method :change, content do |up|
      assert_match(/create_table :products/, up)
      assert_match(/t.string :title, limit: 40/, up)
      assert_match(/t.string :content, limit: 255/, up)
      assert_match(/t.decimal :price, precision: 5, scale: 2/, up)
      assert_match(/t.references :supplier, polymorphic: true/, up)
    end
    assert_match(/add_index :products, :title/, content)
    assert_match(/add_index :products, :price/, content)
    assert_match(/add_index :products, :discount, unique: true/, content)
  end
end
test_add_uuid_to_create_table_migration() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 370
def test_add_uuid_to_create_table_migration
  run_generator ["account", "--primary_key_type=uuid"]
  assert_migration "db/migrate/create_accounts.rb" do |content|
    assert_method :change, content do |change|
      assert_match(/create_table :accounts, id: :uuid/, change)
    end
  end
end
test_check_class_collision() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 345
def test_check_class_collision
  content = capture(:stderr) { run_generator ["object"] }
  assert_match(/The name 'Object' is either already used in your application or reserved/, content)
end
test_existing_migration_is_removed_on_force() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 282
def test_existing_migration_is_removed_on_force
  run_generator
  old_migration = Dir["#{destination_root}/db/migrate/*_create_accounts.rb"].first
  error = capture(:stderr) { run_generator ["Account", "--force"] }
  assert_no_match(/Another migration is already named create_accounts/, error)
  assert_no_file old_migration
  assert_migration "db/migrate/create_accounts.rb"
end
test_fixture_is_skipped() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 324
def test_fixture_is_skipped
  run_generator ["account", "--skip-fixture"]
  assert_no_file "test/fixtures/accounts.yml"
end
test_fixture_is_skipped_if_fixture_replacement_is_given() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 329
def test_fixture_is_skipped_if_fixture_replacement_is_given
  content = run_generator ["account", "-r", "factory_girl"]
  assert_match(/factory_girl \[not found\]/, content)
  assert_no_file "test/fixtures/accounts.yml"
end
test_fixture_without_pluralization() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 335
def test_fixture_without_pluralization
  original_pluralize_table_name = ActiveRecord::Base.pluralize_table_names
  ActiveRecord::Base.pluralize_table_names = false
  run_generator
  assert_generated_fixture("test/fixtures/account.yml",
                           "one" => { "name" => "MyString", "age" => 1 }, "two" => { "name" => "MyString", "age" => 1 })
ensure
  ActiveRecord::Base.pluralize_table_names = original_pluralize_table_name
end
test_fixtures_respect_reserved_yml_keywords() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 317
def test_fixtures_respect_reserved_yml_keywords
  run_generator ["LineItem", "no:integer", "Off:boolean", "ON:boolean"]

  assert_generated_fixture("test/fixtures/line_items.yml",
                           "one" => { "no" => 1, "Off" => false, "ON" => false }, "two" => { "no" => 1, "Off" => false, "ON" => false })
end
test_fixtures_use_the_references_ids() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 300
def test_fixtures_use_the_references_ids
  run_generator ["LineItem", "product:references", "cart:belongs_to"]

  assert_file "test/fixtures/line_items.yml", /product: one\n  cart: one/
  assert_generated_fixture("test/fixtures/line_items.yml",
                           "one" => { "product" => "one", "cart" => "one" }, "two" => { "product" => "two", "cart" => "two" })
end
test_fixtures_use_the_references_ids_and_type() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 308
def test_fixtures_use_the_references_ids_and_type
  run_generator ["LineItem", "product:references{polymorphic}", "cart:belongs_to"]

  assert_file "test/fixtures/line_items.yml", /product: one\n  product_type: Product\n  cart: one/
  assert_generated_fixture("test/fixtures/line_items.yml",
                           "one" => { "product" => "one", "product_type" => "Product", "cart" => "one" },
                            "two" => { "product" => "two", "product_type" => "Product", "cart" => "two" })
end
test_foreign_key_is_added_for_references() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 432
def test_foreign_key_is_added_for_references
  run_generator ["account", "supplier:belongs_to", "user:references"]

  assert_migration "db/migrate/create_accounts.rb" do |m|
    assert_method :change, m do |up|
      assert_match(/t\.belongs_to :supplier,.*\sforeign_key: true/, up)
      assert_match(/t\.references :user,.*\sforeign_key: true/, up)
    end
  end
end
test_foreign_key_is_not_added_for_non_references() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 422
def test_foreign_key_is_not_added_for_non_references
  run_generator ["account", "supplier:string"]

  assert_migration "db/migrate/create_accounts.rb" do |m|
    assert_method :change, m do |up|
      assert_no_match(/foreign_key/, up)
    end
  end
end
test_foreign_key_is_skipped_for_polymorphic_references() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 443
def test_foreign_key_is_skipped_for_polymorphic_references
  run_generator ["account", "supplier:belongs_to{polymorphic}"]

  assert_migration "db/migrate/create_accounts.rb" do |m|
    assert_method :change, m do |up|
      assert_no_match(/foreign_key/, up)
    end
  end
end
test_help_shows_invoked_generators_options() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 11
def test_help_shows_invoked_generators_options
  content = run_generator ["--help"]
  assert_match(/ActiveRecord options:/, content)
  assert_match(/TestUnit options:/, content)
end
test_index_is_skipped_for_belongs_to_association() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 350
def test_index_is_skipped_for_belongs_to_association
  run_generator ["account", "supplier:belongs_to", "--no-indexes"]

  assert_migration "db/migrate/create_accounts.rb" do |m|
    assert_method :change, m do |up|
      assert_no_match(/index: true/, up)
    end
  end
end
test_index_is_skipped_for_references_association() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 360
def test_index_is_skipped_for_references_association
  run_generator ["account", "supplier:references", "--no-indexes"]

  assert_migration "db/migrate/create_accounts.rb" do |m|
    assert_method :change, m do |up|
      assert_no_match(/index: true/, up)
    end
  end
end
test_invokes_default_orm() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 29
def test_invokes_default_orm
  run_generator
  assert_file "app/models/account.rb", /class Account < ApplicationRecord/
end
test_invokes_default_test_framework() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 291
def test_invokes_default_test_framework
  run_generator
  assert_file "test/models/account_test.rb", /class AccountTest < ActiveSupport::TestCase/

  assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/
  assert_generated_fixture("test/fixtures/accounts.yml",
                           "one" => { "name" => "MyString", "age" => 1 }, "two" => { "name" => "MyString", "age" => 1 })
end
test_migration() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 60
def test_migration
  run_generator
  assert_migration "db/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/
end
test_migration_error_is_not_shown_on_revoke() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 270
def test_migration_error_is_not_shown_on_revoke
  run_generator
  error = capture(:stderr) { run_generator ["Account"], behavior: :revoke }
  assert_no_match(/Another migration is already named create_accounts/, error)
end
test_migration_is_ignored_as_identical_with_skip_option() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 258
def test_migration_is_ignored_as_identical_with_skip_option
  run_generator ["Account"]
  output = run_generator ["Account", "--skip"]
  assert_match %r{identical\s+db/migrate/\d+_create_accounts\.rb}, output
end
test_migration_is_removed_on_revoke() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 276
def test_migration_is_removed_on_revoke
  run_generator
  run_generator ["Account"], behavior: :revoke
  assert_no_migration "db/migrate/create_accounts.rb"
end
test_migration_is_skipped() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 109
def test_migration_is_skipped
  run_generator ["account", "--no-migration"]
  assert_no_migration "db/migrate/create_accounts.rb"
end
test_migration_is_skipped_on_skip_behavior() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 264
def test_migration_is_skipped_on_skip_behavior
  run_generator
  output = run_generator ["Account"], behavior: :skip
  assert_match %r{skip\s+db/migrate/\d+_create_accounts\.rb}, output
end
test_migration_is_skipped_with_skip_option() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 252
def test_migration_is_skipped_with_skip_option
  run_generator
  output = run_generator ["Account", "--skip"]
  assert_match %r{skip\s+db/migrate/\d+_create_accounts\.rb}, output
end
test_migration_timestamps_are_skipped() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 242
def test_migration_timestamps_are_skipped
  run_generator ["account", "--no-timestamps"]

  assert_migration "db/migrate/create_accounts.rb" do |m|
    assert_method :change, m do |up|
      assert_no_match(/t\.timestamps/, up)
    end
  end
end
test_migration_with_attributes() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 114
def test_migration_with_attributes
  run_generator ["product", "name:string", "supplier_id:integer"]

  assert_migration "db/migrate/create_products.rb" do |m|
    assert_method :change, m do |up|
      assert_match(/create_table :products/, up)
      assert_match(/t\.string :name/, up)
      assert_match(/t\.integer :supplier_id/, up)
    end
  end
end
test_migration_with_attributes_and_with_index() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 126
def test_migration_with_attributes_and_with_index
  run_generator ["product", "name:string:index", "supplier_id:integer:index", "user_id:integer:uniq", "order_id:uniq"]

  assert_migration "db/migrate/create_products.rb" do |m|
    assert_method :change, m do |up|
      assert_match(/create_table :products/, up)
      assert_match(/t\.string :name/, up)
      assert_match(/t\.integer :supplier_id/, up)
      assert_match(/t\.integer :user_id/, up)
      assert_match(/t\.string :order_id/, up)

      assert_match(/add_index :products, :name/, up)
      assert_match(/add_index :products, :supplier_id/, up)
      assert_match(/add_index :products, :user_id, unique: true/, up)
      assert_match(/add_index :products, :order_id, unique: true/, up)
    end
  end
end
test_migration_with_attributes_and_with_wrong_index_declaration() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 145
def test_migration_with_attributes_and_with_wrong_index_declaration
  run_generator ["product", "name:string", "supplier_id:integer:inex", "user_id:integer:unqu"]

  assert_migration "db/migrate/create_products.rb" do |m|
    assert_method :change, m do |up|
      assert_match(/create_table :products/, up)
      assert_match(/t\.string :name/, up)
      assert_match(/t\.integer :supplier_id/, up)
      assert_match(/t\.integer :user_id/, up)

      assert_no_match(/add_index :products, :name/, up)
      assert_no_match(/add_index :products, :supplier_id/, up)
      assert_no_match(/add_index :products, :user_id/, up)
    end
  end
end
test_migration_with_configured_path() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 206
def test_migration_with_configured_path
  old_paths = Quails.application.config.paths["db/migrate"]
  Quails.application.config.paths.add "db/migrate", with: "db2/migrate"

  run_generator

  assert_migration "db2/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/
ensure
  Quails.application.config.paths["db/migrate"] = old_paths
end
test_migration_with_missing_attribute_type_and_with_index() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 162
def test_migration_with_missing_attribute_type_and_with_index
  run_generator ["product", "name:index", "supplier_id:integer:index", "year:integer"]

  assert_migration "db/migrate/create_products.rb" do |m|
    assert_method :change, m do |up|
      assert_match(/create_table :products/, up)
      assert_match(/t\.string :name/, up)
      assert_match(/t\.integer :supplier_id/, up)

      assert_match(/add_index :products, :name/, up)
      assert_match(/add_index :products, :supplier_id/, up)
      assert_no_match(/add_index :products, :year/, up)
    end
  end
end
test_migration_with_namespace() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 65
def test_migration_with_namespace
  run_generator ["Gallery::Image"]
  assert_migration "db/migrate/create_gallery_images", /class CreateGalleryImages < ActiveRecord::Migration\[[0-9.]+\]/
  assert_no_migration "db/migrate/create_images"
end
test_migration_with_namespaces_in_model_name_without_plurization() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 91
def test_migration_with_namespaces_in_model_name_without_plurization
  ActiveRecord::Base.pluralize_table_names = false
  run_generator ["Gallery::Image"]
  assert_migration "db/migrate/create_gallery_image", /class CreateGalleryImage < ActiveRecord::Migration\[[0-9.]+\]/
  assert_no_migration "db/migrate/create_gallery_images"
ensure
  ActiveRecord::Base.pluralize_table_names = true
end
test_migration_with_nested_namespace() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 71
def test_migration_with_nested_namespace
  run_generator ["Admin::Gallery::Image"]
  assert_no_migration "db/migrate/create_images"
  assert_no_migration "db/migrate/create_gallery_images"
  assert_migration "db/migrate/create_admin_gallery_images", /class CreateAdminGalleryImages < ActiveRecord::Migration\[[0-9.]+\]/
  assert_migration "db/migrate/create_admin_gallery_images", /create_table :admin_gallery_images/
end
test_migration_with_nested_namespace_without_pluralization() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 79
def test_migration_with_nested_namespace_without_pluralization
  ActiveRecord::Base.pluralize_table_names = false
  run_generator ["Admin::Gallery::Image"]
  assert_no_migration "db/migrate/create_images"
  assert_no_migration "db/migrate/create_gallery_images"
  assert_no_migration "db/migrate/create_admin_gallery_images"
  assert_migration "db/migrate/create_admin_gallery_image", /class CreateAdminGalleryImage < ActiveRecord::Migration\[[0-9.]+\]/
  assert_migration "db/migrate/create_admin_gallery_image", /create_table :admin_gallery_image/
ensure
  ActiveRecord::Base.pluralize_table_names = true
end
test_migration_with_timestamps() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 237
def test_migration_with_timestamps
  run_generator
  assert_migration "db/migrate/create_accounts.rb", /t\.timestamps/
end
test_migration_without_pluralization() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 100
def test_migration_without_pluralization
  ActiveRecord::Base.pluralize_table_names = false
  run_generator
  assert_migration "db/migrate/create_account", /class CreateAccount < ActiveRecord::Migration\[[0-9.]+\]/
  assert_no_migration "db/migrate/create_accounts"
ensure
  ActiveRecord::Base.pluralize_table_names = true
end
test_migration_without_timestamps() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 195
def test_migration_without_timestamps
  ActiveRecord::Base.timestamped_migrations = false
  run_generator ["account"]
  assert_file "db/migrate/001_create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/

  run_generator ["project"]
  assert_file "db/migrate/002_create_projects.rb", /class CreateProjects < ActiveRecord::Migration\[[0-9.]+\]/
ensure
  ActiveRecord::Base.timestamped_migrations = true
end
test_model_with_belongs_to_attribute_generates_belongs_to_associations() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 222
def test_model_with_belongs_to_attribute_generates_belongs_to_associations
  run_generator ["product", "name:string", "supplier:belongs_to"]
  assert_file "app/models/product.rb", /belongs_to :supplier/
end
test_model_with_missing_attribute_type() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 17
def test_model_with_missing_attribute_type
  run_generator ["post", "title", "body:text", "author"]

  assert_migration "db/migrate/create_posts.rb" do |m|
    assert_method :change, m do |up|
      assert_match(/t\.string :title/, up)
      assert_match(/t\.text :body/, up)
      assert_match(/t\.string :author/, up)
    end
  end
end
test_model_with_namespace() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 52
def test_model_with_namespace
  run_generator ["admin/account"]
  assert_file "app/models/admin.rb", /module Admin/
  assert_file "app/models/admin.rb", /def self\.table_name_prefix/
  assert_file "app/models/admin.rb", /'admin_'/
  assert_file "app/models/admin/account.rb", /class Admin::Account < ApplicationRecord/
end
test_model_with_parent_option() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 34
def test_model_with_parent_option
  run_generator ["account", "--parent", "Admin::Account"]
  assert_file "app/models/account.rb", /class Account < Admin::Account/
  assert_no_migration "db/migrate/create_accounts.rb"
end
test_model_with_polymorphic_belongs_to_attribute_generates_belongs_to_associations() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 232
def test_model_with_polymorphic_belongs_to_attribute_generates_belongs_to_associations
  run_generator ["product", "name:string", "supplier:belongs_to{polymorphic}"]
  assert_file "app/models/product.rb", /belongs_to :supplier, polymorphic: true/
end
test_model_with_polymorphic_references_attribute_generates_belongs_to_associations() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 227
def test_model_with_polymorphic_references_attribute_generates_belongs_to_associations
  run_generator ["product", "name:string", "supplier:references{polymorphic}"]
  assert_file "app/models/product.rb", /belongs_to :supplier, polymorphic: true/
end
test_model_with_references_attribute_generates_belongs_to_associations() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 217
def test_model_with_references_attribute_generates_belongs_to_associations
  run_generator ["product", "name:string", "supplier:references"]
  assert_file "app/models/product.rb", /belongs_to :supplier/
end
test_model_with_underscored_parent_option() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 47
def test_model_with_underscored_parent_option
  run_generator ["account", "--parent", "admin/account"]
  assert_file "app/models/account.rb", /class Account < Admin::Account/
end
test_plural_names_are_singularized() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 40
def test_plural_names_are_singularized
  content = run_generator ["accounts".freeze]
  assert_file "app/models/account.rb", /class Account < ApplicationRecord/
  assert_file "test/models/account_test.rb", /class AccountTest/
  assert_match(/\[WARNING\] The model name 'accounts' was recognized as a plural, using the singular 'account' instead\. Override with --force-plural or setup custom inflection rules for this noun before running the generator\./, content)
end
test_required_adds_null_false_to_column() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 412
def test_required_adds_null_false_to_column
  run_generator ["account", "supplier:references{required}"]

  assert_migration "db/migrate/create_accounts.rb" do |m|
    assert_method :change, m do |up|
      assert_match(/t\.references :supplier,.*\snull: false/, up)
    end
  end
end
test_required_and_polymorphic_are_order_independent() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 401
  def test_required_and_polymorphic_are_order_independent
    run_generator ["account", "supplier:references{polymorphic.required}"]

    expected_file = <<-FILE.strip_heredoc
    class Account < ApplicationRecord
      belongs_to :supplier, polymorphic: true, required: true
    end
    FILE
    assert_file "app/models/account.rb", expected_file
  end
test_required_belongs_to_adds_required_association() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 379
  def test_required_belongs_to_adds_required_association
    run_generator ["account", "supplier:references{required}"]

    expected_file = <<-FILE.strip_heredoc
    class Account < ApplicationRecord
      belongs_to :supplier, required: true
    end
    FILE
    assert_file "app/models/account.rb", expected_file
  end
test_required_polymorphic_belongs_to_generages_correct_model() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 390
  def test_required_polymorphic_belongs_to_generages_correct_model
    run_generator ["account", "supplier:references{required,polymorphic}"]

    expected_file = <<-FILE.strip_heredoc
    class Account < ApplicationRecord
      belongs_to :supplier, polymorphic: true, required: true
    end
    FILE
    assert_file "app/models/account.rb", expected_file
  end
test_token_option_adds_has_secure_token() click to toggle source
# File railties/test/generators/model_generator_test.rb, line 453
  def test_token_option_adds_has_secure_token
    run_generator ["user", "token:token", "auth_token:token"]
    expected_file = <<-FILE.strip_heredoc
    class User < ApplicationRecord
      has_secure_token
      has_secure_token :auth_token
    end
    FILE
    assert_file "app/models/user.rb", expected_file
  end

Private Instance Methods

assert_generated_fixture(path, parsed_contents) click to toggle source
# File railties/test/generators/model_generator_test.rb, line 465
def assert_generated_fixture(path, parsed_contents)
  fixture_file = File.new File.expand_path(path, destination_root)
  assert_equal(parsed_contents, YAML.load(fixture_file))
end