Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ActiveRecord setup for specs. Bare minimum associations specs.
- Loading branch information
Luca Guidi
committed
Aug 16, 2009
1 parent
3eabdf1
commit 5ecad87
Showing
6 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Comment < ActiveRecord::Base | ||
belongs_to :post, :cached => true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Post < ActiveRecord::Base | ||
has_many :comments, :cached => true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |