Repository: rails/rails
File: activerecord/test/cases/migration_test.rb (Ruby)

Code before:

  def test_create_table_with_if_not_exists_true
    connection = Person.lease_connection
    connection.create_table :testings, force: true do |t|
      t.string :foo
    end

    assert_nothing_raised do
      connection.create_table :testings, if_not_exists: true do |t|
        t.string :foo
      end
    end
  ensure
    connection.drop_table :testings, if_exists: true
  end

  def test_create_table_raises_for_long_table_names
    connection = Person.lease_connection
    name_limit = connection.table_name_length
    long_name = "a" * (name_limit + 1)
    short_name = "a" * name_limit

    error = assert_raises(ArgumentError) do
      connection.create_table(long_name)
    end
    assert_equal "Table name '#{long_name}' is too long \(#{name_limit + 1} characters\); the limit is #{name_limit} characters", error.message

    connection.create_table(short_name)
    assert connection.table_exists?(short_name)
  ensure
    connection.drop_table short_name, if_exists: true
  end

  def test_create_table_with_force_and_if_not_exists
    connection = Person.lease_connection
    assert_raises(ArgumentError, match: /Options `:force` and `:if_not_exists` cannot be used simultaneously/) do
      connection.create_table(:testings, force: true, if_not_exists: true)
    end
  end

  def test_create_table_with_indexes_and_if_not_exists_true
    connection = Person.lease_connection
    connection.create_table :testings, force: true do |t|
      t.references :people
      t.string :foo
    end

    assert_nothing_raised do
      connection.create_table :testings, if_not_exists: true do |t|
        t.references :people
        t.string :foo

Review comment (refactor):
```suggestion
    assert_equal "Table name '#{long_name}' is too long \(#{long_name.length} characters\); the limit is #{name_limit} characters", error.message
```

Code after:

  def test_create_table_with_if_not_exists_true
    connection = Person.lease_connection
    connection.create_table :testings, force: true do |t|
      t.string :foo
    end

    assert_nothing_raised do
      connection.create_table :testings, if_not_exists: true do |t|
        t.string :foo
      end
    end
  ensure
    connection.drop_table :testings, if_exists: true
  end

  def test_create_table_raises_for_long_table_names
    connection = Person.lease_connection
    name_limit = connection.table_name_length
    long_name = "a" * (name_limit + 1)
    short_name = "a" * name_limit

    error = assert_raises(ArgumentError) do
      connection.create_table(long_name)
    end
    assert_equal "Table name '#{long_name}' is too long \(#{long_name.length} characters\); the limit is #{name_limit} characters", error.message

    connection.create_table(short_name)
    assert connection.table_exists?(short_name)
  ensure
    connection.drop_table short_name, if_exists: true
  end

  def test_create_table_with_force_and_if_not_exists
    connection = Person.lease_connection
    assert_raises(ArgumentError, match: /Options `:force` and `:if_not_exists` cannot be used simultaneously/) do
      connection.create_table(:testings, force: true, if_not_exists: true)
    end
  end

  def test_create_table_with_indexes_and_if_not_exists_true
    connection = Person.lease_connection
    connection.create_table :testings, force: true do |t|
      t.references :people
      t.string :foo
    end

    assert_nothing_raised do
      connection.create_table :testings, if_not_exists: true do |t|
        t.references :people
        t.string :foo