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

Code before:
      end

      first_books = cache.execute([], Book.connection)

      3.times do
        Book.create(name: "my book")
      end

      additional_books = cache.execute([], Book.connection)
      assert_not_equal first_books, additional_books
    end

    def test_unprepared_statements_dont_share_a_cache_with_prepared_statements
      Book.create(name: "my book")
      Book.create(name: "my other book")

      book = Book.find_by(name: "my book")
      other_book = Book.connection.unprepared_statement do
        Book.find_by(name: "my other book")
      end

      assert_not_equal book, other_book
    end

    def test_find_by_does_not_use_statement_cache_if_table_name_is_changed
      book = Book.create(name: "my book")

      Book.find_by(name: book.name) # warming the statement cache.

      # changing the table name should change the query that is not cached.
      Book.table_name = :birds
      assert_nil Book.find_by(name: book.name)
    ensure
      Book.table_name = :books
    end

    def test_find_does_not_use_statement_cache_if_table_name_is_changed
      book = Book.create(name: "my book")

      Book.find(book.id) # warming the statement cache.

      # changing the table name should change the query that is not cached.
      Book.table_name = :birds
      assert_raise ActiveRecord::RecordNotFound do
        Book.find(book.id)
      end
    ensure
      Book.table_name = :books
    end

    def test_find_association_does_not_use_statement_cache_if_table_name_is_changed

Review comment (suggestion):
Maybe inline model would be easier to use in here, since there will be no need for cleanup. :thinking: 

Code after:
      end

      first_books = cache.execute([], Book.connection)

      3.times do
        Book.create(name: "my book")
      end

      additional_books = cache.execute([], Book.connection)
      assert_not_equal first_books, additional_books
    end

    def test_unprepared_statements_dont_share_a_cache_with_prepared_statements
      Book.create(name: "my book")
      Book.create(name: "my other book")

      book = Book.find_by(name: "my book")
      other_book = Book.connection.unprepared_statement do
        Book.find_by(name: "my other book")
      end

      assert_not_equal book, other_book
    end

    def test_find_by_does_not_use_statement_cache_if_table_name_is_changed
      liquid = Liquid.create(name: "salty")

      Liquid.find_by(name: liquid.name) # warming the statement cache.

      # changing the table name should change the query that is not cached.
      Liquid.table_name = :birds
      assert_nil Liquid.find_by(name: liquid.name)
    ensure
      Liquid.table_name = :liquid
    end

    def test_find_does_not_use_statement_cache_if_table_name_is_changed
      liquid = Liquid.create(name: "salty")

      Liquid.find(liquid.id) # warming the statement cache.

      # changing the table name should change the query that is not cached.
      Liquid.table_name = :birds
      assert_raise ActiveRecord::RecordNotFound do
        Liquid.find(liquid.id)
      end
    ensure
      Liquid.table_name = :liquid
    end

    def test_find_association_does_not_use_statement_cache_if_table_name_is_changed