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
@@ -1,37 +1,39 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Bullet::Detector::Counter do
4
- before(:each) do
5
- Bullet.start_request
6
- end
3
+ if Rails.version.to_i > 2
4
+ describe Bullet::Detector::Counter do
5
+ before(:each) do
6
+ Bullet.start_request
7
+ end
7
8
 
8
- after(:each) do
9
- Bullet.end_request
10
- end
9
+ after(:each) do
10
+ Bullet.end_request
11
+ end
11
12
 
12
- it "should need counter cache with all cities" do
13
- Country.all.each do |country|
14
- country.cities.size
13
+ it "should need counter cache with all cities" do
14
+ Country.all.each do |country|
15
+ country.cities.size
16
+ end
17
+ Bullet.collected_counter_cache_notifications.should_not be_empty
15
18
  end
16
- Bullet.collected_counter_cache_notifications.should_not be_empty
17
- end
18
19
 
19
- it "should not need counter cache if already define counter_cache" do
20
- Person.all.each do |person|
21
- person.pets.size
20
+ it "should not need counter cache if already define counter_cache" do
21
+ Person.all.each do |person|
22
+ person.pets.size
23
+ end
24
+ Bullet.collected_counter_cache_notifications.should be_empty
22
25
  end
23
- Bullet.collected_counter_cache_notifications.should be_empty
24
- end
25
26
 
26
- it "should not need counter cache with only one object" do
27
- Country.first.cities.size
28
- Bullet.collected_counter_cache_notifications.should be_empty
29
- end
27
+ it "should not need counter cache with only one object" do
28
+ Country.first.cities.size
29
+ Bullet.collected_counter_cache_notifications.should be_empty
30
+ end
30
31
 
31
- it "should not need counter cache with part of cities" do
32
- Country.all.each do |country|
33
- country.cities.where(:name => 'first').size
32
+ it "should not need counter cache with part of cities" do
33
+ Country.all.each do |country|
34
+ country.cities.where(:name => 'first').size
35
+ end
36
+ Bullet.collected_counter_cache_notifications.should be_empty
34
37
  end
35
- Bullet.collected_counter_cache_notifications.should be_empty
36
38
  end
37
39
  end
@@ -0,0 +1,276 @@
1
+ require 'spec_helper'
2
+
3
+ begin
4
+ require 'mongoid'
5
+ describe Bullet::Detector::Association, 'has_many' do
6
+ before(:each) do
7
+ Bullet.clear
8
+ Bullet.start_request
9
+ end
10
+ after(:each) do
11
+ Bullet.end_request
12
+ end
13
+
14
+ context "posts => comments" do
15
+ it "should detect non preload posts => comments" do
16
+ Mongoid::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
21
+
22
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Post, :comments)
23
+ end
24
+
25
+ it "should detect preload post => comments" do
26
+ Mongoid::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
33
+ end
34
+
35
+ it "should detect unused preload post => comments" do
36
+ Mongoid::Post.includes(:comments).map(&:name)
37
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
38
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Post, :comments)
39
+
40
+ Bullet::Detector::Association.should be_completely_preloading_associations
41
+ end
42
+
43
+ it "should not detect unused preload post => comments" do
44
+ Mongoid::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
49
+ end
50
+ end
51
+
52
+ context "category => posts, category => entries" do
53
+ it "should detect non preload with category => [posts, entries]" do
54
+ Mongoid::Category.all.each do |category|
55
+ category.posts.map(&:name)
56
+ category.entries.map(&:name)
57
+ end
58
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
59
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
60
+
61
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :posts)
62
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
63
+ end
64
+
65
+ it "should detect preload with category => posts, but not with category => entries" do
66
+ Mongoid::Category.includes(:posts).each do |category|
67
+ category.posts.collect(&:name)
68
+ category.entries.collect(&:name)
69
+ end
70
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
71
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
72
+
73
+ Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Mongoid::Category, :posts)
74
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
75
+ end
76
+
77
+ it "should detect preload with category => [posts, entries]" do
78
+ Mongoid::Category.includes([:posts, :entries]).each do |category|
79
+ category.posts.map(&:name)
80
+ category.entries.map(&:name)
81
+ end
82
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
83
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
84
+
85
+ Bullet::Detector::Association.should be_completely_preloading_associations
86
+ end
87
+
88
+ it "should detect unused preload with category => [posts, entries]" do
89
+ Mongoid::Category.includes([:posts, :entries]).map(&:name)
90
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
91
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :posts)
92
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :entries)
93
+
94
+ Bullet::Detector::Association.should be_completely_preloading_associations
95
+ end
96
+
97
+ it "should detect unused preload with category => entries, but not with category => posts" do
98
+ Mongoid::Category.includes([:posts, :entries]).each do |category|
99
+ category.posts.map(&:name)
100
+ end
101
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
102
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Mongoid::Category, :posts)
103
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :entries)
104
+
105
+ Bullet::Detector::Association.should be_completely_preloading_associations
106
+ end
107
+ end
108
+
109
+ context "post => comment" do
110
+ it "should detect unused preload with post => comments" do
111
+ Mongoid::Post.includes(:comments).each do |post|
112
+ post.comments.first.name
113
+ end
114
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
115
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Mongoid::Post, :comments)
116
+
117
+ Bullet::Detector::Association.should be_completely_preloading_associations
118
+ end
119
+
120
+ it "should detect preload with post => commnets" do
121
+ Mongoid::Post.first.comments.collect(&:name)
122
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
123
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
124
+
125
+ Bullet::Detector::Association.should be_completely_preloading_associations
126
+ end
127
+ end
128
+
129
+ context "scope preload_comments" do
130
+ it "should detect preload post => comments with scope" do
131
+ Mongoid::Post.preload_comments.each do |post|
132
+ post.comments.map(&:name)
133
+ end
134
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
135
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
136
+
137
+ Bullet::Detector::Association.should be_completely_preloading_associations
138
+ end
139
+
140
+ it "should detect unused preload with scope" do
141
+ Mongoid::Post.preload_comments.map(&:name)
142
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
143
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Post, :comments)
144
+
145
+ Bullet::Detector::Association.should be_completely_preloading_associations
146
+ end
147
+ end
148
+ end
149
+
150
+ describe Bullet::Detector::Association, 'belongs_to' do
151
+ before(:each) do
152
+ Bullet.clear
153
+ Bullet.start_request
154
+ end
155
+
156
+ after(:each) do
157
+ Bullet.end_request
158
+ end
159
+
160
+ context "comment => post" do
161
+ it "should detect non preload with comment => post" do
162
+ Mongoid::Comment.all.each do |comment|
163
+ comment.post.name
164
+ end
165
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
166
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
167
+
168
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Comment, :post)
169
+ end
170
+
171
+ it "should detect preload with one comment => post" do
172
+ Mongoid::Comment.first.post.name
173
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
174
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
175
+
176
+ Bullet::Detector::Association.should be_completely_preloading_associations
177
+ end
178
+
179
+ it "should detect preload with comment => post" do
180
+ Mongoid::Comment.includes(:post).each do |comment|
181
+ comment.post.name
182
+ end
183
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
184
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
185
+
186
+ Bullet::Detector::Association.should be_completely_preloading_associations
187
+ end
188
+
189
+ it "should not detect preload with comment => post" do
190
+ Mongoid::Comment.all.collect(&:name)
191
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
192
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
193
+
194
+ Bullet::Detector::Association.should be_completely_preloading_associations
195
+ end
196
+
197
+ it "should detect unused preload with comments => post" do
198
+ Mongoid::Comment.includes(:post).map(&:name)
199
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
200
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Comment, :post)
201
+
202
+ Bullet::Detector::Association.should be_completely_preloading_associations
203
+ end
204
+ end
205
+ end
206
+
207
+ describe Bullet::Detector::Association, "has_one" do
208
+ before(:each) do
209
+ Bullet.clear
210
+ Bullet.start_request
211
+ end
212
+
213
+ after(:each) do
214
+ Bullet.end_request
215
+ end
216
+
217
+ context "company => address" do
218
+ it "should detect non preload association" do
219
+ Mongoid::Company.all.each do |company|
220
+ company.address.name
221
+ end
222
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
223
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
224
+
225
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Company, :address)
226
+ end
227
+
228
+ it "should detect preload association" do
229
+ Mongoid::Company.includes(:address).each do |company|
230
+ company.address.name
231
+ end
232
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
233
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
234
+
235
+ Bullet::Detector::Association.should be_completely_preloading_associations
236
+ end
237
+
238
+ it "should not detect preload association" do
239
+ Mongoid::Company.all.collect(&:name)
240
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
241
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
242
+
243
+ Bullet::Detector::Association.should be_completely_preloading_associations
244
+ end
245
+
246
+ it "should detect unused preload association" do
247
+ Mongoid::Company.includes(:address).collect(&:name)
248
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
249
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Company, :address)
250
+
251
+ Bullet::Detector::Association.should be_completely_preloading_associations
252
+ end
253
+ end
254
+ end
255
+
256
+ describe Bullet::Detector::Association, "call one association that in possible objects" do
257
+ before(:each) do
258
+ Bullet.clear
259
+ Bullet.start_request
260
+ end
261
+
262
+ after(:each) do
263
+ Bullet.end_request
264
+ end
265
+
266
+ it "should not detect preload association" do
267
+ Mongoid::Post.all
268
+ Mongoid::Post.first.comments.map(&:name)
269
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
270
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
271
+
272
+ Bullet::Detector::Association.should be_completely_preloading_associations
273
+ end
274
+ end
275
+ rescue LoadError
276
+ end
@@ -0,0 +1,593 @@
1
+ require 'spec_helper'
2
+
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
8
+ end
9
+
10
+ after(:each) do
11
+ Bullet.end_request
12
+ end
13
+
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
21
+
22
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :comments)
23
+ end
24
+
25
+ it "should detect preload with post => comments" do
26
+ Post.all(:include => :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
33
+ end
34
+
35
+ it "should detect unused preload post => comments" do
36
+ Post.all(:include => :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
41
+ end
42
+
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
49
+ end
50
+ end
51
+
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
58
+ end
59
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
60
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
61
+
62
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Category, :posts)
63
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :comments)
64
+ end
65
+
66
+ it "should detect preload category => posts, but no post => comments" do
67
+ Category.all(:include => :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)
77
+ end
78
+
79
+ it "should detect preload with category => posts => comments" do
80
+ Category.all(:include => {: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
87
+
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.all(:include => {:posts => :comments}).map(&:name)
93
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
94
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Post, :comments)
95
+
96
+ Bullet::Detector::Association.should be_completely_preloading_associations
97
+ end
98
+
99
+ it "should detect unused preload with post => commnets, no category => posts" do
100
+ Category.all(:include => {: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)
105
+
106
+ Bullet::Detector::Association.should be_completely_preloading_associations
107
+ end
108
+ end
109
+
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)
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)
121
+ end
122
+
123
+ it "should detect preload with category => posts, but not with category => entries" do
124
+ Category.all(:include => :posts).each do |category|
125
+ category.posts.map(&:name)
126
+ category.entries.map(&:name)
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)
133
+ end
134
+
135
+ it "should detect preload with category => [posts, entries]" do
136
+ Category.all(:include => [:posts, :entries]).each do |category|
137
+ category.posts.map(&:name)
138
+ category.entries.map(&:name)
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
144
+ end
145
+
146
+ it "should detect unused preload with category => [posts, entries]" do
147
+ Category.all(:include => [: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)
151
+
152
+ Bullet::Detector::Association.should be_completely_preloading_associations
153
+ end
154
+
155
+ it "should detect unused preload with category => entries, but not with category => posts" do
156
+ Category.all(:include => [:posts, :entries]).each do |category|
157
+ category.posts.map(&:name)
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
164
+ end
165
+ end
166
+
167
+ context "post => comment" do
168
+ it "should detect unused preload with post => comments" do
169
+ Post.all(:include => :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
176
+ end
177
+
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
184
+ end
185
+ end
186
+
187
+ context "category => posts => writer" do
188
+ it "should not detect unused preload associations" do
189
+ category = Category.first(:include => {:posts => :writer}, :order => "id DESC", :conditions => {: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)
197
+ end
198
+ end
199
+
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
207
+
208
+ Bullet::Detector::Association.should be_completely_preloading_associations
209
+ end
210
+
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
217
+ end
218
+ end
219
+
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
229
+ end
230
+
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
238
+ end
239
+ end
240
+
241
+ describe Bullet::Detector::Association, 'belongs_to' do
242
+ before(:each) do
243
+ Bullet.clear
244
+ Bullet.start_request
245
+ end
246
+
247
+ after(:each) do
248
+ Bullet.end_request
249
+ end
250
+
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)
260
+ end
261
+
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
266
+
267
+ Bullet::Detector::Association.should be_completely_preloading_associations
268
+ end
269
+
270
+ it "should dtect preload with comment => post" do
271
+ Comment.all(:include => :post).each do |comment|
272
+ comment.post.name
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
278
+ end
279
+
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
286
+ end
287
+
288
+ it "should detect unused preload with comments => post" do
289
+ Comment.all(:include => :post).map(&:name)
290
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
291
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Comment, :post)
292
+
293
+ Bullet::Detector::Association.should be_completely_preloading_associations
294
+ end
295
+ end
296
+
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
304
+
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.all(:include => :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
314
+
315
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :category)
316
+ end
317
+
318
+ it "should not detect unpreload association" do
319
+ Comment.all(:include => {: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
326
+ end
327
+ end
328
+
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.all(:include => [:author, :post], :conditions => ["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
338
+
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.all(:include => [:author, {:post => :writer}], :conditions => ["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
353
+
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.all(:include => {:post => :category}).each do |com|
371
+ com.post.category
372
+ end
373
+ }.should_not raise_error(SystemStackError)
374
+ end
375
+ end
376
+ end
377
+
378
+ describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
379
+ before(:each) do
380
+ Bullet.clear
381
+ Bullet.start_request
382
+ end
383
+
384
+ after(:each) do
385
+ Bullet.end_request
386
+ end
387
+
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
395
+
396
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Student, :teachers)
397
+ end
398
+
399
+ it "should detect preload associations" do
400
+ Student.all(:include => :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.all(:include => :teachers).map(&:name)
411
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
412
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Student, :teachers)
413
+
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
424
+ end
425
+ end
426
+
427
+ describe Bullet::Detector::Association, 'has_many :through' do
428
+ before(:each) do
429
+ Bullet.clear
430
+ Bullet.start_request
431
+ end
432
+
433
+ after(:each) do
434
+ Bullet.end_request
435
+ end
436
+
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
447
+
448
+ it "should detect preload associations" do
449
+ Firm.all(:include => :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
454
+
455
+ Bullet::Detector::Association.should be_completely_preloading_associations
456
+ end
457
+
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
462
+
463
+ Bullet::Detector::Association.should be_completely_preloading_associations
464
+ end
465
+
466
+ it "should detect unused preload associations" do
467
+ Firm.all(:include => :clients).collect(&:name)
468
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
469
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Firm, :clients)
470
+
471
+ Bullet::Detector::Association.should be_completely_preloading_associations
472
+ end
473
+ end
474
+ end
475
+
476
+ describe Bullet::Detector::Association, "has_one" do
477
+ before(:each) do
478
+ Bullet.clear
479
+ Bullet.start_request
480
+ end
481
+
482
+ after(:each) do
483
+ Bullet.end_request
484
+ end
485
+
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
493
+
494
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Company, :address)
495
+ end
496
+
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
503
+
504
+ Bullet::Detector::Association.should be_completely_preloading_associations
505
+ end
506
+
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
511
+
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)
519
+
520
+ Bullet::Detector::Association.should be_completely_preloading_associations
521
+ end
522
+ end
523
+ end
524
+
525
+ describe Bullet::Detector::Association, "call one association that in possible objects" do
526
+ before(:each) do
527
+ Bullet.clear
528
+ Bullet.start_request
529
+ end
530
+
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
543
+ end
544
+
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
592
+ end
593
+ end