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
defunkt (author)
Tue Jan 22 16:27:07 -0800 2008
cache_fu / test / helper.rb
100644 216 lines (173 sloc) 4.795 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
##
# This file exists to fake out all the Railsisms we use so we can run the
# tests in isolation.
$LOAD_PATH.unshift 'lib/'
#
 
begin
  require 'rubygems'
  gem 'mocha', '>= 0.4.0'
  require 'mocha'
  gem 'test-spec', '= 0.3.0'
  require 'test/spec'
  require 'multi_rails_init'
rescue LoadError
  puts '=> acts_as_cached tests depend on the following gems: mocha (0.4.0+), test-spec (0.3.0), multi_rails (0.0.2), and rails.'
end
 
begin
  require 'redgreen'
rescue LoadError
  nil
end
 
Test::Spec::Should.send :alias_method, :have, :be
Test::Spec::ShouldNot.send :alias_method, :have, :be
 
##
# real men test without mocks
if $with_memcache = ARGV.include?('with-memcache')
  require 'memcache'
end
 
##
# init.rb hacks
RAILS_ROOT = '.' unless defined? RAILS_ROOT
RAILS_ENV = 'test' unless defined? RAILS_ENV
 
##
# get the default config using absolute path, so tests all play nice when run in isolation
DEFAULT_CONFIG_FILE = File.expand_path(File.dirname(__FILE__) + '/../defaults/memcached.yml.default')
 
##
# aac
require 'acts_as_cached'
Object.send :include, ActsAsCached::Mixin
 
##
# i need you.
module Enumerable
  def index_by
    inject({}) do |accum, elem|
      accum[yield(elem)] = elem
      accum
    end
  end
end
 
##
# mocky.
class HashStore < Hash
  alias :get :[]
 
  def get_multi(*values)
    reject { |k,v| !values.include? k }
  end
 
  def set(key, value, *others)
    self[key] = value
  end
  
  def namespace
    nil
  end
end
 
$cache = HashStore.new
 
class Story
  acts_as_cached($with_memcache ? {} : { :store => $cache })
 
  attr_accessor :id, :title
 
  def initialize(attributes = {})
    attributes.each { |key, value| instance_variable_set("@#{key}", value) }
  end
 
  def attributes
    { :id => id, :title => title }
  end
 
  def ==(other)
    return false unless other.respond_to? :attributes
    attributes == other.attributes
  end
 
  def self.find(*args)
    options = args.last.is_a?(Hash) ? args.pop : {}
 
    if (ids = args.flatten).size > 1
      ids.map { |id| $stories[id.to_i] }
    elsif (id = args.flatten.first).to_i.to_s == id.to_s
      $stories[id.to_i]
    end
  end
  
  def self.find_by_title(*args)
    title = args.shift
    find(args).select { |s| s.title == title }
  end
 
  def self.base_class
    Story
  end
 
  def self.something_cool; :redbull end
  def something_flashy; :molassy end
 
  def self.block_on(target = nil)
    target || :something
  end
 
  def self.pass_through(target)
    target
  end
 
  def self.two_params(first, second)
    "first: #{first} | second: #{second}"
  end
 
  def self.find_live(*args) false end
end
 
class Feature < Story; end
class Interview < Story; end
 
module ActionController
  class Base
    def rendering_runtime(*args) '' end
    def self.silence; yield end
  end
end
 
class MemCache
  attr_accessor :servers
  def initialize(*args) end
  class MemCacheError < StandardError; end unless defined? MemCacheError
end unless $with_memcache
 
module StoryCacheSpecSetup
  def self.included(base)
    base.setup do
      setup_cache_spec
      Story.instance_eval { @max_key_length = nil }
    end
  end
 
  def setup_cache_spec
    @story = Story.new(:id => 1, :title => "acts_as_cached 2 released!")
    @story2 = Story.new(:id => 2, :title => "BDD is something you can use")
    @story3 = Story.new(:id => 3, :title => "RailsConf is overrated.")
    $stories = { 1 => @story, 2 => @story2, 3 => @story3 }
 
    $with_memcache ? with_memcache : with_mock
  end
 
  def with_memcache
    unless $mc_setup_for_story_cache_spec
      ActsAsCached.config.clear
      config = YAML.load_file(DEFAULT_CONFIG_FILE)
      config['test'] = config['development'].merge('benchmarking' => false, 'disabled' => false)
      ActsAsCached.config = config
      $mc_setup_for_story_cache_spec = true
    end
 
    Story.send :acts_as_cached
    Story.expire_cache(1)
    Story.expire_cache(2)
    Story.expire_cache(3)
    Story.expire_cache(:block)
    Story.set_cache(2, @story2)
  end
 
  def with_mock
    $cache.clear
 
    Story.send :acts_as_cached, :store => $cache
    $cache['Story:2'] = @story2
  end
end
 
module FragmentCacheSpecSetup
  def self.included(base)
    base.setup { setup_fragment_spec }
  end
  
  def setup_fragment_spec
    unless $mc_setup_for_fragment_cache_spec
      ActsAsCached.config.clear
      config = YAML.load_file(DEFAULT_CONFIG_FILE)
 
      if $with_memcache
        other_options = { 'fragments' => true }
      else
        Object.const_set(:CACHE, $cache) unless defined? CACHE
        other_options = { 'fragments' => true, 'store' => $cache }
      end
 
      config['test'] = config['development'].merge other_options
 
      ActsAsCached.config = config
      ActsAsCached::FragmentCache.setup!
      $mc_setup_for_fragment_cache_spec = true
    end
  end
end