Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
funny-falcon committed Aug 3, 2010
1 parent d2b7c59 commit 04ca91b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 88 deletions.
21 changes: 0 additions & 21 deletions spec/database.yml

This file was deleted.

10 changes: 0 additions & 10 deletions spec/db/schema.rb

This file was deleted.

98 changes: 88 additions & 10 deletions spec/identity_map_spec.rb
Expand Up @@ -3,35 +3,113 @@
describe "Customers" do

before(:each) do
Thread.current["identity_map"] = Cache.new
ActiveRecord::Base.create_identity_map
end

it "should load the same model twice" do
c1 = Customer.first
c2 = Customer.first
c1.__id__.should == c2.__id__
end

it "should fetch loaded model from cache" do
c1 = Customer.first
Customer.connection.should_not_receive(:select_all)
c2 = Customer.find(c1.id)
end

it "should work for has_many associations" do
p1 = PhoneNumber.first
p2 = Customer.first.phone_numbers.first
ps = Customer.first.phone_numbers
p2 = ps.first
p1.__id__.should == p2.__id__
end

it "should work for belongs_to assocations" do
d1 = Customer.first
d2 = PhoneNumber.first.customer
p1 = PhoneNumber.first
Customer.connection.should_not_receive(:select_all)
d2 = p1.customer
d1.__id__.should == d2.target.__id__
end

it "should work for creating objects" do
c1 = Customer.create(:name => "billy")
c2 = Customer.find_by_name("billy")
c1.__id__.should == c2.__id__
end
describe "creation and deletion:" do
before(:each) do
@billy = Customer.create(:name => "billy")
end

it "should work for creating objects" do
c2 = Customer.find_by_name("billy")
@billy.__id__.should == c2.__id__
Customer.connection.should_not_receive(:select_all)
c3 = Customer.find(@billy.id)
@billy.__id__.should == c3.__id__
end

it "should work for destroyed objects" do
@billy.destroy
c2 = Customer.find_by_id(@billy.id)
c2.should be_nil
end

after(:each) do
@billy.destroy unless @billy.destroyed?
end
end

describe "switching identity map:" do
it "should disable id_map with `without_id_map`" do
c1, c2, c3 = Customer.first, nil, nil
Customer.without_id_map do
c2 = Customer.first
c3 = Customer.first
end
c1.__id__.should_not == c2.__id__
c1.__id__.should_not == c3.__id__
c3.__id__.should_not == c2.__id__
end

it "should use current id_map with `with_id_map(false)`" do
c1, c2, c3 = Customer.first, nil, nil
Customer.with_id_map(false) do
c2 = Customer.first
Customer.connection.should_not_receive(:select_all)
c3 = Customer.find(c2.id)
end
c1.__id__.should == c2.__id__
c1.__id__.should == c3.__id__
c3.__id__.should == c2.__id__
end

it "should create new id_map with `with_id_map`" do
c1, c2, c3 = Customer.first, nil, nil
Customer.with_id_map do
c2 = Customer.first
Customer.connection.should_not_receive(:select_all)
c3 = Customer.find(c2.id)
end
c1.__id__.should_not == c2.__id__
c1.__id__.should_not == c3.__id__
c3.__id__.should == c2.__id__
end

it "should reenable id_map with `with_id_map`" do
c1, c2, c3 = Customer.first, nil, nil
Customer.without_id_map do
Customer.with_id_map do
c2 = Customer.first
Customer.connection.should_not_receive(:select_all)
c3 = Customer.find(c2.id)
end
end
c1.__id__.should_not == c2.__id__
c1.__id__.should_not == c3.__id__
c3.__id__.should == c2.__id__
end
end

after(:each) do
Thread.current["identity_map"] = nil
ActiveRecord::Base.drop_identity_map
end

end
end
72 changes: 38 additions & 34 deletions spec/spec_helper.rb
@@ -1,38 +1,42 @@
begin
require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
rescue LoadError
puts "You need to install rspec in your base app"
exit
end
require 'rubygems'
require 'spec'
require 'active_support'
require 'active_support/test_case'
require 'active_record'
require 'active_record/test_case'
require 'action_controller'
require 'action_view'
require 'identity_map'

#ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Base.establish_connection(
:adapter=>'sqlite3',
:database=>'spec/identity_map.test.sqlite3'
)

plugin_spec_dir = File.dirname(__FILE__)
ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")

def load_schema
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")

db_adapter = ENV['DB']
# no db passed, try one of these fine config-free DBs before bombing.
db_adapter ||=
begin
require 'rubygems'
require 'sqlite'
'sqlite'
rescue MissingSourceFile
begin
require 'sqlite3'
'sqlite3'
rescue MissingSourceFile
end
end

if db_adapter.nil?
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
ActiveRecord::Schema.define(:version => 0) do
puts "Creating Schema"
create_table :customers, :force => true do |t|
t.string :name
end
create_table :phone_numbers, :force => true do |t|
t.string :number
t.integer :customer_id
end
end

ActiveRecord::Base.establish_connection(config[db_adapter])
load(File.dirname(__FILE__) + "/db/schema.rb")
require File.dirname(__FILE__) + '/../init.rb'
class Customer < ActiveRecord::Base
use_id_map
has_many :phone_numbers
end
require File.dirname(__FILE__) + '/test_models.rb'

customer = Customer.create(:name => "Boneman")

class PhoneNumber < ActiveRecord::Base
use_id_map
belongs_to :customer
end

phone_number = customer.phone_numbers.create(:number => "8675309")

13 changes: 0 additions & 13 deletions spec/test_models.rb

This file was deleted.

0 comments on commit 04ca91b

Please sign in to comment.