Permalink
Browse files

refactor tests

  • Loading branch information...
1 parent c33a251 commit 27aab6db441e469c492c7fe72682bfbc342f06ec @flyerhzm committed Jun 6, 2011
@@ -69,7 +69,7 @@ def load_target
origin_load_target
end
end
-
+
::ActiveRecord::Associations::AssociationProxy.class_eval do
# call has_one and belong_to association
alias_method :origin_load_target, :load_target
@@ -15,7 +15,7 @@ def self.check_unused_preload_associations
create_notification object.class, object_association_diff
end
end
-
+
protected
def self.create_notification(klazz, associations)
notice = Bullet::Notification::UnusedEagerLoading.new( klazz, associations )
@@ -1,96 +0,0 @@
-require File.dirname(__FILE__) + '/../spec_helper'
-
-ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
-# This test is just used for http://github.com/flyerhzm/bullet/issues/#issue/14
-describe Bullet::Detector::Association do
-
- describe "for chris" do
- def setup_db
- ActiveRecord::Schema.define(:version => 1) do
- create_table :locations do |t|
- t.column :name, :string
- end
-
- create_table :hotels do |t|
- t.column :name, :string
- t.column :location_id, :integer
- end
-
- create_table :deals do |t|
- t.column :name, :string
- t.column :hotel_id, :integer
- end
- end
- end
-
- def teardown_db
- ActiveRecord::Base.connection.tables.each do |table|
- ActiveRecord::Base.connection.drop_table(table)
- end
- end
-
- class Location < ActiveRecord::Base
- has_many :hotels
- end
-
- class Hotel < ActiveRecord::Base
- belongs_to :location
- has_many :deals
- end
-
- class Deal < ActiveRecord::Base
- belongs_to :hotel
- has_one :location, :through => :hotel
- end
-
- before(:all) do
- setup_db
-
- location1 = Location.create(:name => "location1")
- location2 = Location.create(:name => "location2")
-
- hotel1 = location1.hotels.create(:name => "hotel1")
- hotel2 = location1.hotels.create(:name => "hotel2")
- hotel3 = location2.hotels.create(:name => "hotel3")
- hotel4 = location2.hotels.create(:name => "hotel4")
-
- deal1 = hotel1.deals.create(:name => "deal1")
- deal2 = hotel2.deals.create(:name => "deal2")
- deal3 = hotel3.deals.create(:name => "deal3")
- deal4 = hotel4.deals.create(:name => "deal4")
- end
-
- after(:all) do
- teardown_db
- end
-
- before(:each) do
- Bullet.start_request
- end
-
- after(:each) do
- Bullet.end_request
- end
-
- it "should detect unpreload association from deal to hotel" do
- Deal.all.each do |deal|
- deal.hotel.location.name
- end
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Deal, :hotel)
- end
-
- it "should detect unpreload association from hotel to location" do
- Deal.includes(:hotel).each do |deal|
- deal.hotel.location.name
- end
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Hotel, :location)
- end
-
- it "should not detect unpreload association" do
- Deal.includes({:hotel => :location}).each do |deal|
- deal.hotel.location.name
- end
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
- end
- end
-end
@@ -1,86 +0,0 @@
-require File.dirname(__FILE__) + '/../spec_helper'
-
-ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
-# This test is just used for http://github.com/flyerhzm/bullet/issues#issue/20
-describe Bullet::Detector::Association do
-
- describe "for peschkaj" do
- def setup_db
- ActiveRecord::Schema.define(:version => 1) do
- create_table :categories do |t|
- t.column :name, :string
- end
-
- create_table :submissions do |t|
- t.column :name, :string
- t.column :category_id, :integer
- t.column :user_id, :integer
- end
-
- create_table :users do |t|
- t.column :name, :string
- t.column :category_id, :integer
- end
- end
- end
-
- def teardown_db
- ActiveRecord::Base.connection.tables.each do |table|
- ActiveRecord::Base.connection.drop_table(table)
- end
- end
-
- class Category < ActiveRecord::Base
- has_many :submissions
- has_many :users
- end
-
- class Submission < ActiveRecord::Base
- belongs_to :category
- belongs_to :user
- end
-
- class User < ActiveRecord::Base
- has_one :submission
- belongs_to :category
- end
-
- before(:all) do
- setup_db
-
- category1 = Category.create(:name => "category1")
- category2 = Category.create(:name => "category2")
-
- user1 = User.create(:name => 'user1', :category => category1)
- user2 = User.create(:name => 'user2', :category => category1)
-
- submission1 = category1.submissions.create(:name => "submission1", :user => user1)
- submission2 = category1.submissions.create(:name => "submission2", :user => user2)
- submission3 = category2.submissions.create(:name => "submission3", :user => user1)
- submission4 = category2.submissions.create(:name => "submission4", :user => user2)
- end
-
- after(:all) do
- teardown_db
- end
-
- before(:each) do
- Bullet.start_request
- end
-
- after(:each) do
- Bullet.end_request
- end
-
- it "should not detect unused preload associations" do
- category = Category.includes({:submissions => :user}).order("id DESC").find_by_name('category1')
- category.submissions.map do |submission|
- submission.name
- submission.user.name
- end
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
- Bullet::Detector::Association.should_not be_unused_preload_associations_for(Category, :submissions)
- Bullet::Detector::Association.should_not be_unused_preload_associations_for(Submission, :user)
- end
- end
-end
Oops, something went wrong.

0 comments on commit 27aab6d

Please sign in to comment.