bullet 2.3.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/.travis.yml +5 -0
  2. data/Gemfile +3 -1
  3. data/Gemfile.lock +43 -32
  4. data/Gemfile.rails-2.3.14 +16 -0
  5. data/Gemfile.rails-2.3.14.lock +65 -0
  6. data/Gemfile.rails-3.0.12 +17 -0
  7. data/Gemfile.rails-3.0.12.lock +116 -0
  8. data/Gemfile.rails-3.1.4 +18 -0
  9. data/Gemfile.rails-3.1.4.lock +134 -0
  10. data/README.textile +5 -5
  11. data/lib/bullet.rb +15 -9
  12. data/lib/bullet/active_record2.rb +12 -0
  13. data/lib/bullet/active_record3.rb +12 -0
  14. data/lib/bullet/detector/association.rb +11 -12
  15. data/lib/bullet/detector/counter.rb +6 -6
  16. data/lib/bullet/detector/n_plus_one_query.rb +9 -9
  17. data/lib/bullet/detector/unused_eager_association.rb +9 -9
  18. data/lib/bullet/ext/object.rb +5 -0
  19. data/lib/bullet/ext/string.rb +5 -0
  20. data/lib/bullet/mongoid.rb +56 -0
  21. data/lib/bullet/registry/object.rb +4 -6
  22. data/lib/bullet/version.rb +1 -1
  23. data/spec/bullet/detector/association_spec.rb +14 -14
  24. data/spec/bullet/detector/counter_spec.rb +8 -9
  25. data/spec/bullet/detector/n_plus_one_query_spec.rb +22 -22
  26. data/spec/bullet/detector/unused_eager_association_spec.rb +7 -7
  27. data/spec/bullet/ext/object_spec.rb +20 -0
  28. data/spec/bullet/ext/string_spec.rb +13 -0
  29. data/spec/bullet/registry/object_spec.rb +4 -4
  30. data/spec/integration/association_spec.rb +463 -401
  31. data/spec/integration/counter_spec.rb +27 -25
  32. data/spec/integration/mongoid/association_spec.rb +276 -0
  33. data/spec/integration/rails2/association_spec.rb +593 -0
  34. data/spec/integration/rails2/counter_spec.rb +39 -0
  35. data/spec/models/mongoid/address.rb +5 -0
  36. data/spec/models/mongoid/category.rb +6 -0
  37. data/spec/models/mongoid/comment.rb +5 -0
  38. data/spec/models/mongoid/company.rb +5 -0
  39. data/spec/models/mongoid/entry.rb +5 -0
  40. data/spec/models/mongoid/post.rb +8 -0
  41. data/spec/models/post.rb +12 -6
  42. data/spec/spec_helper.rb +32 -8
  43. data/spec/support/bullet_ext.rb +1 -1
  44. data/spec/support/mongo_seed.rb +41 -0
  45. data/spec/support/{seed.rb → sqlite_seed.rb} +1 -22
  46. data/test.result +2293 -0
  47. metadata +42 -22
  48. data/spec/integration/association_for_chris_spec.rb +0 -37
  49. data/spec/integration/association_for_peschkaj_spec.rb +0 -26
  50. data/spec/models/contact.rb +0 -3
  51. data/spec/models/deal.rb +0 -4
  52. data/spec/models/email.rb +0 -3
  53. data/spec/models/hotel.rb +0 -4
  54. data/spec/models/location.rb +0 -3
@@ -11,7 +11,6 @@ module Bullet
11
11
 
12
12
  context ".clear" do
13
13
  it "should clear all class variables" do
14
- Counter.clear
15
14
  Counter.class_variable_get(:@@possible_objects).should be_nil
16
15
  Counter.class_variable_get(:@@impossible_objects).should be_nil
17
16
  end
@@ -19,13 +18,13 @@ module Bullet
19
18
 
20
19
  context ".add_counter_cache" do
21
20
  it "should create notification if conditions met" do
22
- Counter.should_receive(:conditions_met?).with(@post1.ar_key, [:comments]).and_return(true)
21
+ Counter.should_receive(:conditions_met?).with(@post1.bullet_ar_key, [:comments]).and_return(true)
23
22
  Counter.should_receive(:create_notification).with("Post", [:comments])
24
23
  Counter.add_counter_cache(@post1, [:comments])
25
24
  end
26
25
 
27
26
  it "should not create notification if conditions not met" do
28
- Counter.should_receive(:conditions_met?).with(@post1.ar_key, [:comments]).and_return(false)
27
+ Counter.should_receive(:conditions_met?).with(@post1.bullet_ar_key, [:comments]).and_return(false)
29
28
  Counter.should_receive(:create_notification).never
30
29
  Counter.add_counter_cache(@post1, [:comments])
31
30
  end
@@ -34,30 +33,30 @@ module Bullet
34
33
  context ".add_possible_objects" do
35
34
  it "should add possible objects" do
36
35
  Counter.add_possible_objects([@post1, @post2])
37
- Counter.send(:possible_objects).should be_include(@post1.ar_key)
38
- Counter.send(:possible_objects).should be_include(@post2.ar_key)
36
+ Counter.send(:possible_objects).should be_include(@post1.bullet_ar_key)
37
+ Counter.send(:possible_objects).should be_include(@post2.bullet_ar_key)
39
38
  end
40
39
 
41
40
  it "should add impossible object" do
42
41
  Counter.add_impossible_object(@post1)
43
- Counter.send(:impossible_objects).should be_include(@post1.ar_key)
42
+ Counter.send(:impossible_objects).should be_include(@post1.bullet_ar_key)
44
43
  end
45
44
  end
46
45
 
47
46
  context ".conditions_met?" do
48
47
  it "should be true when object is possible, not impossible" do
49
48
  Counter.add_possible_objects(@post1)
50
- Counter.send(:conditions_met?, @post1.ar_key, :associations).should be_true
49
+ Counter.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_true
51
50
  end
52
51
 
53
52
  it "should be false when object is not possible" do
54
- Counter.send(:conditions_met?, @post1.ar_key, :associations).should be_false
53
+ Counter.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_false
55
54
  end
56
55
 
57
56
  it "should be true when object is possible, and impossible" do
58
57
  Counter.add_possible_objects(@post1)
59
58
  Counter.add_impossible_object(@post1)
60
- Counter.send(:conditions_met?, @post1.ar_key, :associations).should be_false
59
+ Counter.send(:conditions_met?, @post1.bullet_ar_key, :associations).should be_false
61
60
  end
62
61
  end
63
62
  end
@@ -21,69 +21,69 @@ module Bullet
21
21
  context ".possible?" do
22
22
  it "should be true if possible_objects contain" do
23
23
  NPlusOneQuery.add_possible_objects(@post)
24
- NPlusOneQuery.send(:possible?, @post.ar_key).should be_true
24
+ NPlusOneQuery.send(:possible?, @post.bullet_ar_key).should be_true
25
25
  end
26
26
  end
27
27
 
28
28
  context ".impossible?" do
29
29
  it "should be true if impossible_objects contain" do
30
30
  NPlusOneQuery.add_impossible_object(@post)
31
- NPlusOneQuery.send(:impossible?, @post.ar_key).should be_true
31
+ NPlusOneQuery.send(:impossible?, @post.bullet_ar_key).should be_true
32
32
  end
33
33
  end
34
34
 
35
35
  context ".association?" do
36
36
  it "should be true if object, associations pair is already existed" do
37
37
  NPlusOneQuery.add_object_associations(@post, :association)
38
- NPlusOneQuery.send(:association?, @post.ar_key, :association).should be_true
38
+ NPlusOneQuery.send(:association?, @post.bullet_ar_key, :association).should be_true
39
39
  end
40
40
 
41
41
  it "should be false if object, association pair is not existed" do
42
42
  NPlusOneQuery.add_object_associations(@post, :association1)
43
- NPlusOneQuery.send(:association?, @post.ar_key, :associatio2).should be_false
43
+ NPlusOneQuery.send(:association?, @post.bullet_ar_key, :associatio2).should be_false
44
44
  end
45
45
  end
46
46
 
47
47
  context ".conditions_met?" do
48
48
  it "should be true if object is possible, not impossible and object, associations pair is not already existed" do
49
- NPlusOneQuery.stub(:possible?).with(@post.ar_key).and_return(true)
50
- NPlusOneQuery.stub(:impossible?).with(@post.ar_key).and_return(false)
51
- NPlusOneQuery.stub(:association?).with(@post.ar_key, :associations).and_return(false)
52
- NPlusOneQuery.send(:conditions_met?, @post.ar_key, :associations).should be_true
49
+ NPlusOneQuery.stub(:possible?).with(@post.bullet_ar_key).and_return(true)
50
+ NPlusOneQuery.stub(:impossible?).with(@post.bullet_ar_key).and_return(false)
51
+ NPlusOneQuery.stub(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
52
+ NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).should be_true
53
53
  end
54
54
 
55
55
  it "should be false if object is not possible, not impossible and object, associations pair is not already existed" do
56
- NPlusOneQuery.stub(:possible?).with(@post.ar_key).and_return(false)
57
- NPlusOneQuery.stub(:impossible?).with(@post.ar_key).and_return(false)
58
- NPlusOneQuery.stub(:association?).with(@post.ar_key, :associations).and_return(false)
59
- NPlusOneQuery.send(:conditions_met?, @post.ar_key, :associations).should be_false
56
+ NPlusOneQuery.stub(:possible?).with(@post.bullet_ar_key).and_return(false)
57
+ NPlusOneQuery.stub(:impossible?).with(@post.bullet_ar_key).and_return(false)
58
+ NPlusOneQuery.stub(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
59
+ NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).should be_false
60
60
  end
61
61
 
62
62
  it "should be false if object is possible, but impossible and object, associations pair is not already existed" do
63
- NPlusOneQuery.stub(:possible?).with(@post.ar_key).and_return(true)
64
- NPlusOneQuery.stub(:impossible?).with(@post.ar_key).and_return(true)
65
- NPlusOneQuery.stub(:association?).with(@post.ar_key, :associations).and_return(false)
66
- NPlusOneQuery.send(:conditions_met?, @post.ar_key, :associations).should be_false
63
+ NPlusOneQuery.stub(:possible?).with(@post.bullet_ar_key).and_return(true)
64
+ NPlusOneQuery.stub(:impossible?).with(@post.bullet_ar_key).and_return(true)
65
+ NPlusOneQuery.stub(:association?).with(@post.bullet_ar_key, :associations).and_return(false)
66
+ NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).should be_false
67
67
  end
68
68
 
69
69
  it "should be false if object is possible, not impossible and object, associations pair is already existed" do
70
- NPlusOneQuery.stub(:possible?).with(@post.ar_key).and_return(true)
71
- NPlusOneQuery.stub(:impossible?).with(@post.ar_key).and_return(false)
72
- NPlusOneQuery.stub(:association?).with(@post.ar_key, :associations).and_return(true)
73
- NPlusOneQuery.send(:conditions_met?, @post.ar_key, :associations).should be_false
70
+ NPlusOneQuery.stub(:possible?).with(@post.bullet_ar_key).and_return(true)
71
+ NPlusOneQuery.stub(:impossible?).with(@post.bullet_ar_key).and_return(false)
72
+ NPlusOneQuery.stub(:association?).with(@post.bullet_ar_key, :associations).and_return(true)
73
+ NPlusOneQuery.send(:conditions_met?, @post.bullet_ar_key, :associations).should be_false
74
74
  end
75
75
  end
76
76
 
77
77
  context ".call_association" do
78
78
  it "should create notification if conditions met" do
79
- NPlusOneQuery.should_receive(:conditions_met?).with(@post.ar_key, :association).and_return(true)
79
+ NPlusOneQuery.should_receive(:conditions_met?).with(@post.bullet_ar_key, :association).and_return(true)
80
80
  NPlusOneQuery.should_receive(:caller_in_project).and_return(["caller"])
81
81
  NPlusOneQuery.should_receive(:create_notification).with(["caller"], "Post", :association)
82
82
  NPlusOneQuery.call_association(@post, :association)
83
83
  end
84
84
 
85
85
  it "should not create notification if conditions not met" do
86
- NPlusOneQuery.should_receive(:conditions_met?).with(@post.ar_key, :association).and_return(false)
86
+ NPlusOneQuery.should_receive(:conditions_met?).with(@post.bullet_ar_key, :association).and_return(false)
87
87
  NPlusOneQuery.should_not_receive(:caller_in_project!)
88
88
  NPlusOneQuery.should_not_receive(:create_notification).with("Post", :association)
89
89
  NPlusOneQuery.call_association(@post, :association)
@@ -8,30 +8,30 @@ module Bullet
8
8
 
9
9
  context ".call_associations" do
10
10
  it "should get empty array if eager_loadgins" do
11
- UnusedEagerAssociation.send(:call_associations, @post.ar_key, Set.new([:association])).should be_empty
11
+ UnusedEagerAssociation.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
12
12
  end
13
13
 
14
14
  it "should get call associations if object and association are both in eager_loadings and call_object_associations" do
15
15
  UnusedEagerAssociation.add_eager_loadings([@post], :association)
16
16
  UnusedEagerAssociation.add_call_object_associations(@post, :association)
17
- UnusedEagerAssociation.send(:call_associations, @post.ar_key, Set.new([:association])).should == [:association]
17
+ UnusedEagerAssociation.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should == [:association]
18
18
  end
19
19
 
20
20
  it "should not get call associations if not exist in call_object_associations" do
21
21
  UnusedEagerAssociation.add_eager_loadings([@post], :association)
22
- UnusedEagerAssociation.send(:call_associations, @post.ar_key, Set.new([:association])).should be_empty
22
+ UnusedEagerAssociation.send(:call_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
23
23
  end
24
24
  end
25
25
 
26
- context ".diff_object_association" do
26
+ context ".diff_object_associations" do
27
27
  it "should return associations not exist in call_association" do
28
- UnusedEagerAssociation.send(:diff_object_association, @post.ar_key, Set.new([:association])).should == [:association]
28
+ UnusedEagerAssociation.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should == [:association]
29
29
  end
30
30
 
31
31
  it "should return empty if associations exist in call_association" do
32
32
  UnusedEagerAssociation.add_eager_loadings([@post], :association)
33
33
  UnusedEagerAssociation.add_call_object_associations(@post, :association)
34
- UnusedEagerAssociation.send(:diff_object_association, @post.ar_key, Set.new([:association])).should be_empty
34
+ UnusedEagerAssociation.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
35
35
  end
36
36
  end
37
37
 
@@ -52,7 +52,7 @@ module Bullet
52
52
  UnusedEagerAssociation.add_object_associations(@post, :association)
53
53
  UnusedEagerAssociation.add_eager_loadings([@post], :association)
54
54
  UnusedEagerAssociation.add_call_object_associations(@post, :association)
55
- UnusedEagerAssociation.send(:diff_object_association, @post.ar_key, Set.new([:association])).should be_empty
55
+ UnusedEagerAssociation.send(:diff_object_associations, @post.bullet_ar_key, Set.new([:association])).should be_empty
56
56
  UnusedEagerAssociation.should_not_receive(:create_notification).with("Post", [:association])
57
57
  UnusedEagerAssociation.check_unused_preload_associations
58
58
  end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Object do
4
+ context "bullet_ar_key" do
5
+ it "should return class and id composition" do
6
+ post = Post.first
7
+ post.bullet_ar_key.should == "Post:#{post.id}"
8
+ end
9
+
10
+ begin
11
+ require 'mongoid'
12
+
13
+ it "should return class with namesapce and id composition" do
14
+ post = Mongoid::Post.first
15
+ post.bullet_ar_key.should == "Mongoid::Post:#{post.id}"
16
+ end
17
+ rescue LoadError
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+ context "bullet_class_name" do
5
+ it "should only return class name" do
6
+ "Post:1".bullet_class_name.should == "Post"
7
+ end
8
+
9
+ it "should return class name with namespace" do
10
+ "Mongoid::Post:1234567890".bullet_class_name.should == "Mongoid::Post"
11
+ end
12
+ end
13
+ end
@@ -6,18 +6,18 @@ module Bullet
6
6
  describe Object do
7
7
  let(:post) { Post.first }
8
8
  let(:another_post) { Post.last }
9
- subject { Object.new.tap { |object| object.add(post.ar_key) } }
9
+ subject { Object.new.tap { |object| object.add(post.bullet_ar_key) } }
10
10
 
11
11
  context "#include?" do
12
12
  it "should include the object" do
13
- subject.should be_include(post.ar_key)
13
+ subject.should be_include(post.bullet_ar_key)
14
14
  end
15
15
  end
16
16
 
17
17
  context "#add" do
18
18
  it "should add an object" do
19
- subject.add(another_post.ar_key)
20
- subject.should be_include(another_post.ar_key)
19
+ subject.add(another_post.bullet_ar_key)
20
+ subject.should be_include(another_post.bullet_ar_key)
21
21
  end
22
22
  end
23
23
  end
@@ -1,531 +1,593 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Bullet::Detector::Association, 'has_many' do
4
- before(:each) do
5
- Bullet.start_request
6
- end
7
-
8
- after(:each) do
9
- Bullet.end_request
10
- end
11
-
12
- context "for unused cases" do
13
- #If you have the same record created twice with different includes
14
- # the hash value get's accumulated includes, which leads to false Unused eager loading
15
- #it "should not incorrectly mark associations as unused when multiple object instances" do
16
- #comments_with_author = Comment.includes(:author)
17
- #comments_with_post = Comment.includes(:post)
18
- #comments_with_author.each { |c| c.author.name }
19
- #comments_with_author.each { |c| c.post.name }
20
- #Bullet::Association.check_unused_preload_associations
21
- #Bullet::Association.should be_unused_preload_associations_for(Comment, :post)
22
- #Bullet::Association.should be_detecting_unpreloaded_association_for(Comment, :post)
23
- #end
24
-
25
- # same as above with different Models being queried
26
- #it "should not incorrectly mark associations as unused when multiple object instances different Model" do
27
- #post_with_comments = Post.includes(:comments)
28
- #comments_with_author = Comment.includes(:author)
29
- #post_with_comments.each { |p| p.comments.first.author.name }
30
- #comments_with_author.each { |c| c.name }
31
- #Bullet::Association.check_unused_preload_associations
32
- #Bullet::Association.should be_unused_preload_associations_for(Comment, :author)
33
- #Bullet::Association.should be_detecting_unpreloaded_association_for(Comment, :author)
34
- #end
35
-
36
- # this test passes right now. But is a regression test to ensure that if only a small set of returned records
37
- # is not used that a unused preload association error is not generated
38
- it "should not have unused when small set of returned records are discarded" do
39
- comments_with_author = Comment.includes(:author)
40
- comment_collection = comments_with_author.limit(2)
41
- comment_collection.collect { |com| com.author.name }
42
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
43
- Bullet::Detector::Association.should_not be_unused_preload_associations_for(Comment, :author)
3
+ if Rails.version.to_i > 2
4
+ describe Bullet::Detector::Association, 'has_many' do
5
+ before(:each) do
6
+ Bullet.clear
7
+ Bullet.start_request
44
8
  end
45
- end
46
9
 
10
+ after(:each) do
11
+ Bullet.end_request
12
+ end
47
13
 
48
- context "comments => posts => category" do
14
+ context "post => comments" do
15
+ it "should detect non preload post => comments" do
16
+ Post.all.each do |post|
17
+ post.comments.map(&:name)
18
+ end
19
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
20
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
49
21
 
50
- # this happens because the post isn't a possible object even though the writer is access through the post
51
- # which leads to an 1+N queries
52
- it "should detect unpreloaded writer" do
53
- Comment.includes([:author, :post]).where(["base_users.id = ?", BaseUser.first]).each do |com|
54
- com.post.writer.name
22
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :comments)
55
23
  end
56
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :writer)
57
- end
58
24
 
59
- # this happens because the comment doesn't break down the hash into keys
60
- # properly creating an association from comment to post
61
- it "should detect preload of comment => post" do
62
- comments = Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |com|
63
- com.post.writer.name
25
+ it "should detect preload with post => comments" do
26
+ Post.includes(:comments).each do |post|
27
+ post.comments.map(&:name)
28
+ end
29
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
30
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
31
+
32
+ Bullet::Detector::Association.should be_completely_preloading_associations
64
33
  end
65
- Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Comment, :post)
66
- Bullet::Detector::Association.should be_completely_preloading_associations
67
- end
68
34
 
69
- it "should detect preload of post => writer" do
70
- comments = Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |com|
71
- com.post.writer.name
35
+ it "should detect unused preload post => comments" do
36
+ Post.includes(:comments).map(&:name)
37
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
38
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
39
+
40
+ Bullet::Detector::Association.should be_completely_preloading_associations
72
41
  end
73
- Bullet::Detector::Association.should be_creating_object_association_for(comments.first, :author)
74
- Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Post, :writer)
75
- Bullet::Detector::Association.should be_completely_preloading_associations
76
- end
77
42
 
78
- # To flyerhzm: This does not detect that newspaper is unpreloaded. The association is
79
- # not within possible objects, and thus cannot be detected as unpreloaded
80
- it "should detect unpreloading of writer => newspaper" do
81
- comments = Comment.all(:include => {:post => :writer}, :conditions => "posts.name like '%first%'").each do |com|
82
- com.post.writer.newspaper.name
43
+ it "should not detect unused preload post => comments" do
44
+ Post.all.map(&:name)
45
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
46
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
47
+
48
+ Bullet::Detector::Association.should be_completely_preloading_associations
83
49
  end
84
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Writer, :newspaper)
85
50
  end
86
51
 
87
- # when we attempt to access category, there is an infinite overflow because load_target is hijacked leading to
88
- # a repeating loop of calls in this test
89
- it "should not raise a stack error from posts to category" do
90
- lambda {
91
- Comment.includes({:post => :category}).each do |com|
92
- com.post.category
52
+ context "category => posts => comments" do
53
+ it "should detect non preload category => posts => comments" do
54
+ Category.all.each do |category|
55
+ category.posts.each do |post|
56
+ post.comments.map(&:name)
57
+ end
93
58
  end
94
- }.should_not raise_error(SystemStackError)
95
- end
96
- end
59
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
60
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
97
61
 
98
- context "post => comments" do
99
- #
100
- ### FIXME: Please double check semantic equivalence with original
101
- it "should detect preload with post => comments" do
102
- Post.includes(:comments).each do |post|
103
- post.comments.collect(&:name)
62
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Category, :posts)
63
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :comments)
104
64
  end
105
- # Bullet::Detector::Association.should_not be_has_unpreload_associations
106
- Bullet::Detector::Association.should be_completely_preloading_associations
107
- end
108
65
 
109
- it "should detect no preload post => comments" do
110
- Post.all.each do |post|
111
- post.comments.collect(&:name)
66
+ it "should detect preload category => posts, but no post => comments" do
67
+ Category.includes(:posts).each do |category|
68
+ category.posts.each do |post|
69
+ post.comments.collect(&:name)
70
+ end
71
+ end
72
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
73
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
74
+
75
+ Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Category, :posts)
76
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :comments)
112
77
  end
113
- # Bullet::Detector::Association.should be_has_unpreload_associations
114
- Bullet::Detector::Association.should_not be_completely_preloading_associations
115
- end
116
78
 
117
- it "should detect unused preload post => comments for post" do
118
- Post.includes(:comments).collect(&:name)
119
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
120
- Bullet::Detector::Association.should be_has_unused_preload_associations
121
- end
79
+ it "should detect preload with category => posts => comments" do
80
+ Category.includes({:posts => :comments}).each do |category|
81
+ category.posts.each do |post|
82
+ post.comments.map(&:name)
83
+ end
84
+ end
85
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
86
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
122
87
 
123
- it "should detect no unused preload post => comments for post" do
124
- Post.all.collect(&:name)
125
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
126
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
127
- end
88
+ Bullet::Detector::Association.should be_completely_preloading_associations
89
+ end
90
+
91
+ it "should detect unused preload with category => posts => comments" do
92
+ Category.includes({:posts => :comments}).map(&:name)
93
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
94
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
128
95
 
129
- it "should detect no unused preload post => comments for comment" do
130
- Post.all.each do |post|
131
- post.comments.collect(&:name)
96
+ Bullet::Detector::Association.should be_completely_preloading_associations
132
97
  end
133
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
134
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
135
98
 
136
- Bullet.end_request
137
- Bullet.start_request
99
+ it "should detect unused preload with post => commnets, no category => posts" do
100
+ Category.includes({:posts => :comments}).each do |category|
101
+ category.posts.map(&:name)
102
+ end
103
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
104
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
138
105
 
139
- Post.all.each do |post|
140
- post.comments.collect(&:name)
106
+ Bullet::Detector::Association.should be_completely_preloading_associations
141
107
  end
142
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
143
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
144
108
  end
145
- end
146
109
 
147
- context "category => posts => comments" do
148
- it "should detect preload with category => posts => comments" do
149
- Category.includes({:posts => :comments}).each do |category|
150
- category.posts.each do |post|
151
- post.comments.collect(&:name)
110
+ context "category => posts, category => entries" do
111
+ it "should detect non preload with category => [posts, entries]" do
112
+ Category.all.each do |category|
113
+ category.posts.map(&:name)
114
+ category.entries.map(&:name)
152
115
  end
116
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
117
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
118
+
119
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Category, :posts)
120
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Category, :entries)
153
121
  end
154
- # Bullet::Detector::Association.should_not be_has_unpreload_associations
155
- Bullet::Detector::Association.should be_completely_preloading_associations
156
- end
157
122
 
158
- it "should detect preload category => posts, but no post => comments" do
159
- Category.includes(:posts).each do |category|
160
- category.posts.each do |post|
161
- post.comments.collect(&:name)
123
+ it "should detect preload with category => posts, but not with category => entries" do
124
+ Category.includes(:posts).each do |category|
125
+ category.posts.map(&:name)
126
+ category.entries.map(&:name)
162
127
  end
128
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
129
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
130
+
131
+ Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Category, :posts)
132
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Category, :entries)
163
133
  end
164
- # Bullet::Detector::Association.should be_has_unpreload_associations
165
- Bullet::Detector::Association.should_not be_completely_preloading_associations
166
- end
167
134
 
168
- it "should detect no preload category => posts => comments" do
169
- Category.all.each do |category|
170
- category.posts.each do |post|
171
- post.comments.collect(&:name)
135
+ it "should detect preload with category => [posts, entries]" do
136
+ Category.includes([:posts, :entries]).each do |category|
137
+ category.posts.map(&:name)
138
+ category.entries.map(&:name)
172
139
  end
140
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
141
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
142
+
143
+ Bullet::Detector::Association.should be_completely_preloading_associations
173
144
  end
174
- # Bullet::Detector::Association.should be_has_unpreload_associations
175
- Bullet::Detector::Association.should_not be_completely_preloading_associations
176
- end
177
145
 
178
- it "should detect unused preload with category => posts => comments" do
179
- Category.includes({:posts => :comments}).collect(&:name)
180
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
181
- Bullet::Detector::Association.should be_has_unused_preload_associations
182
- end
146
+ it "should detect unused preload with category => [posts, entries]" do
147
+ Category.includes([:posts, :entries]).map(&:name)
148
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
149
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Category, :posts)
150
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Category, :entries)
183
151
 
184
- it "should detect unused preload with post => commnets, no category => posts" do
185
- Category.includes({:posts => :comments}).each do |category|
186
- category.posts.collect(&:name)
152
+ Bullet::Detector::Association.should be_completely_preloading_associations
187
153
  end
188
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
189
- Bullet::Detector::Association.should be_has_unused_preload_associations
190
- end
191
154
 
192
- it "should no detect preload with category => posts => comments" do
193
- Category.all.each do |category|
194
- category.posts.each do |post|
195
- post.comments.collect(&:name)
155
+ it "should detect unused preload with category => entries, but not with category => posts" do
156
+ Category.includes([:posts, :entries]).each do |category|
157
+ category.posts.map(&:name)
196
158
  end
159
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
160
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Category, :posts)
161
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Category, :entries)
162
+
163
+ Bullet::Detector::Association.should be_completely_preloading_associations
197
164
  end
198
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
199
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
200
165
  end
201
- end
202
166
 
203
- context "category => posts, category => entries" do
204
- it "should detect preload with category => [posts, entries]" do
205
- Category.includes([:posts, :entries]).each do |category|
206
- category.posts.collect(&:name)
207
- category.entries.collect(&:name)
167
+ context "post => comment" do
168
+ it "should detect unused preload with post => comments" do
169
+ Post.includes(:comments).each do |post|
170
+ post.comments.first.name
171
+ end
172
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
173
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Post, :comments)
174
+
175
+ Bullet::Detector::Association.should be_completely_preloading_associations
208
176
  end
209
- Bullet::Detector::Association.should be_completely_preloading_associations
210
- end
211
177
 
212
- it "should detect preload with category => posts, but no category => entries" do
213
- Category.includes(:posts).each do |category|
214
- category.posts.collect(&:name)
215
- category.entries.collect(&:name)
178
+ it "should detect preload with post => commnets" do
179
+ Post.first.comments.collect(&:name)
180
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
181
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
182
+
183
+ Bullet::Detector::Association.should be_completely_preloading_associations
216
184
  end
217
- Bullet::Detector::Association.should_not be_completely_preloading_associations
218
185
  end
219
186
 
220
- it "should detect no preload with category => [posts, entries]" do
221
- Category.all.each do |category|
222
- category.posts.collect(&:name)
223
- category.entries.collect(&:name)
187
+ context "category => posts => writer" do
188
+ it "should not detect unused preload associations" do
189
+ category = Category.includes({:posts => :writer}).order("id DESC").find_by_name('first')
190
+ category.posts.map do |post|
191
+ post.name
192
+ post.writer.name
193
+ end
194
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
195
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Category, :posts)
196
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Post, :writer)
224
197
  end
225
- Bullet::Detector::Association.should_not be_completely_preloading_associations
226
198
  end
227
199
 
228
- it "should detect unused with category => [posts, entries]" do
229
- Category.includes([:posts, :entries]).collect(&:name)
230
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
231
- Bullet::Detector::Association.should be_has_unused_preload_associations
232
- end
200
+ context "scope for_category_name" do
201
+ it "should detect preload with post => category" do
202
+ Post.in_category_name('first').all.each do |post|
203
+ post.category.name
204
+ end
205
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
206
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
233
207
 
234
- it "should detect unused preload with category => entries, but no category => posts" do
235
- Category.includes([:posts, :entries]).each do |category|
236
- category.posts.collect(&:name)
208
+ Bullet::Detector::Association.should be_completely_preloading_associations
237
209
  end
238
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
239
- Bullet::Detector::Association.should be_has_unused_preload_associations
240
- end
241
210
 
242
- it "should detect no unused preload" do
243
- Category.all.each do |category|
244
- category.posts.collect(&:name)
245
- category.entries.collect(&:name)
211
+ it "should not be unused preload post => category" do
212
+ Post.in_category_name('first').all.map(&:name)
213
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
214
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
215
+
216
+ Bullet::Detector::Association.should be_completely_preloading_associations
246
217
  end
247
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
248
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
249
218
  end
250
- end
251
219
 
252
- context "no preload" do
253
- it "should no preload only display only one post => comment" do
254
- Post.includes(:comments).each do |post|
255
- post.comments.first.name
220
+ context "scope preload_comments" do
221
+ it "should detect preload post => comments with scope" do
222
+ Post.preload_comments.each do |post|
223
+ post.comments.map(&:name)
224
+ end
225
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
226
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
227
+
228
+ Bullet::Detector::Association.should be_completely_preloading_associations
256
229
  end
257
- Bullet::Detector::Association.should be_completely_preloading_associations
258
- end
259
230
 
260
- it "should no preload only one post => commnets" do
261
- Post.first.comments.collect(&:name)
262
- Bullet::Detector::Association.should be_completely_preloading_associations
231
+ it "should detect unused preload with scope" do
232
+ Post.preload_comments.map(&:name)
233
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
234
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
235
+
236
+ Bullet::Detector::Association.should be_completely_preloading_associations
237
+ end
263
238
  end
264
239
  end
265
240
 
266
- context "scope for_category_name" do
267
- it "should detect preload with post => category" do
268
- Post.in_category_name('first').all.each do |post|
269
- post.category.name
270
- end
271
- Bullet::Detector::Association.should be_completely_preloading_associations
241
+ describe Bullet::Detector::Association, 'belongs_to' do
242
+ before(:each) do
243
+ Bullet.clear
244
+ Bullet.start_request
272
245
  end
273
246
 
274
- it "should not be unused preload post => category" do
275
- Post.in_category_name('first').all.collect(&:name)
276
- Bullet::Detector::Association.should be_completely_preloading_associations
277
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
278
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
247
+ after(:each) do
248
+ Bullet.end_request
279
249
  end
280
- end
281
250
 
282
- context "scope preload_posts" do
283
- it "should no preload post => comments with scope" do
284
- Post.preload_posts.each do |post|
285
- post.comments.collect(&:name)
251
+ context "comment => post" do
252
+ it "should detect non preload with comment => post" do
253
+ Comment.all.each do |comment|
254
+ comment.post.name
255
+ end
256
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
257
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
258
+
259
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Comment, :post)
286
260
  end
287
- Bullet::Detector::Association.should be_completely_preloading_associations
288
- end
289
261
 
290
- it "should unused preload with scope" do
291
- Post.preload_posts.collect(&:name)
292
- Bullet::Detector::Association.should be_completely_preloading_associations
293
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
294
- Bullet::Detector::Association.should be_has_unused_preload_associations
295
- end
296
- end
262
+ it "should detect preload with one comment => post" do
263
+ Comment.first.post.name
264
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
265
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
297
266
 
298
- context "no unused" do
299
- it "should no unused only display only one post => comment" do
300
- Post.includes(:comments).each do |post|
301
- i = 0
302
- post.comments.each do |comment|
303
- if i == 0
304
- comment.name
305
- else
306
- i += 1
307
- end
267
+ Bullet::Detector::Association.should be_completely_preloading_associations
268
+ end
269
+
270
+ it "should dtect preload with comment => post" do
271
+ Comment.includes(:post).each do |comment|
272
+ comment.post.name
308
273
  end
274
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
275
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
276
+
277
+ Bullet::Detector::Association.should be_completely_preloading_associations
309
278
  end
310
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
311
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
312
- end
313
- end
314
279
 
315
- context "belongs_to" do
316
- it "should preload comments => post" do
317
- Comment.all.each do |comment|
318
- comment.post.name
280
+ it "should not detect preload with comment => post" do
281
+ Comment.all.collect(&:name)
282
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
283
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
284
+
285
+ Bullet::Detector::Association.should be_completely_preloading_associations
319
286
  end
320
- Bullet::Detector::Association.should_not be_completely_preloading_associations
321
- end
322
287
 
323
- it "should no preload comment => post" do
324
- Comment.first.post.name
325
- Bullet::Detector::Association.should be_completely_preloading_associations
326
- end
288
+ it "should detect unused preload with comments => post" do
289
+ Comment.includes(:post).map(&:name)
290
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
291
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Comment, :post)
327
292
 
328
- it "should no preload comments => post" do
329
- Comment.includes(:post).each do |comment|
330
- comment.post.name
293
+ Bullet::Detector::Association.should be_completely_preloading_associations
331
294
  end
332
- Bullet::Detector::Association.should be_completely_preloading_associations
333
295
  end
334
296
 
335
- it "should detect no unused preload comments => post" do
336
- Comment.all.collect(&:name)
337
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
338
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
339
- end
297
+ context "comment => post => category" do
298
+ it "should detect non preload association with comment => post" do
299
+ Comment.all.each do |comment|
300
+ comment.post.category.name
301
+ end
302
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
303
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
340
304
 
341
- it "should detect unused preload comments => post" do
342
- Comment.includes(:post).collect(&:name)
343
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
344
- Bullet::Detector::Association.should be_has_unused_preload_associations
345
- end
305
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Comment, :post)
306
+ end
307
+
308
+ it "should detect non preload association with post => category" do
309
+ Comment.includes(:post).each do |comment|
310
+ comment.post.category.name
311
+ end
312
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
313
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
346
314
 
347
- it "should dectect no unused preload comments => post" do
348
- Comment.all.each do |comment|
349
- comment.post.name
315
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :category)
350
316
  end
351
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
352
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
353
- end
354
317
 
355
- it "should dectect no unused preload comments => post" do
356
- Comment.includes(:post).each do |comment|
357
- comment.post.name
318
+ it "should not detect unpreload association" do
319
+ Comment.includes(:post => :category).each do |comment|
320
+ comment.post.category.name
321
+ end
322
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
323
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
324
+
325
+ Bullet::Detector::Association.should be_completely_preloading_associations
358
326
  end
359
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
360
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
361
327
  end
362
- end
363
- end
364
328
 
365
- describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
366
- before(:each) do
367
- Bullet.start_request
368
- end
329
+ context "comment => author, post => writer" do
330
+ # this happens because the post isn't a possible object even though the writer is access through the post
331
+ # which leads to an 1+N queries
332
+ it "should detect non preloaded writer" do
333
+ Comment.includes([:author, :post]).where(["base_users.id = ?", BaseUser.first]).each do |comment|
334
+ comment.post.writer.name
335
+ end
336
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
337
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
369
338
 
370
- after(:each) do
371
- Bullet.end_request
372
- end
339
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :writer)
340
+ end
341
+
342
+ # this happens because the comment doesn't break down the hash into keys
343
+ # properly creating an association from comment to post
344
+ it "should detect unused preload with comment => author" do
345
+ Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |comment|
346
+ comment.post.writer.name
347
+ end
348
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
349
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Comment, :author)
350
+
351
+ Bullet::Detector::Association.should be_completely_preloading_associations
352
+ end
373
353
 
374
- it "should detect unpreload associations" do
375
- Student.all.each do |student|
376
- student.teachers.collect(&:name)
354
+ # To flyerhzm: This does not detect that newspaper is unpreloaded. The association is
355
+ # not within possible objects, and thus cannot be detected as unpreloaded
356
+ it "should detect non preloading with writer => newspaper" do
357
+ Comment.all(:include => {:post => :writer}, :conditions => "posts.name like '%first%'").each do |comment|
358
+ comment.post.writer.newspaper.name
359
+ end
360
+ #Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
361
+ #Bullet::Detector::Association.should_not be_has_unused_preload_associations
362
+
363
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Writer, :newspaper)
364
+ end
365
+
366
+ # when we attempt to access category, there is an infinite overflow because load_target is hijacked leading to
367
+ # a repeating loop of calls in this test
368
+ it "should not raise a stack error from posts to category" do
369
+ lambda {
370
+ Comment.includes({:post => :category}).each do |com|
371
+ com.post.category
372
+ end
373
+ }.should_not raise_error(SystemStackError)
374
+ end
377
375
  end
378
- Bullet::Detector::Association.should_not be_completely_preloading_associations
379
376
  end
380
377
 
381
- it "should detect no unpreload associations" do
382
- Student.includes(:teachers).each do |student|
383
- student.teachers.collect(&:name)
378
+ describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
379
+ before(:each) do
380
+ Bullet.clear
381
+ Bullet.start_request
384
382
  end
385
- Bullet::Detector::Association.should be_completely_preloading_associations
386
- end
387
383
 
388
- it "should detect unused preload associations" do
389
- Student.includes(:teachers).collect(&:name)
390
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
391
- Bullet::Detector::Association.should be_has_unused_preload_associations
392
- end
384
+ after(:each) do
385
+ Bullet.end_request
386
+ end
393
387
 
394
- it "should detect no unused preload associations" do
395
- Student.all.collect(&:name)
396
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
397
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
398
- end
399
- end
388
+ context "students <=> teachers" do
389
+ it "should detect non preload associations" do
390
+ Student.all.each do |student|
391
+ student.teachers.map(&:name)
392
+ end
393
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
394
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
400
395
 
401
- describe Bullet::Detector::Association, 'has_many :through' do
402
- before(:each) do
403
- Bullet.start_request
404
- end
396
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Student, :teachers)
397
+ end
405
398
 
406
- after(:each) do
407
- Bullet.end_request
408
- end
399
+ it "should detect preload associations" do
400
+ Student.includes(:teachers).each do |student|
401
+ student.teachers.map(&:name)
402
+ end
403
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
404
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
405
+
406
+ Bullet::Detector::Association.should be_completely_preloading_associations
407
+ end
408
+
409
+ it "should detect unused preload associations" do
410
+ Student.includes(:teachers).map(&:name)
411
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
412
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Student, :teachers)
409
413
 
410
- it "should detect unpreload associations" do
411
- Firm.all.each do |firm|
412
- firm.clients.collect(&:name)
414
+ Bullet::Detector::Association.should be_completely_preloading_associations
415
+ end
416
+
417
+ it "should detect no unused preload associations" do
418
+ Student.all.collect(&:name)
419
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
420
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
421
+
422
+ Bullet::Detector::Association.should be_completely_preloading_associations
423
+ end
413
424
  end
414
- Bullet::Detector::Association.should_not be_completely_preloading_associations
415
425
  end
416
426
 
417
- it "should detect no unpreload associations" do
418
- Firm.includes(:clients).each do |firm|
419
- firm.clients.collect(&:name)
427
+ describe Bullet::Detector::Association, 'has_many :through' do
428
+ before(:each) do
429
+ Bullet.clear
430
+ Bullet.start_request
420
431
  end
421
- Bullet::Detector::Association.should be_completely_preloading_associations
422
- end
423
432
 
424
- it "should detect no unused preload associations" do
425
- Firm.all.collect(&:name)
426
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
427
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
428
- end
433
+ after(:each) do
434
+ Bullet.end_request
435
+ end
429
436
 
430
- it "should detect unused preload associations" do
431
- Firm.includes(:clients).collect(&:name)
432
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
433
- Bullet::Detector::Association.should be_has_unused_preload_associations
434
- end
435
- end
437
+ context "firm => clients" do
438
+ it "should detect non preload associations" do
439
+ Firm.all.each do |firm|
440
+ firm.clients.map(&:name)
441
+ end
442
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
443
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
444
+
445
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Firm, :clients)
446
+ end
436
447
 
448
+ it "should detect preload associations" do
449
+ Firm.includes(:clients).each do |firm|
450
+ firm.clients.map(&:name)
451
+ end
452
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
453
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
437
454
 
455
+ Bullet::Detector::Association.should be_completely_preloading_associations
456
+ end
438
457
 
439
- describe Bullet::Detector::Association, "has_one" do
440
- before(:each) do
441
- Bullet.start_request
442
- end
458
+ it "should not detect preload associations" do
459
+ Firm.all.collect(&:name)
460
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
461
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
443
462
 
444
- after(:each) do
445
- Bullet.end_request
446
- end
463
+ Bullet::Detector::Association.should be_completely_preloading_associations
464
+ end
465
+
466
+ it "should detect unused preload associations" do
467
+ Firm.includes(:clients).collect(&:name)
468
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
469
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Firm, :clients)
447
470
 
448
- it "should detect unpreload association" do
449
- Company.all.each do |company|
450
- company.address.name
471
+ Bullet::Detector::Association.should be_completely_preloading_associations
472
+ end
451
473
  end
452
- Bullet::Detector::Association.should_not be_completely_preloading_associations
453
474
  end
454
475
 
455
- it "should detect no unpreload association" do
456
- Company.find(:all, :include => :address).each do |company|
457
- company.address.name
476
+ describe Bullet::Detector::Association, "has_one" do
477
+ before(:each) do
478
+ Bullet.clear
479
+ Bullet.start_request
458
480
  end
459
- Bullet::Detector::Association.should be_completely_preloading_associations
460
- end
461
481
 
462
- it "should detect no unused preload association" do
463
- Company.all.collect(&:name)
464
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
465
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
466
- end
482
+ after(:each) do
483
+ Bullet.end_request
484
+ end
467
485
 
468
- it "should detect unused preload association" do
469
- Company.find(:all, :include => :address).collect(&:name)
470
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
471
- Bullet::Detector::Association.should be_has_unused_preload_associations
472
- end
473
- end
486
+ context "company => address" do
487
+ it "should detect non preload association" do
488
+ Company.all.each do |company|
489
+ company.address.name
490
+ end
491
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
492
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
474
493
 
475
- describe Bullet::Detector::Association, "call one association that in possible objects" do
476
- before(:each) do
477
- Bullet.start_request
478
- end
494
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Company, :address)
495
+ end
479
496
 
480
- after(:each) do
481
- Bullet.end_request
482
- end
497
+ it "should detect preload association" do
498
+ Company.find(:all, :include => :address).each do |company|
499
+ company.address.name
500
+ end
501
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
502
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
483
503
 
484
- it "should detect no unpreload association" do
485
- Contact.all
486
- Contact.first.emails.collect(&:name)
487
- Bullet::Detector::Association.should be_completely_preloading_associations
488
- end
489
- end
504
+ Bullet::Detector::Association.should be_completely_preloading_associations
505
+ end
490
506
 
491
- describe Bullet::Detector::Association, "STI" do
492
- before(:each) do
493
- Bullet.start_request
494
- end
507
+ it "should not detect preload association" do
508
+ Company.all.collect(&:name)
509
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
510
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
495
511
 
496
- after(:each) do
497
- Bullet.end_request
498
- end
512
+ Bullet::Detector::Association.should be_completely_preloading_associations
513
+ end
514
+
515
+ it "should detect unused preload association" do
516
+ Company.find(:all, :include => :address).collect(&:name)
517
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
518
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Company, :address)
499
519
 
500
- it "should detect unpreload associations" do
501
- Page.all.each do |page|
502
- page.author.name
520
+ Bullet::Detector::Association.should be_completely_preloading_associations
521
+ end
503
522
  end
504
- Bullet::Detector::Association.should_not be_completely_preloading_associations
505
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
506
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
507
523
  end
508
524
 
509
- it "should not detect unpreload associations" do
510
- Page.find(:all, :include => :author).each do |page|
511
- page.author.name
525
+ describe Bullet::Detector::Association, "call one association that in possible objects" do
526
+ before(:each) do
527
+ Bullet.clear
528
+ Bullet.start_request
512
529
  end
513
- Bullet::Detector::Association.should be_completely_preloading_associations
514
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
515
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
516
- end
517
530
 
518
- it "should detect unused preload associations" do
519
- Page.find(:all, :include => :author).collect(&:name)
520
- Bullet::Detector::Association.should be_completely_preloading_associations
521
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
522
- Bullet::Detector::Association.should be_has_unused_preload_associations
531
+ after(:each) do
532
+ Bullet.end_request
533
+ end
534
+
535
+ it "should not detect preload association" do
536
+ Post.all
537
+ Post.first.comments.map(&:name)
538
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
539
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
540
+
541
+ Bullet::Detector::Association.should be_completely_preloading_associations
542
+ end
523
543
  end
524
544
 
525
- it "should not detect unused preload associations" do
526
- Page.all.collect(&:name)
527
- Bullet::Detector::Association.should be_completely_preloading_associations
528
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
529
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
545
+ describe Bullet::Detector::Association, "STI" do
546
+ before(:each) do
547
+ Bullet.clear
548
+ Bullet.start_request
549
+ end
550
+
551
+ after(:each) do
552
+ Bullet.end_request
553
+ end
554
+
555
+ context "page => author" do
556
+ it "should detect non preload associations" do
557
+ Page.all.each do |page|
558
+ page.author.name
559
+ end
560
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
561
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
562
+
563
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Page, :author)
564
+ end
565
+
566
+ it "should detect preload associations" do
567
+ Page.find(:all, :include => :author).each do |page|
568
+ page.author.name
569
+ end
570
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
571
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
572
+
573
+ Bullet::Detector::Association.should be_completely_preloading_associations
574
+ end
575
+
576
+ it "should detect unused preload associations" do
577
+ Page.find(:all, :include => :author).collect(&:name)
578
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
579
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Page, :author)
580
+
581
+ Bullet::Detector::Association.should be_completely_preloading_associations
582
+ end
583
+
584
+ it "should not detect preload associations" do
585
+ Page.all.collect(&:name)
586
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
587
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
588
+
589
+ Bullet::Detector::Association.should be_completely_preloading_associations
590
+ end
591
+ end
530
592
  end
531
593
  end