bullet_instructure 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +20 -0
  6. data/CHANGELOG.md +75 -0
  7. data/Gemfile +19 -0
  8. data/Gemfile.mongoid +14 -0
  9. data/Gemfile.mongoid-2.4 +19 -0
  10. data/Gemfile.mongoid-2.5 +19 -0
  11. data/Gemfile.mongoid-2.6 +19 -0
  12. data/Gemfile.mongoid-2.7 +19 -0
  13. data/Gemfile.mongoid-2.8 +19 -0
  14. data/Gemfile.mongoid-3.0 +19 -0
  15. data/Gemfile.mongoid-3.1 +19 -0
  16. data/Gemfile.mongoid-4.0 +19 -0
  17. data/Gemfile.rails-3.0 +19 -0
  18. data/Gemfile.rails-3.1 +19 -0
  19. data/Gemfile.rails-3.2 +19 -0
  20. data/Gemfile.rails-4.0 +19 -0
  21. data/Gemfile.rails-4.1 +19 -0
  22. data/Guardfile +8 -0
  23. data/Hacking.md +74 -0
  24. data/MIT-LICENSE +20 -0
  25. data/README.md +428 -0
  26. data/Rakefile +52 -0
  27. data/bullet_instructure.gemspec +27 -0
  28. data/lib/bullet.rb +196 -0
  29. data/lib/bullet/active_record3.rb +148 -0
  30. data/lib/bullet/active_record3x.rb +128 -0
  31. data/lib/bullet/active_record4.rb +128 -0
  32. data/lib/bullet/active_record41.rb +121 -0
  33. data/lib/bullet/dependency.rb +81 -0
  34. data/lib/bullet/detector.rb +9 -0
  35. data/lib/bullet/detector/association.rb +67 -0
  36. data/lib/bullet/detector/base.rb +6 -0
  37. data/lib/bullet/detector/counter_cache.rb +59 -0
  38. data/lib/bullet/detector/n_plus_one_query.rb +89 -0
  39. data/lib/bullet/detector/unused_eager_loading.rb +84 -0
  40. data/lib/bullet/ext/object.rb +9 -0
  41. data/lib/bullet/ext/string.rb +5 -0
  42. data/lib/bullet/mongoid2x.rb +56 -0
  43. data/lib/bullet/mongoid3x.rb +56 -0
  44. data/lib/bullet/mongoid4x.rb +56 -0
  45. data/lib/bullet/notification.rb +10 -0
  46. data/lib/bullet/notification/base.rb +97 -0
  47. data/lib/bullet/notification/counter_cache.rb +13 -0
  48. data/lib/bullet/notification/n_plus_one_query.rb +28 -0
  49. data/lib/bullet/notification/unused_eager_loading.rb +13 -0
  50. data/lib/bullet/notification_collector.rb +24 -0
  51. data/lib/bullet/rack.rb +81 -0
  52. data/lib/bullet/registry.rb +7 -0
  53. data/lib/bullet/registry/association.rb +13 -0
  54. data/lib/bullet/registry/base.rb +40 -0
  55. data/lib/bullet/registry/object.rb +13 -0
  56. data/lib/bullet/version.rb +4 -0
  57. data/perf/benchmark.rb +121 -0
  58. data/rails/init.rb +1 -0
  59. data/spec/bullet/detector/association_spec.rb +26 -0
  60. data/spec/bullet/detector/base_spec.rb +8 -0
  61. data/spec/bullet/detector/counter_cache_spec.rb +56 -0
  62. data/spec/bullet/detector/n_plus_one_query_spec.rb +138 -0
  63. data/spec/bullet/detector/unused_eager_loading_spec.rb +88 -0
  64. data/spec/bullet/ext/object_spec.rb +17 -0
  65. data/spec/bullet/ext/string_spec.rb +13 -0
  66. data/spec/bullet/notification/base_spec.rb +83 -0
  67. data/spec/bullet/notification/counter_cache_spec.rb +12 -0
  68. data/spec/bullet/notification/n_plus_one_query_spec.rb +14 -0
  69. data/spec/bullet/notification/unused_eager_loading_spec.rb +12 -0
  70. data/spec/bullet/notification_collector_spec.rb +32 -0
  71. data/spec/bullet/rack_spec.rb +97 -0
  72. data/spec/bullet/registry/association_spec.rb +26 -0
  73. data/spec/bullet/registry/base_spec.rb +44 -0
  74. data/spec/bullet/registry/object_spec.rb +24 -0
  75. data/spec/bullet_spec.rb +41 -0
  76. data/spec/integration/active_record3/association_spec.rb +651 -0
  77. data/spec/integration/active_record4/association_spec.rb +649 -0
  78. data/spec/integration/counter_cache_spec.rb +63 -0
  79. data/spec/integration/mongoid/association_spec.rb +258 -0
  80. data/spec/models/address.rb +3 -0
  81. data/spec/models/author.rb +3 -0
  82. data/spec/models/base_user.rb +5 -0
  83. data/spec/models/category.rb +7 -0
  84. data/spec/models/city.rb +3 -0
  85. data/spec/models/client.rb +4 -0
  86. data/spec/models/comment.rb +4 -0
  87. data/spec/models/company.rb +3 -0
  88. data/spec/models/country.rb +3 -0
  89. data/spec/models/document.rb +5 -0
  90. data/spec/models/entry.rb +3 -0
  91. data/spec/models/firm.rb +4 -0
  92. data/spec/models/folder.rb +2 -0
  93. data/spec/models/mongoid/address.rb +7 -0
  94. data/spec/models/mongoid/category.rb +8 -0
  95. data/spec/models/mongoid/comment.rb +7 -0
  96. data/spec/models/mongoid/company.rb +7 -0
  97. data/spec/models/mongoid/entry.rb +7 -0
  98. data/spec/models/mongoid/post.rb +12 -0
  99. data/spec/models/mongoid/user.rb +5 -0
  100. data/spec/models/newspaper.rb +3 -0
  101. data/spec/models/page.rb +2 -0
  102. data/spec/models/person.rb +3 -0
  103. data/spec/models/pet.rb +3 -0
  104. data/spec/models/post.rb +10 -0
  105. data/spec/models/relationship.rb +4 -0
  106. data/spec/models/student.rb +3 -0
  107. data/spec/models/submission.rb +4 -0
  108. data/spec/models/teacher.rb +3 -0
  109. data/spec/models/user.rb +4 -0
  110. data/spec/models/writer.rb +2 -0
  111. data/spec/spec_helper.rb +103 -0
  112. data/spec/support/bullet_ext.rb +55 -0
  113. data/spec/support/mongo_seed.rb +65 -0
  114. data/spec/support/rack_double.rb +55 -0
  115. data/spec/support/sqlite_seed.rb +229 -0
  116. data/tasks/bullet_tasks.rake +9 -0
  117. data/test.sh +15 -0
  118. metadata +246 -0
@@ -0,0 +1,649 @@
1
+ require 'spec_helper'
2
+
3
+ if !mongoid? && active_record4?
4
+ describe Bullet::Detector::Association, 'has_many' do
5
+ context "post => comments" do
6
+ it "should detect non preload post => comments" do
7
+ Post.all.each do |post|
8
+ post.comments.map(&:name)
9
+ end
10
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
11
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
12
+
13
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
14
+ end
15
+
16
+ it "should detect preload with post => comments" do
17
+ Post.includes(:comments).each do |post|
18
+ post.comments.map(&:name)
19
+ end
20
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
21
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
22
+
23
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
24
+ end
25
+
26
+ it "should detect unused preload post => comments" do
27
+ Post.includes(:comments).map(&:name)
28
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
29
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
30
+
31
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
32
+ end
33
+
34
+ it "should not detect unused preload post => comments" do
35
+ Post.all.map(&:name)
36
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
37
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
38
+
39
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
40
+ end
41
+
42
+ it "should detect non preload comment => post with inverse_of" do
43
+ Post.includes(:comments).each do |post|
44
+ post.comments.each do |comment|
45
+ comment.name
46
+ comment.post.name
47
+ end
48
+ end
49
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
50
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
51
+
52
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
53
+ end
54
+
55
+ it "should detect non preload post => comments with empty?" do
56
+ Post.all.each do |post|
57
+ post.comments.empty?
58
+ end
59
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
60
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
61
+
62
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
63
+ end
64
+ end
65
+
66
+ context "category => posts => comments" do
67
+ it "should detect non preload category => posts => comments" do
68
+ Category.all.each do |category|
69
+ category.posts.each do |post|
70
+ post.comments.map(&:name)
71
+ end
72
+ end
73
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
74
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
75
+
76
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :posts)
77
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
78
+ end
79
+
80
+ it "should detect preload category => posts, but no post => comments" do
81
+ Category.includes(:posts).each do |category|
82
+ category.posts.each do |post|
83
+ post.comments.map(&:name)
84
+ end
85
+ end
86
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
87
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
88
+
89
+ expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Category, :posts)
90
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
91
+ end
92
+
93
+ it "should detect preload with category => posts => comments" do
94
+ Category.includes({:posts => :comments}).each do |category|
95
+ category.posts.each do |post|
96
+ post.comments.map(&:name)
97
+ end
98
+ end
99
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
100
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
101
+
102
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
103
+ end
104
+
105
+ it "should detect preload with category => posts => comments with posts.id > 0" do
106
+ Category.includes({:posts => :comments}).where('posts.id > 0').references(:posts).each do |category|
107
+ category.posts.each do |post|
108
+ post.comments.map(&:name)
109
+ end
110
+ end
111
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
112
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
113
+
114
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
115
+ end
116
+
117
+ it "should detect unused preload with category => posts => comments" do
118
+ Category.includes({:posts => :comments}).map(&:name)
119
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
120
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
121
+
122
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
123
+ end
124
+
125
+ it "should detect unused preload with post => commnets, no category => posts" do
126
+ Category.includes({:posts => :comments}).each do |category|
127
+ category.posts.map(&:name)
128
+ end
129
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
130
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
131
+
132
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
133
+ end
134
+ end
135
+
136
+ context "category => posts, category => entries" do
137
+ it "should detect non preload with category => [posts, entries]" do
138
+ Category.all.each do |category|
139
+ category.posts.map(&:name)
140
+ category.entries.map(&:name)
141
+ end
142
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
143
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
144
+
145
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :posts)
146
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :entries)
147
+ end
148
+
149
+ it "should detect preload with category => posts, but not with category => entries" do
150
+ Category.includes(:posts).each do |category|
151
+ category.posts.map(&:name)
152
+ category.entries.map(&:name)
153
+ end
154
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
155
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
156
+
157
+ expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Category, :posts)
158
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :entries)
159
+ end
160
+
161
+ it "should detect preload with category => [posts, entries]" do
162
+ Category.includes([:posts, :entries]).each do |category|
163
+ category.posts.map(&:name)
164
+ category.entries.map(&:name)
165
+ end
166
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
167
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
168
+
169
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
170
+ end
171
+
172
+ it "should detect unused preload with category => [posts, entries]" do
173
+ Category.includes([:posts, :entries]).map(&:name)
174
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
175
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :posts)
176
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
177
+
178
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
179
+ end
180
+
181
+ it "should detect unused preload with category => entries, but not with category => posts" do
182
+ Category.includes([:posts, :entries]).each do |category|
183
+ category.posts.map(&:name)
184
+ end
185
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
186
+ expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
187
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
188
+
189
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
190
+ end
191
+ end
192
+
193
+ context "post => comment" do
194
+ it "should detect unused preload with post => comments" do
195
+ Post.includes(:comments).each do |post|
196
+ post.comments.first.name
197
+ end
198
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
199
+ expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :comments)
200
+
201
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
202
+ end
203
+
204
+ it "should detect preload with post => commnets" do
205
+ Post.first.comments.map(&:name)
206
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
207
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
208
+
209
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
210
+ end
211
+ end
212
+
213
+ context "category => posts => writer" do
214
+ it "should not detect unused preload associations" do
215
+ category = Category.includes({:posts => :writer}).order("id DESC").find_by_name('first')
216
+ category.posts.map do |post|
217
+ post.name
218
+ post.writer.name
219
+ end
220
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
221
+ expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
222
+ expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :writer)
223
+ end
224
+ end
225
+
226
+ context "scope for_category_name" do
227
+ it "should detect preload with post => category" do
228
+ Post.in_category_name('first').references(:categories).each do |post|
229
+ post.category.name
230
+ end
231
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
232
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
233
+
234
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
235
+ end
236
+
237
+ it "should not be unused preload post => category" do
238
+ Post.in_category_name('first').references(:categories).map(&:name)
239
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
240
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
241
+
242
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
243
+ end
244
+ end
245
+
246
+ context "scope preload_comments" do
247
+ it "should detect preload post => comments with scope" do
248
+ Post.preload_comments.each do |post|
249
+ post.comments.map(&:name)
250
+ end
251
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
252
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
253
+
254
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
255
+ end
256
+
257
+ it "should detect unused preload with scope" do
258
+ Post.preload_comments.map(&:name)
259
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
260
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
261
+
262
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
263
+ end
264
+ end
265
+ end
266
+
267
+ describe Bullet::Detector::Association, 'belongs_to' do
268
+ context "comment => post" do
269
+ it "should detect non preload with comment => post" do
270
+ Comment.all.each do |comment|
271
+ comment.post.name
272
+ end
273
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
274
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
275
+
276
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
277
+ end
278
+
279
+ it "should detect preload with one comment => post" do
280
+ Comment.first.post.name
281
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
282
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
283
+
284
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
285
+ end
286
+
287
+ it "should dtect preload with comment => post" do
288
+ Comment.includes(:post).each do |comment|
289
+ comment.post.name
290
+ end
291
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
292
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
293
+
294
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
295
+ end
296
+
297
+ it "should not detect preload with comment => post" do
298
+ Comment.all.map(&:name)
299
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
300
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
301
+
302
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
303
+ end
304
+
305
+ it "should detect unused preload with comments => post" do
306
+ Comment.includes(:post).map(&:name)
307
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
308
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Comment, :post)
309
+
310
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
311
+ end
312
+ end
313
+
314
+ context "comment => post => category" do
315
+ it "should detect non preload association with comment => post" do
316
+ Comment.all.each do |comment|
317
+ comment.post.category.name
318
+ end
319
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
320
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
321
+
322
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
323
+ end
324
+
325
+ it "should detect non preload association with post => category" do
326
+ Comment.includes(:post).each do |comment|
327
+ comment.post.category.name
328
+ end
329
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
330
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
331
+
332
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :category)
333
+ end
334
+
335
+ it "should not detect unpreload association" do
336
+ Comment.includes(:post => :category).each do |comment|
337
+ comment.post.category.name
338
+ end
339
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
340
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
341
+
342
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
343
+ end
344
+ end
345
+
346
+ context "comment => author, post => writer" do
347
+ it "should detect non preloaded writer" do
348
+ Comment.includes([:author, :post]).where(["base_users.id = ?", BaseUser.first]).references(:base_users).each do |comment|
349
+ comment.post.writer.name
350
+ end
351
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
352
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
353
+
354
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :writer)
355
+ end
356
+
357
+ it "should detect unused preload with comment => author" do
358
+ Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).references(:base_users).each do |comment|
359
+ comment.post.writer.name
360
+ end
361
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
362
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
363
+
364
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
365
+ end
366
+
367
+ it "should detect non preloading with writer => newspaper" do
368
+ Comment.includes(:post => :writer).where("posts.name like '%first%'").references(:posts).each do |comment|
369
+ comment.post.writer.newspaper.name
370
+ end
371
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
372
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
373
+
374
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Writer, :newspaper)
375
+ end
376
+
377
+ it "should not raise a stack error from posts to category" do
378
+ expect {
379
+ Comment.includes({:post => :category}).each do |com|
380
+ com.post.category
381
+ end
382
+ }.not_to raise_error()
383
+ end
384
+ end
385
+ end
386
+
387
+ describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
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::UnusedEagerLoading.check_unused_preload_associations
394
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
395
+
396
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
397
+ end
398
+
399
+ it "should detect preload associations" do
400
+ Student.includes(:teachers).each do |student|
401
+ student.teachers.map(&:name)
402
+ end
403
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
404
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
405
+
406
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
407
+ end
408
+
409
+ it "should detect unused preload associations" do
410
+ Student.includes(:teachers).map(&:name)
411
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
412
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Student, :teachers)
413
+
414
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
415
+ end
416
+
417
+ it "should detect no unused preload associations" do
418
+ Student.all.map(&:name)
419
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
420
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
421
+
422
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
423
+ end
424
+ end
425
+ end
426
+
427
+ describe Bullet::Detector::Association, 'has_many :through' do
428
+ context "firm => clients" do
429
+ it "should detect non preload associations" do
430
+ Firm.all.each do |firm|
431
+ firm.clients.map(&:name)
432
+ end
433
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
434
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
435
+
436
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Firm, :clients)
437
+ end
438
+
439
+ it "should detect preload associations" do
440
+ Firm.includes(:clients).each do |firm|
441
+ firm.clients.map(&:name)
442
+ end
443
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
444
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
445
+
446
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
447
+ end
448
+
449
+ it "should not detect preload associations" do
450
+ Firm.all.map(&:name)
451
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
452
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
453
+
454
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
455
+ end
456
+
457
+ it "should detect unused preload associations" do
458
+ Firm.includes(:clients).map(&:name)
459
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
460
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Firm, :clients)
461
+
462
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
463
+ end
464
+ end
465
+ end
466
+
467
+ describe Bullet::Detector::Association, "has_one" do
468
+ context "company => address" do
469
+ it "should detect non preload association" do
470
+ Company.all.each do |company|
471
+ company.address.name
472
+ end
473
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
474
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
475
+
476
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Company, :address)
477
+ end
478
+
479
+ it "should detect preload association" do
480
+ Company.includes(:address).each do |company|
481
+ company.address.name
482
+ end
483
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
484
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
485
+
486
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
487
+ end
488
+
489
+ it "should not detect preload association" do
490
+ Company.all.map(&:name)
491
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
492
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
493
+
494
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
495
+ end
496
+
497
+ it "should detect unused preload association" do
498
+ Company.includes(:address).map(&:name)
499
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
500
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Company, :address)
501
+
502
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
503
+ end
504
+ end
505
+ end
506
+
507
+ describe Bullet::Detector::Association, "call one association that in possible objects" do
508
+ it "should not detect preload association" do
509
+ Post.all
510
+ Post.first.comments.map(&:name)
511
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
512
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
513
+
514
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
515
+ end
516
+ end
517
+
518
+ describe Bullet::Detector::Association, "STI" do
519
+ context "page => author" do
520
+ it "should detect non preload associations" do
521
+ Page.all.each do |page|
522
+ page.author.name
523
+ end
524
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
525
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
526
+
527
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Page, :author)
528
+ end
529
+
530
+ it "should detect preload associations" do
531
+ Page.includes(:author).each do |page|
532
+ page.author.name
533
+ end
534
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
535
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
536
+
537
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
538
+ end
539
+
540
+ it "should detect unused preload associations" do
541
+ Page.includes(:author).map(&:name)
542
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
543
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Page, :author)
544
+
545
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
546
+ end
547
+
548
+ it "should not detect preload associations" do
549
+ Page.all.map(&:name)
550
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
551
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
552
+
553
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
554
+ end
555
+ end
556
+
557
+ context "disable n plus one query" do
558
+ before { Bullet.n_plus_one_query_enable = false }
559
+ after { Bullet.n_plus_one_query_enable = true }
560
+
561
+ it "should not detect n plus one query" do
562
+ Post.all.each do |post|
563
+ post.comments.map(&:name)
564
+ end
565
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
566
+
567
+ expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
568
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
569
+ end
570
+
571
+ it "should still detect unused eager loading" do
572
+ Post.includes(:comments).map(&:name)
573
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
574
+
575
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
576
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
577
+ end
578
+ end
579
+
580
+ context "disable unused eager loading" do
581
+ before { Bullet.unused_eager_loading_enable = false }
582
+ after { Bullet.unused_eager_loading_enable = true }
583
+
584
+ it "should not detect unused eager loading" do
585
+ Post.includes(:comments).map(&:name)
586
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
587
+
588
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
589
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
590
+ end
591
+
592
+ it "should still detect n plus one query" do
593
+ Post.all.each do |post|
594
+ post.comments.map(&:name)
595
+ end
596
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
597
+
598
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
599
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
600
+ end
601
+ end
602
+
603
+ context "whitelist n plus one query" do
604
+ before { Bullet.add_whitelist :type => :n_plus_one_query, :class_name => "Post", :association => :comments }
605
+ after { Bullet.reset_whitelist }
606
+
607
+ it "should not detect n plus one query" do
608
+ Post.all.each do |post|
609
+ post.comments.map(&:name)
610
+ end
611
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
612
+
613
+ expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
614
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
615
+ end
616
+
617
+ it "should still detect unused eager loading" do
618
+ Post.includes(:comments).map(&:name)
619
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
620
+
621
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
622
+ expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
623
+ end
624
+ end
625
+
626
+ context "whitelist unused eager loading" do
627
+ before { Bullet.add_whitelist :type => :unused_eager_loading, :class_name => "Post", :association => :comments }
628
+ after { Bullet.reset_whitelist }
629
+
630
+ it "should not detect unused eager loading" do
631
+ Post.includes(:comments).map(&:name)
632
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
633
+
634
+ expect(Bullet::Detector::Association).to be_completely_preloading_associations
635
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
636
+ end
637
+
638
+ it "should still detect n plus one query" do
639
+ Post.all.each do |post|
640
+ post.comments.map(&:name)
641
+ end
642
+ Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
643
+
644
+ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
645
+ expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
646
+ end
647
+ end
648
+ end
649
+ end