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

Code before:
        e = assert_raises(ArgumentError) {
          connection.rename_index(table_name, "old_idx", too_long_index_name)
        }
        assert_match(/too long \(#{connection.index_name_length + 1} characters\); the limit is #{connection.index_name_length} characters/, e.message)

        assert connection.index_name_exists?(table_name, "old_idx")
      end

      def test_remove_nonexistent_index
        assert_raise(ArgumentError) { connection.remove_index(table_name, "no_such_index") }
      end

      def test_add_index_works_with_long_index_names
        connection.add_index(table_name, "foo", name: good_index_name)

        assert connection.index_name_exists?(table_name, good_index_name)
        connection.remove_index(table_name, name: good_index_name)
      end

      def test_add_index_does_not_accept_too_long_index_names
        too_long_index_name = good_index_name + "x"

        e = assert_raises(ArgumentError) {
          connection.add_index(table_name, "foo", name: too_long_index_name)
        }
        assert_match(/too long \(#{connection.index_name_length + 1} characters\); the limit is #{connection.index_name_length} characters/, e.message)

        assert_not connection.index_name_exists?(table_name, too_long_index_name)
        connection.add_index(table_name, "foo", name: good_index_name)
      end

      def test_add_index_which_already_exists_does_not_raise_error_with_option
        connection.add_index(table_name, "foo")

        assert_nothing_raised do
          connection.add_index(table_name, "foo", if_not_exists: true)
        end

        assert connection.index_name_exists?(table_name, "index_testings_on_foo")
      end

      def test_add_index_with_if_not_exists_matches_exact_index
        connection.add_index(table_name, [:foo, :bar], unique: false, name: "index_testings_on_foo_bar")

        assert connection.index_name_exists?(table_name, "index_testings_on_foo_bar")

        assert_nothing_raised do
          connection.add_index(table_name, [:foo, :bar], unique: true, if_not_exists: true)
        end

        assert connection.index_name_exists?(table_name, "index_testings_on_foo_and_bar")

Review comment (refactor):
```suggestion
        assert_match(/too long \(#{too_long_index_name.length} characters\); the limit is #{connection.index_name_length} characters/, e.message)
```

Code after:
        e = assert_raises(ArgumentError) {
          connection.rename_index(table_name, "old_idx", too_long_index_name)
        }
        assert_match(/too long \(#{too_long_index_name.length} characters\); the limit is #{connection.index_name_length} characters/, e.message)

        assert connection.index_name_exists?(table_name, "old_idx")
      end

      def test_remove_nonexistent_index
        assert_raise(ArgumentError) { connection.remove_index(table_name, "no_such_index") }
      end

      def test_add_index_works_with_long_index_names
        connection.add_index(table_name, "foo", name: good_index_name)

        assert connection.index_name_exists?(table_name, good_index_name)
        connection.remove_index(table_name, name: good_index_name)
      end

      def test_add_index_does_not_accept_too_long_index_names
        too_long_index_name = good_index_name + "x"

        e = assert_raises(ArgumentError) {
          connection.add_index(table_name, "foo", name: too_long_index_name)
        }
        assert_match(/too long \(#{too_long_index_name.length} characters\); the limit is #{connection.index_name_length} characters/, e.message)

        assert_not connection.index_name_exists?(table_name, too_long_index_name)
        connection.add_index(table_name, "foo", name: good_index_name)
      end

      def test_add_index_which_already_exists_does_not_raise_error_with_option
        connection.add_index(table_name, "foo")

        assert_nothing_raised do
          connection.add_index(table_name, "foo", if_not_exists: true)
        end

        assert connection.index_name_exists?(table_name, "index_testings_on_foo")
      end

      def test_add_index_with_if_not_exists_matches_exact_index
        connection.add_index(table_name, [:foo, :bar], unique: false, name: "index_testings_on_foo_bar")

        assert connection.index_name_exists?(table_name, "index_testings_on_foo_bar")

        assert_nothing_raised do
          connection.add_index(table_name, [:foo, :bar], unique: true, if_not_exists: true)
        end

        assert connection.index_name_exists?(table_name, "index_testings_on_foo_and_bar")