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

Code before:

    topic_class.undefine_attribute_methods
    assert_equal true, topic.respond_to?(:subject_to_be_undefined)
    assert_equal true, topic_class.method_defined?(:subject_to_be_undefined)
  end

  test "#define_attribute_methods brings back undefined aliases" do
    topic_class = Class.new(ActiveRecord::Base) do
      self.table_name = "topics"

      alias_attribute :title_alias_to_be_undefined, :title
    end

    topic = topic_class.new(title: "New topic")
    assert_equal("New topic", topic.title_alias_to_be_undefined)
    topic_class.undefine_attribute_methods

    assert_equal false, topic_class.method_defined?(:title_alias_to_be_undefined)

    topic_class.define_attribute_methods

    assert_equal true, topic_class.method_defined?(:title_alias_to_be_undefined)
    assert_equal "New topic", topic.title_alias_to_be_undefined
  end

  # Test for de765e37c1d
  test "#define_attribute_methods doesn't connect to the database when schema cache is present" do
    with_temporary_connection_pool do
      if in_memory_db?
        # Separate connections to an in-memory database create an entirely new database,
        # with an empty schema etc, so we just stub out this schema on the fly.
        ActiveRecord::Base.connection_pool.with_connection do |connection|
          connection.create_table :tasks do |t|
            t.datetime :starting
            t.datetime :ending
          end
        end
      end

      @target.table_name = "tasks"

      @target.connection_pool.schema_cache.load!
      @target.connection_pool.schema_cache.add("tasks")
      @target.connection_pool.disconnect!

      assert_no_queries(include_schema: true) do
        @target.define_attribute_methods
      end
    ensure
      ActiveRecord::Base.connection_pool.disconnect!
    end

Review comment (suggestion):
```suggestion
```

If you want add this information in the commit message, that shoul de sufficient to know why this test was added.

Code after:

    topic_class.undefine_attribute_methods
    assert_equal true, topic.respond_to?(:subject_to_be_undefined)
    assert_equal true, topic_class.method_defined?(:subject_to_be_undefined)
  end

  test "#define_attribute_methods brings back undefined aliases" do
    topic_class = Class.new(ActiveRecord::Base) do
      self.table_name = "topics"

      alias_attribute :title_alias_to_be_undefined, :title
    end

    topic = topic_class.new(title: "New topic")
    assert_equal("New topic", topic.title_alias_to_be_undefined)
    topic_class.undefine_attribute_methods

    assert_equal false, topic_class.method_defined?(:title_alias_to_be_undefined)

    topic_class.define_attribute_methods

    assert_equal true, topic_class.method_defined?(:title_alias_to_be_undefined)
    assert_equal "New topic", topic.title_alias_to_be_undefined
  end

  test "#define_attribute_methods doesn't connect to the database when schema cache is present" do
    with_temporary_connection_pool do
      if in_memory_db?
        # Separate connections to an in-memory database create an entirely new database,
        # with an empty schema etc, so we just stub out this schema on the fly.
        ActiveRecord::Base.connection_pool.with_connection do |connection|
          connection.create_table :tasks do |t|
            t.datetime :starting
            t.datetime :ending
          end
        end
      end

      @target.table_name = "tasks"

      @target.connection_pool.schema_cache.load!
      @target.connection_pool.schema_cache.add("tasks")
      @target.connection_pool.disconnect!

      assert_no_queries(include_schema: true) do
        @target.define_attribute_methods
      end
    ensure
      ActiveRecord::Base.connection_pool.disconnect!
    end
  end