2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/book"
|
|
|
|
require "models/liquid"
|
|
|
|
require "models/molecule"
|
|
|
|
require "models/electron"
|
2013-04-10 10:40:01 -04:00
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class StatementCacheTest < ActiveRecord::TestCase
|
|
|
|
def setup
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
2013-05-15 10:20:23 -04:00
|
|
|
def test_statement_cache
|
|
|
|
Book.create(name: "my book")
|
|
|
|
Book.create(name: "my other book")
|
|
|
|
|
2014-04-10 20:24:10 -04:00
|
|
|
cache = StatementCache.create(Book.connection) do |params|
|
2016-08-06 13:37:57 -04:00
|
|
|
Book.where(name: params.bind)
|
2013-05-15 10:20:23 -04:00
|
|
|
end
|
|
|
|
|
2017-08-02 17:35:20 -04:00
|
|
|
b = cache.execute([ "my book" ], Book.connection)
|
2013-05-15 10:20:23 -04:00
|
|
|
assert_equal "my book", b[0].name
|
2017-08-02 17:35:20 -04:00
|
|
|
b = cache.execute([ "my other book" ], Book.connection)
|
2013-05-15 10:20:23 -04:00
|
|
|
assert_equal "my other book", b[0].name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_statement_cache_id
|
2014-01-14 17:33:03 -05:00
|
|
|
b1 = Book.create(name: "my book")
|
|
|
|
b2 = Book.create(name: "my other book")
|
2013-05-15 10:20:23 -04:00
|
|
|
|
2014-04-10 20:24:10 -04:00
|
|
|
cache = StatementCache.create(Book.connection) do |params|
|
2014-04-12 21:40:29 -04:00
|
|
|
Book.where(id: params.bind)
|
2013-05-15 10:20:23 -04:00
|
|
|
end
|
|
|
|
|
2017-08-02 17:35:20 -04:00
|
|
|
b = cache.execute([ b1.id ], Book.connection)
|
2014-01-14 17:33:03 -05:00
|
|
|
assert_equal b1.name, b[0].name
|
2017-08-02 17:35:20 -04:00
|
|
|
b = cache.execute([ b2.id ], Book.connection)
|
2014-01-14 17:33:03 -05:00
|
|
|
assert_equal b2.name, b[0].name
|
2013-05-15 10:20:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-04-10 10:40:01 -04:00
|
|
|
def test_statement_cache_with_simple_statement
|
2014-04-10 20:24:10 -04:00
|
|
|
cache = ActiveRecord::StatementCache.create(Book.connection) do |params|
|
2013-04-10 10:40:01 -04:00
|
|
|
Book.where(name: "my book").where("author_id > 3")
|
|
|
|
end
|
|
|
|
|
|
|
|
Book.create(name: "my book", author_id: 4)
|
|
|
|
|
2017-08-02 17:35:20 -04:00
|
|
|
books = cache.execute([], Book.connection)
|
2013-04-10 10:40:01 -04:00
|
|
|
assert_equal "my book", books[0].name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_statement_cache_with_complex_statement
|
2014-04-10 20:24:10 -04:00
|
|
|
cache = ActiveRecord::StatementCache.create(Book.connection) do |params|
|
2016-08-06 13:37:57 -04:00
|
|
|
Liquid.joins(molecules: :electrons).where("molecules.name" => "dioxane", "electrons.name" => "lepton")
|
2013-04-10 10:40:01 -04:00
|
|
|
end
|
2013-04-10 15:02:26 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
salty = Liquid.create(name: "salty")
|
|
|
|
molecule = salty.molecules.create(name: "dioxane")
|
|
|
|
molecule.electrons.create(name: "lepton")
|
2013-04-10 10:40:01 -04:00
|
|
|
|
2017-08-02 17:35:20 -04:00
|
|
|
liquids = cache.execute([], Book.connection)
|
2013-04-10 15:02:26 -04:00
|
|
|
assert_equal "salty", liquids[0].name
|
2013-04-10 10:40:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_statement_cache_values_differ
|
2014-04-10 20:24:10 -04:00
|
|
|
cache = ActiveRecord::StatementCache.create(Book.connection) do |params|
|
2013-04-10 10:40:01 -04:00
|
|
|
Book.where(name: "my book")
|
|
|
|
end
|
2013-04-10 15:02:26 -04:00
|
|
|
|
2013-04-11 08:38:46 -04:00
|
|
|
3.times do
|
2013-04-10 10:40:01 -04:00
|
|
|
Book.create(name: "my book")
|
|
|
|
end
|
|
|
|
|
2017-08-02 17:35:20 -04:00
|
|
|
first_books = cache.execute([], Book.connection)
|
2013-04-10 15:02:26 -04:00
|
|
|
|
2013-04-11 08:38:46 -04:00
|
|
|
3.times do
|
2013-04-10 10:40:01 -04:00
|
|
|
Book.create(name: "my book")
|
|
|
|
end
|
|
|
|
|
2017-08-02 17:35:20 -04:00
|
|
|
additional_books = cache.execute([], Book.connection)
|
2013-04-10 15:02:26 -04:00
|
|
|
assert first_books != additional_books
|
2013-04-10 10:40:01 -04:00
|
|
|
end
|
2016-03-31 15:33:42 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-04-03 21:34:51 -04:00
|
|
|
assert_not_equal book, other_book
|
2016-03-31 15:33:42 -04:00
|
|
|
end
|
2017-02-09 12:04:28 -05:00
|
|
|
|
|
|
|
def test_find_by_does_not_use_statement_cache_if_table_name_is_changed
|
|
|
|
book = Book.create(name: "my book")
|
|
|
|
|
2017-02-13 05:27:30 -05:00
|
|
|
Book.find_by(name: book.name) # warming the statement cache.
|
2017-02-09 12:04:28 -05:00
|
|
|
|
|
|
|
# changing the table name should change the query that is not cached.
|
|
|
|
Book.table_name = :birds
|
2017-02-13 05:27:30 -05:00
|
|
|
assert_nil Book.find_by(name: book.name)
|
2017-02-09 12:04:28 -05:00
|
|
|
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
|
2013-04-10 10:40:01 -04:00
|
|
|
end
|
2014-01-14 17:26:00 -05:00
|
|
|
end
|