aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/test/query_cache_test.rb
blob: d63df6141db29ff8639f3ddf9647a45029439e7a (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                        
                         
 

                                   

                                           

                       
                                                     



                                  
                                                       

       






                                                   
                               
                                                                    

                                                                  

           










                                             


                                               

                                                          

       
  

                                                    
                                                       


       








                                                                                    





                                                                                                         















































                                                                                   
   
require 'abstract_unit'
require 'fixtures/topic'
require 'fixtures/reply'
require 'fixtures/task'
require 'fixtures/course'

require 'active_record/query_cache'

class QueryCacheTest < Test::Unit::TestCase
  fixtures :tasks
  
  def test_find_queries
    assert_queries(2) {  Task.find(1); Task.find(1) }
  end

  def test_find_queries_with_cache
    Task.cache do
      assert_queries(1) {  Task.find(1); Task.find(1) }
    end
  end

  def test_count_queries_with_cache
    Task.cache do
      assert_queries(1) {  Task.count; Task.count }
    end
  end

  def test_query_cache_returned
    assert_not_equal ActiveRecord::QueryCache, Task.connection.class
    Task.cache do
      assert_equal ActiveRecord::QueryCache, Task.connection.class
    end    
  end

  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

  def test_cache_is_scoped_on_actual_class_only
    Task.cache do
      Topic.columns # don't count this query
      assert_queries(2) {  Topic.find(1); Topic.find(1); }
    end
  end
  
  def test_cache_is_scoped_on_all_descending_classes
    ActiveRecord::Base.cache do
      assert_queries(1) {  Task.find(1); Task.find(1) }
    end
  end
  
  def test_cache_does_not_blow_up_other_connections
    assert_not_equal Course.connection.object_id, Task.connection.object_id, 
        "Connections should be different, Course connects to a different database"
    
    ActiveRecord::Base.cache do
      assert_not_equal Course.connection.object_id, Task.connection.object_id, 
          "Connections should be different, Course connects to a different database"
    end
  end

  def test_cache_does_not_wrap_string_results_in_arrays 
    Task.cache do 
      assert_instance_of String, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks") 
    end 
  end 
end

uses_mocha('QueryCacheExpiryTest') do

class QueryCacheExpiryTest < Test::Unit::TestCase
  fixtures :tasks

  def test_find
    ActiveRecord::QueryCache.any_instance.expects(:clear_query_cache).times(0)
    
    Task.cache do 
      Task.find(1)
    end
  end

  def test_save
    ActiveRecord::QueryCache.any_instance.expects(:clear_query_cache).times(1)
    
    Task.cache do 
      Task.find(1).save
    end
  end

  def test_destroy
    ActiveRecord::QueryCache.any_instance.expects(:clear_query_cache).at_least_once
    
    Task.cache do 
      Task.find(1).destroy
    end
  end

  def test_create
    ActiveRecord::QueryCache.any_instance.expects(:clear_query_cache).times(1)
    
    Task.cache do 
      Task.create!
    end
  end

  def test_new_save
    ActiveRecord::QueryCache.any_instance.expects(:clear_query_cache).times(1)
    
    Task.cache do 
      Task.new.save
    end
  end
end

end