public
Fork of defunkt/cache_fu
Description: Everyone's favorite memcached plugin for ActiveRecord.
Homepage: http://errtheblog.com
Clone URL: git://github.com/technoweenie/cache_fu.git
cache_fu / test / config_test.rb
100644 129 lines (107 sloc) 4.655 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require File.join(File.dirname(__FILE__), 'helper')
 
context "The global cache configuration" do
  # Pass in a hash to update the config.
  # If the first arg is a symbol, an expectation will be set.
  def setup_config(*args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    @config[RAILS_ENV].update options.stringify_keys
    ActsAsCached::Config.expects(args.first) if args.first.is_a? Symbol
    ActsAsCached.config = @config
  end
 
  setup do
    ActsAsCached.config.clear
    @config = YAML.load_file('defaults/memcached.yml.default')
    @config['test'] = @config['development'].merge('benchmarking' => false, 'disabled' => false)
  end
 
  specify "should be able to set itself as the session store" do
    setup_config :setup_session_store, :sessions => true
  end
 
  specify "should be able to set itself as the fragment store" do
    setup_config :setup_fragment_store!, :fragments => true
  end
 
  specify "should construct a namespace from the environment and a config value" do
    setup_config
    ActsAsCached.config[:namespace].should.equal "app-#{RAILS_ENV}"
  end
 
  specify "should be able to set a global default ttl" do
    setup_config
    Story.send :acts_as_cached
    ActsAsCached.config[:ttl].should.not.be.nil
    Story.cache_config[:ttl].should.equal ActsAsCached.config[:ttl]
  end
 
  specify "should be able to swallow errors" do
    setup_config :raise_errors => false
    Story.send :acts_as_cached
    Story.stubs(:find).returns(Story.new)
    Story.cache_config[:store].expects(:get).raises(MemCache::MemCacheError)
    Story.cache_config[:store].expects(:set).returns(true)
    proc { Story.get_cache(1) }.should.not.raise(MemCache::MemCacheError)
  end
 
  specify "should not swallow marshal errors" do
    setup_config :raise_errors => false
    Story.send :acts_as_cached
    Story.stubs(:find).returns(Story.new)
    Story.cache_config[:store].expects(:get).returns(nil)
    Story.cache_config[:store].expects(:set).raises(TypeError.new("Some kind of Proc error"))
    proc { Story.get_cache(1) }.should.raise(ActsAsCached::MarshalError)
  end
 
  specify "should be able to re-raise errors" do
    setup_config :raise_errors => true
    Story.send :acts_as_cached
    Story.cache_config[:store].expects(:get).raises(MemCache::MemCacheError)
    proc { Story.get_cache(1) }.should.raise(MemCache::MemCacheError)
  end
 
  specify "should be able to enable benchmarking" do
    setup_config :benchmarking => true
    ActsAsCached.config[:benchmarking].should.equal true
    Story.send :acts_as_cached
    Story.methods.should.include 'fetch_cache_with_benchmarking'
  end
 
  specify "should be able to disable all caching" do
    setup_config :disabled => true
    Story.send :acts_as_cached
    Story.should.respond_to :fetch_cache_with_disabled
    ActsAsCached.config[:disabled].should.equal true
  end
 
  specify "should be able to use a global store other than memcache" do
    setup_config :store => 'HashStore'
    ActsAsCached.config[:store].should.equal HashStore.new
    Story.send :acts_as_cached
    Story.cache_config[:store].should.be ActsAsCached.config[:store]
  end
 
  specify "should be able to override the memcache-client hashing algorithm" do
    setup_config :fast_hash => true
    ActsAsCached.config[:fast_hash].should.equal true
    CACHE.hash_for('eatingsnotcheating').should.equal 1919
  end
 
  specify "should be able to override the memcache-client hashing algorithm" do
    setup_config :fastest_hash => true
    ActsAsCached.config[:fastest_hash].should.equal true
    CACHE.hash_for(string = 'eatingsnotcheating').should.equal string.hash
  end
  
end
 
 
context "The class configuration" do
  # Setups up the Story class with acts_as_cached using options
  def setup_cached(options = {})
    Story.send :acts_as_cached, options
  end
 
  specify "should save unknown keys as options and not config" do
    setup_cached :pengiuns => true
    Story.cache_options.should.include :pengiuns
    Story.cache_config.should.not.include :pengiuns
  end
 
  specify "should be able to override the default finder" do
    setup_cached :finder => :find_by_title
    Story.cache_config[:finder].should.equal :find_by_title
  end
  
  specify "should be able to override the default cache_id" do
    setup_cached :cache_id => :title
    Story.cache_config[:cache_id].should.equal :title
  end
  
  specify "should be able to override the default finder and the cache_id using find_by" do
    setup_cached :find_by => :title
    Story.cache_config.should.not.include :find
    Story.cache_config[:finder].should.equal :find_by_title
    Story.cache_config[:cache_id].should.equal :title
  end
end