Skip to content

Commit

Permalink
ActiveRecord setup for specs. Bare minimum associations specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Guidi committed Aug 16, 2009
1 parent 3eabdf1 commit 5ecad87
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
16 changes: 16 additions & 0 deletions spec/active_record/associations_spec.rb
@@ -0,0 +1,16 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe "ActiveRecord::Associations" do
before(:each) do
@cache = ActiveRecord::Base.associations_cache
@post = Factory.create(:post)
@cache.clear rescue nil
end

describe "has_many" do
it "should store results in cache" do
@post.comments
@cache.read("#{@post.cache_key}/comments").should_not be_nil
end
end
end
8 changes: 8 additions & 0 deletions spec/factories.rb
@@ -0,0 +1,8 @@
Factory.define(:post) do |p|
p.title "Welcome to my blog!"
p.comments { |c| [c.association(:comment)] }
end

Factory.define(:comment) do |c|
c.text "Nice post!"
end
3 changes: 3 additions & 0 deletions spec/models/comment.rb
@@ -0,0 +1,3 @@
class Comment < ActiveRecord::Base
belongs_to :post, :cached => true
end
3 changes: 3 additions & 0 deletions spec/models/post.rb
@@ -0,0 +1,3 @@
class Post < ActiveRecord::Base
has_many :comments, :cached => true
end
20 changes: 20 additions & 0 deletions spec/schema.rb
@@ -0,0 +1,20 @@
ActiveRecord::Schema.define do
create_table :posts, :force => true do |t|
t.integer :author_id
t.integer :blog_id
t.string :title
t.text :text
t.datetime :published_at
t.integer :rating, :default => 0

t.timestamps
end

create_table :comments, :force => true do |t|
t.integer :post_id
t.string :email
t.text :text

t.timestamps
end
end
12 changes: 10 additions & 2 deletions spec/spec_helper.rb
@@ -1,9 +1,17 @@
$:.unshift "#{File.dirname(__FILE__)}/../lib"
require "cached-models"
require "factory_girl"
require "memcache"

ActiveRecord::Base.send(:class_variable_set, :@@associations_cache, ActiveSupport::Cache.lookup_store(:mem_cache_store))
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :dbfile => ":memory:"
ActiveRecord::Schema.verbose = false
load(File.dirname(__FILE__) + "/schema.rb")

def cache
ActiveRecord::Base.associations_cache
Dir["#{File.dirname(__FILE__)}/models/**/*.rb"].each { |model| require model }

begin
MemCache.new('localhost').stats
rescue MemCache::MemCacheError
$stderr.puts "[WARNING] Memcache is not running!"
end

0 comments on commit 5ecad87

Please sign in to comment.