2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/topic'
|
|
|
|
require 'models/reply'
|
|
|
|
require 'models/task'
|
|
|
|
require 'models/course'
|
|
|
|
require 'models/category'
|
|
|
|
require 'models/post'
|
2007-02-06 16:16:07 -05:00
|
|
|
|
2007-09-08 00:31:26 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class QueryCacheTest < ActiveRecord::TestCase
|
2008-01-17 20:55:11 -05:00
|
|
|
fixtures :tasks, :topics, :categories, :posts, :categories_posts
|
2007-09-17 02:15:58 -04:00
|
|
|
|
2010-03-22 12:53:07 -04:00
|
|
|
def setup
|
|
|
|
Task.connection.clear_query_cache
|
|
|
|
end
|
|
|
|
|
2007-02-06 16:16:07 -05:00
|
|
|
def test_find_queries
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(2) { Task.find(1); Task.find(1) }
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_queries_with_cache
|
|
|
|
Task.cache do
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(1) { Task.find(1); Task.find(1) }
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
end
|
2007-09-02 19:52:58 -04:00
|
|
|
|
|
|
|
def test_count_queries_with_cache
|
|
|
|
Task.cache do
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(1) { Task.count; Task.count }
|
2007-09-02 19:52:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-07-24 23:48:30 -04:00
|
|
|
def test_query_cache_dups_results_correctly
|
|
|
|
Task.cache do
|
|
|
|
now = Time.now.utc
|
|
|
|
task = Task.find 1
|
|
|
|
assert_not_equal now, task.starting
|
|
|
|
task.starting = now
|
|
|
|
task.reload
|
|
|
|
assert_not_equal now, task.starting
|
|
|
|
end
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
|
2007-09-17 02:15:58 -04:00
|
|
|
def test_cache_is_flat
|
2007-02-06 16:16:07 -05:00
|
|
|
Task.cache do
|
2007-07-24 23:48:30 -04:00
|
|
|
Topic.columns # don't count this query
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(1) { Topic.find(1); Topic.find(1); }
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
2007-09-17 02:15:58 -04:00
|
|
|
|
2007-02-20 18:42:04 -05:00
|
|
|
ActiveRecord::Base.cache do
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(1) { Task.find(1); Task.find(1) }
|
2007-02-20 18:42:04 -05:00
|
|
|
end
|
|
|
|
end
|
2007-09-17 02:15:58 -04:00
|
|
|
|
|
|
|
def test_cache_does_not_wrap_string_results_in_arrays
|
2010-01-26 16:00:19 -05:00
|
|
|
require 'sqlite3/version' if current_adapter?(:SQLite3Adapter)
|
|
|
|
|
2007-09-17 02:15:58 -04:00
|
|
|
Task.cache do
|
2009-03-22 18:19:27 -04:00
|
|
|
# Oracle adapter returns count() as Fixnum or Float
|
|
|
|
if current_adapter?(:OracleAdapter)
|
|
|
|
assert Task.connection.select_value("SELECT count(*) AS count_all FROM tasks").is_a?(Numeric)
|
2010-01-26 16:00:19 -05:00
|
|
|
elsif current_adapter?(:SQLite3Adapter) && SQLite3::Version::VERSION > '1.2.5'
|
|
|
|
# Future versions of the sqlite3 adapter will return numeric
|
|
|
|
assert_instance_of Fixnum,
|
|
|
|
Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
|
2009-03-22 18:19:27 -04:00
|
|
|
else
|
|
|
|
assert_instance_of String, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
|
|
|
|
end
|
2007-02-21 16:54:41 -05:00
|
|
|
end
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class QueryCacheExpiryTest < ActiveRecord::TestCase
|
2008-07-31 16:59:53 -04:00
|
|
|
fixtures :tasks, :posts, :categories, :categories_posts
|
2007-02-06 16:16:07 -05:00
|
|
|
|
|
|
|
def test_find
|
2007-09-17 02:15:58 -04:00
|
|
|
Task.connection.expects(:clear_query_cache).times(1)
|
|
|
|
|
|
|
|
assert !Task.connection.query_cache_enabled
|
|
|
|
Task.cache do
|
|
|
|
assert Task.connection.query_cache_enabled
|
2007-02-06 16:16:07 -05:00
|
|
|
Task.find(1)
|
2007-09-17 02:15:58 -04:00
|
|
|
|
|
|
|
Task.uncached do
|
|
|
|
assert !Task.connection.query_cache_enabled
|
|
|
|
Task.find(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert Task.connection.query_cache_enabled
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
2007-09-17 02:15:58 -04:00
|
|
|
assert !Task.connection.query_cache_enabled
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
|
2007-09-17 02:15:58 -04:00
|
|
|
def test_update
|
|
|
|
Task.connection.expects(:clear_query_cache).times(2)
|
|
|
|
|
|
|
|
Task.cache do
|
2008-03-30 21:10:04 -04:00
|
|
|
task = Task.find(1)
|
|
|
|
task.starting = Time.now.utc
|
|
|
|
task.save!
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy
|
2007-09-17 02:15:58 -04:00
|
|
|
Task.connection.expects(:clear_query_cache).times(2)
|
|
|
|
|
|
|
|
Task.cache do
|
2007-02-06 16:16:07 -05:00
|
|
|
Task.find(1).destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-17 02:15:58 -04:00
|
|
|
def test_insert
|
|
|
|
ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
|
2007-02-06 16:16:07 -05:00
|
|
|
|
2007-09-17 02:15:58 -04:00
|
|
|
Task.cache do
|
|
|
|
Task.create!
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
end
|
2008-01-17 20:55:11 -05:00
|
|
|
|
|
|
|
def test_cache_is_expired_by_habtm_update
|
|
|
|
ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
|
|
|
|
ActiveRecord::Base.cache do
|
|
|
|
c = Category.find(:first)
|
|
|
|
p = Post.find(:first)
|
|
|
|
p.categories << c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_is_expired_by_habtm_delete
|
|
|
|
ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
|
|
|
|
ActiveRecord::Base.cache do
|
2008-08-26 05:17:36 -04:00
|
|
|
c = Category.find(1)
|
|
|
|
p = Post.find(1)
|
|
|
|
assert p.categories.any?
|
2008-01-17 20:55:11 -05:00
|
|
|
p.categories.delete_all
|
|
|
|
end
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|