class ActiveRecord::StatementCacheTest

Public Instance Methods

setup() click to toggle source
# File activerecord/test/cases/statement_cache_test.rb, line 11
def setup
  @connection = ActiveRecord::Base.connection
end
test_find_by_does_not_use_statement_cache_if_table_name_is_changed() click to toggle source
# File activerecord/test/cases/statement_cache_test.rb, line 111
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
test_find_does_not_use_statement_cache_if_table_name_is_changed() click to toggle source
# File activerecord/test/cases/statement_cache_test.rb, line 123
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
test_find_or_create_by() click to toggle source
# File activerecord/test/cases/statement_cache_test.rb, line 44
def test_find_or_create_by
  Book.create(name: "my book")

  a = Book.find_or_create_by(name: "my book")
  b = Book.find_or_create_by(name: "my other book")

  assert_equal("my book", a.name)
  assert_equal("my other book", b.name)
end
test_statement_cache() click to toggle source

Cache v 1.1 tests

# File activerecord/test/cases/statement_cache_test.rb, line 16
def test_statement_cache
  Book.create(name: "my book")
  Book.create(name: "my other book")

  cache = StatementCache.create(Book.connection) do |params|
    Book.where(name: params.bind)
  end

  b = cache.execute([ "my book" ], Book.connection)
  assert_equal "my book", b[0].name
  b = cache.execute([ "my other book" ], Book.connection)
  assert_equal "my other book", b[0].name
end
test_statement_cache_id() click to toggle source
# File activerecord/test/cases/statement_cache_test.rb, line 30
def test_statement_cache_id
  b1 = Book.create(name: "my book")
  b2 = Book.create(name: "my other book")

  cache = StatementCache.create(Book.connection) do |params|
    Book.where(id: params.bind)
  end

  b = cache.execute([ b1.id ], Book.connection)
  assert_equal b1.name, b[0].name
  b = cache.execute([ b2.id ], Book.connection)
  assert_equal b2.name, b[0].name
end
test_statement_cache_values_differ() click to toggle source
# File activerecord/test/cases/statement_cache_test.rb, line 80
def test_statement_cache_values_differ
  cache = ActiveRecord::StatementCache.create(Book.connection) do |params|
    Book.where(name: "my book")
  end

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

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

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

  additional_books = cache.execute([], Book.connection)
  assert first_books != additional_books
end
test_statement_cache_with_complex_statement() click to toggle source
# File activerecord/test/cases/statement_cache_test.rb, line 67
def test_statement_cache_with_complex_statement
  cache = ActiveRecord::StatementCache.create(Book.connection) do |params|
    Liquid.joins(molecules: :electrons).where("molecules.name" => "dioxane", "electrons.name" => "lepton")
  end

  salty = Liquid.create(name: "salty")
  molecule = salty.molecules.create(name: "dioxane")
  molecule.electrons.create(name: "lepton")

  liquids = cache.execute([], Book.connection)
  assert_equal "salty", liquids[0].name
end
test_statement_cache_with_simple_statement() click to toggle source

End

# File activerecord/test/cases/statement_cache_test.rb, line 56
def test_statement_cache_with_simple_statement
  cache = ActiveRecord::StatementCache.create(Book.connection) do |params|
    Book.where(name: "my book").where("author_id > 3")
  end

  Book.create(name: "my book", author_id: 4)

  books = cache.execute([], Book.connection)
  assert_equal "my book", books[0].name
end
test_unprepared_statements_dont_share_a_cache_with_prepared_statements() click to toggle source
# File activerecord/test/cases/statement_cache_test.rb, line 99
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

  refute_equal book, other_book
end