2013-04-10 10:40:01 -04:00
|
|
|
require 'cases/helper'
|
|
|
|
require 'models/book'
|
|
|
|
require 'models/liquid'
|
|
|
|
require 'models/molecule'
|
|
|
|
require 'models/electron'
|
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class StatementCacheTest < ActiveRecord::TestCase
|
|
|
|
def setup
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_statement_cache_with_simple_statement
|
|
|
|
cache = ActiveRecord::StatementCache.new do
|
|
|
|
Book.where(name: "my book").where("author_id > 3")
|
|
|
|
end
|
|
|
|
|
|
|
|
Book.create(name: "my book", author_id: 4)
|
|
|
|
|
|
|
|
books = cache.execute
|
|
|
|
assert_equal "my book", books[0].name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_statement_cache_with_nil_statement_raises_error
|
|
|
|
assert_raise(ArgumentError) do
|
2013-04-11 08:38:46 -04:00
|
|
|
ActiveRecord::StatementCache.new do
|
2013-04-10 10:40:01 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_statement_cache_with_complex_statement
|
|
|
|
cache = ActiveRecord::StatementCache.new do
|
|
|
|
Liquid.joins(:molecules => :electrons).where('molecules.name' => 'dioxane', 'electrons.name' => 'lepton')
|
|
|
|
end
|
2013-04-10 15:02:26 -04:00
|
|
|
|
2013-04-10 10:40:01 -04:00
|
|
|
salty = Liquid.create(name: 'salty')
|
|
|
|
molecule = salty.molecules.create(name: 'dioxane')
|
2013-04-11 08:38:46 -04:00
|
|
|
molecule.electrons.create(name: 'lepton')
|
2013-04-10 10:40:01 -04:00
|
|
|
|
|
|
|
liquids = cache.execute
|
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
|
|
|
|
cache = ActiveRecord::StatementCache.new do
|
|
|
|
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
|
|
|
|
|
|
|
|
first_books = cache.execute
|
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
|
|
|
|
|
|
|
|
additional_books = cache.execute
|
2013-04-10 15:02:26 -04:00
|
|
|
assert first_books != additional_books
|
2013-04-10 10:40:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|