bullet 7.0.3 → 7.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -1
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +7 -2
  5. data/lib/bullet/active_record4.rb +9 -0
  6. data/lib/bullet/active_record41.rb +9 -0
  7. data/lib/bullet/active_record42.rb +9 -0
  8. data/lib/bullet/active_record5.rb +24 -8
  9. data/lib/bullet/active_record52.rb +23 -7
  10. data/lib/bullet/active_record60.rb +22 -6
  11. data/lib/bullet/active_record61.rb +22 -6
  12. data/lib/bullet/active_record70.rb +43 -14
  13. data/lib/bullet/active_record71.rb +304 -0
  14. data/lib/bullet/dependency.rb +12 -0
  15. data/lib/bullet/detector/association.rb +8 -0
  16. data/lib/bullet/detector/n_plus_one_query.rb +17 -9
  17. data/lib/bullet/detector/unused_eager_loading.rb +6 -3
  18. data/lib/bullet/ext/object.rb +13 -3
  19. data/lib/bullet/mongoid7x.rb +8 -10
  20. data/lib/bullet/mongoid8x.rb +59 -0
  21. data/lib/bullet/notification/base.rb +9 -8
  22. data/lib/bullet/notification/counter_cache.rb +1 -1
  23. data/lib/bullet/rack.rb +41 -7
  24. data/lib/bullet/registry/association.rb +2 -1
  25. data/lib/bullet/registry/call_stack.rb +12 -0
  26. data/lib/bullet/registry.rb +1 -0
  27. data/lib/bullet/stack_trace_filter.rb +13 -10
  28. data/lib/bullet/version.rb +1 -1
  29. data/lib/bullet.rb +13 -3
  30. metadata +8 -153
  31. data/.github/workflows/main.yml +0 -82
  32. data/.gitignore +0 -15
  33. data/.rspec +0 -2
  34. data/Gemfile +0 -24
  35. data/Gemfile.mongoid +0 -12
  36. data/Gemfile.mongoid-4.0 +0 -15
  37. data/Gemfile.mongoid-5.0 +0 -15
  38. data/Gemfile.mongoid-6.0 +0 -15
  39. data/Gemfile.mongoid-7.0 +0 -15
  40. data/Gemfile.rails-4.0 +0 -16
  41. data/Gemfile.rails-4.1 +0 -16
  42. data/Gemfile.rails-4.2 +0 -16
  43. data/Gemfile.rails-5.0 +0 -15
  44. data/Gemfile.rails-5.1 +0 -15
  45. data/Gemfile.rails-5.2 +0 -15
  46. data/Gemfile.rails-6.0 +0 -15
  47. data/Gemfile.rails-6.1 +0 -15
  48. data/Gemfile.rails-7.0 +0 -10
  49. data/Guardfile +0 -8
  50. data/Hacking.md +0 -75
  51. data/Rakefile +0 -51
  52. data/bullet.gemspec +0 -33
  53. data/perf/benchmark.rb +0 -118
  54. data/rails/init.rb +0 -3
  55. data/spec/bullet/detector/association_spec.rb +0 -28
  56. data/spec/bullet/detector/base_spec.rb +0 -10
  57. data/spec/bullet/detector/counter_cache_spec.rb +0 -58
  58. data/spec/bullet/detector/n_plus_one_query_spec.rb +0 -182
  59. data/spec/bullet/detector/unused_eager_loading_spec.rb +0 -121
  60. data/spec/bullet/ext/object_spec.rb +0 -44
  61. data/spec/bullet/ext/string_spec.rb +0 -15
  62. data/spec/bullet/notification/base_spec.rb +0 -94
  63. data/spec/bullet/notification/counter_cache_spec.rb +0 -14
  64. data/spec/bullet/notification/n_plus_one_query_spec.rb +0 -29
  65. data/spec/bullet/notification/unused_eager_loading_spec.rb +0 -18
  66. data/spec/bullet/notification_collector_spec.rb +0 -34
  67. data/spec/bullet/rack_spec.rb +0 -279
  68. data/spec/bullet/registry/association_spec.rb +0 -28
  69. data/spec/bullet/registry/base_spec.rb +0 -46
  70. data/spec/bullet/registry/object_spec.rb +0 -26
  71. data/spec/bullet_spec.rb +0 -136
  72. data/spec/integration/active_record/association_spec.rb +0 -793
  73. data/spec/integration/counter_cache_spec.rb +0 -68
  74. data/spec/integration/mongoid/association_spec.rb +0 -246
  75. data/spec/models/address.rb +0 -5
  76. data/spec/models/attachment.rb +0 -5
  77. data/spec/models/author.rb +0 -5
  78. data/spec/models/base_user.rb +0 -7
  79. data/spec/models/category.rb +0 -12
  80. data/spec/models/city.rb +0 -5
  81. data/spec/models/client.rb +0 -8
  82. data/spec/models/comment.rb +0 -8
  83. data/spec/models/company.rb +0 -5
  84. data/spec/models/country.rb +0 -5
  85. data/spec/models/deal.rb +0 -5
  86. data/spec/models/document.rb +0 -7
  87. data/spec/models/entry.rb +0 -5
  88. data/spec/models/firm.rb +0 -7
  89. data/spec/models/folder.rb +0 -4
  90. data/spec/models/group.rb +0 -4
  91. data/spec/models/mongoid/address.rb +0 -9
  92. data/spec/models/mongoid/category.rb +0 -10
  93. data/spec/models/mongoid/comment.rb +0 -9
  94. data/spec/models/mongoid/company.rb +0 -9
  95. data/spec/models/mongoid/entry.rb +0 -9
  96. data/spec/models/mongoid/post.rb +0 -14
  97. data/spec/models/mongoid/user.rb +0 -7
  98. data/spec/models/newspaper.rb +0 -5
  99. data/spec/models/page.rb +0 -4
  100. data/spec/models/person.rb +0 -5
  101. data/spec/models/pet.rb +0 -5
  102. data/spec/models/post.rb +0 -34
  103. data/spec/models/relationship.rb +0 -6
  104. data/spec/models/reply.rb +0 -5
  105. data/spec/models/role.rb +0 -7
  106. data/spec/models/student.rb +0 -5
  107. data/spec/models/submission.rb +0 -7
  108. data/spec/models/teacher.rb +0 -5
  109. data/spec/models/user.rb +0 -8
  110. data/spec/models/writer.rb +0 -4
  111. data/spec/spec_helper.rb +0 -97
  112. data/spec/support/bullet_ext.rb +0 -56
  113. data/spec/support/mongo_seed.rb +0 -59
  114. data/spec/support/rack_double.rb +0 -49
  115. data/spec/support/sqlite_seed.rb +0 -284
  116. data/test.sh +0 -15
  117. data/update.sh +0 -10
@@ -1,793 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- if active_record?
6
- describe Bullet::Detector::Association, 'has_many' do
7
- context 'post => comments' do
8
- it 'should detect non preload post => comments' do
9
- Post.all.each { |post| post.comments.map(&:name) }
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 non preload post => comments for find_by_sql' do
17
- Post.find_by_sql('SELECT * FROM posts').each { |post| post.comments.map(&:name) }
18
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
19
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
20
-
21
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
22
- end
23
-
24
- it 'should detect preload with post => comments' do
25
- Post.includes(:comments).each { |post| post.comments.map(&:name) }
26
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
27
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
28
-
29
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
30
- end
31
-
32
- it 'should detect unused preload post => comments' do
33
- Post.includes(:comments).map(&:name)
34
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
35
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
36
-
37
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
38
- end
39
-
40
- it 'should not detect unused preload post => comments' do
41
- Post.all.map(&:name)
42
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
43
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
44
-
45
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
46
- end
47
-
48
- it 'should detect non preload comment => post with inverse_of' do
49
- Post.includes(:comments).each do |post|
50
- post.comments.each do |comment|
51
- comment.name
52
- comment.post.name
53
- end
54
- end
55
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
56
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
57
-
58
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
59
- end
60
-
61
- it 'should detect non preload post => comments with empty?' do
62
- Post.all.each { |post| post.comments.empty? }
63
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
64
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
65
-
66
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
67
- end
68
-
69
- it 'should detect non preload post => comments with include?' do
70
- comment = Comment.last
71
- Post.all.each { |post| post.comments.include?(comment) }
72
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
73
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
74
-
75
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
76
- end
77
-
78
- it 'should not detect unused preload person => pets with empty?' do
79
- Person.all.each { |person| person.pets.empty? }
80
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
81
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
82
-
83
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
84
- end
85
- end
86
-
87
- context 'category => posts => comments' do
88
- it 'should detect non preload category => posts => comments' do
89
- Category.all.each { |category| category.posts.each { |post| post.comments.map(&:name) } }
90
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
91
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
92
-
93
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :posts)
94
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
95
- end
96
-
97
- it 'should detect preload category => posts, but no post => comments' do
98
- Category.includes(:posts).each { |category| category.posts.each { |post| post.comments.map(&:name) } }
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).not_to be_detecting_unpreloaded_association_for(Category, :posts)
103
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
104
- end
105
-
106
- it 'should detect preload with category => posts => comments' do
107
- Category.includes(posts: :comments).each { |category| category.posts.each { |post| post.comments.map(&:name) } }
108
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
109
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
110
-
111
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
112
- end
113
-
114
- it 'should detect preload with category => posts => comments with posts.id > 0' do
115
- Category.includes(posts: :comments).where('posts.id > 0').references(:posts).each do |category|
116
- category.posts.each { |post| post.comments.map(&:name) }
117
- end
118
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
119
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
120
-
121
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
122
- end
123
-
124
- it 'should detect unused preload with category => posts => comments' do
125
- Category.includes(posts: :comments).map(&:name)
126
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
127
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
128
-
129
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
130
- end
131
-
132
- it 'should detect unused preload with post => comments, no category => posts' do
133
- Category.includes(posts: :comments).each { |category| category.posts.map(&:name) }
134
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
135
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
136
-
137
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
138
- end
139
- end
140
-
141
- context 'category => posts, category => entries' do
142
- it 'should detect non preload with category => [posts, entries]' do
143
- Category.all.each do |category|
144
- category.posts.map(&:name)
145
- category.entries.map(&:name)
146
- end
147
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
148
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
149
-
150
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :posts)
151
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :entries)
152
- end
153
-
154
- it 'should detect preload with category => posts, but not with category => entries' do
155
- Category.includes(:posts).each do |category|
156
- category.posts.map(&:name)
157
- category.entries.map(&:name)
158
- end
159
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
160
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
161
-
162
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Category, :posts)
163
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Category, :entries)
164
- end
165
-
166
- it 'should detect preload with category => [posts, entries]' do
167
- Category.includes(%i[posts entries]).each do |category|
168
- category.posts.map(&:name)
169
- category.entries.map(&:name)
170
- end
171
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
172
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
173
-
174
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
175
- end
176
-
177
- it 'should detect unused preload with category => [posts, entries]' do
178
- Category.includes(%i[posts entries]).map(&:name)
179
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
180
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :posts)
181
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
182
-
183
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
184
- end
185
-
186
- it 'should detect unused preload with category => entries, but not with category => posts' do
187
- Category.includes(%i[posts entries]).each { |category| category.posts.map(&:name) }
188
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
189
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
190
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Category, :entries)
191
-
192
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
193
- end
194
- end
195
-
196
- context 'post => comment' do
197
- it 'should detect unused preload with post => comments' do
198
- Post.includes(:comments).each { |post| post.comments.first&.name }
199
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
200
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :comments)
201
-
202
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
203
- end
204
-
205
- it 'should detect preload with post => comments' do
206
- Post.first.comments.map(&:name)
207
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
208
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
209
-
210
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
211
- end
212
-
213
- it 'should not detect unused preload with category => posts' do
214
- category = Category.first
215
- category.draft_post.destroy!
216
- post = category.draft_post
217
- post.update!(link: true)
218
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
219
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
220
-
221
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
222
-
223
- Support::SqliteSeed.setup_db
224
- Support::SqliteSeed.seed_db
225
- end
226
- end
227
-
228
- context 'category => posts => writer' do
229
- it 'should not detect unused preload associations' do
230
- category = Category.includes(posts: :writer).order('id DESC').find_by_name('first')
231
- category.posts.map do |post|
232
- post.name
233
- post.writer.name
234
- end
235
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
236
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Category, :posts)
237
- expect(Bullet::Detector::Association).not_to be_unused_preload_associations_for(Post, :writer)
238
- end
239
- end
240
-
241
- context 'scope for_category_name' do
242
- it 'should detect preload with post => category' do
243
- Post.in_category_name('first').references(:categories).each { |post| post.category.name }
244
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
245
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
246
-
247
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
248
- end
249
-
250
- it 'should not be unused preload post => category' do
251
- Post.in_category_name('first').references(:categories).map(&:name)
252
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
253
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
254
-
255
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
256
- end
257
- end
258
-
259
- context 'scope preload_comments' do
260
- it 'should detect preload post => comments with scope' do
261
- Post.preload_comments.each { |post| post.comments.map(&:name) }
262
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
263
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
264
-
265
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
266
- end
267
-
268
- it 'should detect unused preload with scope' do
269
- Post.preload_comments.map(&:name)
270
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
271
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
272
-
273
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
274
- end
275
- end
276
- end
277
-
278
- describe Bullet::Detector::Association, 'belongs_to' do
279
- context 'comment => post' do
280
- it 'should detect non preload with comment => post' do
281
- Comment.all.each { |comment| comment.post.name }
282
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
283
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
284
-
285
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
286
- end
287
-
288
- it 'should detect preload with one comment => post' do
289
- Comment.first.post.name
290
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
291
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
292
-
293
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
294
- end
295
-
296
- it 'should detect preload with comment => post' do
297
- Comment.includes(:post).each { |comment| comment.post.name }
298
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
299
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
300
-
301
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
302
- end
303
-
304
- it 'should not detect preload with comment => post' do
305
- Comment.all.map(&:name)
306
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
307
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
308
-
309
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
310
- end
311
-
312
- it 'should detect unused preload with comment => post' do
313
- Comment.includes(:post).map(&:name)
314
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
315
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Comment, :post)
316
-
317
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
318
- end
319
-
320
- it 'should not detect newly assigned object in an after_save' do
321
- new_post = Post.new(category: Category.first)
322
-
323
- new_post.trigger_after_save = true
324
- new_post.save!
325
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
326
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
327
-
328
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
329
- end
330
- end
331
-
332
- context 'comment => post => category' do
333
- it 'should detect non preload association with comment => post' do
334
- Comment.all.each { |comment| comment.post.category.name }
335
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
336
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
337
-
338
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Comment, :post)
339
- end
340
-
341
- it 'should not detect non preload association with only one comment' do
342
- Comment.first.post.category.name
343
-
344
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
345
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
346
-
347
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
348
- end
349
-
350
- it 'should detect non preload association with post => category' do
351
- Comment.includes(:post).each { |comment| comment.post.category.name }
352
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
353
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
354
-
355
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :category)
356
- end
357
-
358
- it 'should not detect unpreload association' do
359
- Comment.includes(post: :category).each { |comment| comment.post.category.name }
360
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
361
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
362
-
363
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
364
- end
365
- end
366
-
367
- context 'comment => author, post => writer' do
368
- it 'should detect non preloaded writer' do
369
- Comment.includes(%i[author post]).where(['base_users.id = ?', BaseUser.first]).references(:base_users)
370
- .each { |comment| comment.post.writer.name }
371
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
372
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
373
-
374
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :writer)
375
- end
376
-
377
- it 'should detect unused preload with comment => author' do
378
- Comment.includes([:author, { post: :writer }]).where(['base_users.id = ?', BaseUser.first]).references(
379
- :base_users
380
- ).each { |comment| comment.post.writer.name }
381
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
382
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
383
-
384
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
385
- end
386
-
387
- it 'should detect non preloading with writer => newspaper' do
388
- Comment.includes(post: :writer).where("posts.name like '%first%'").references(:posts).each do |comment|
389
- comment.post.writer.newspaper.name
390
- end
391
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
392
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
393
-
394
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Writer, :newspaper)
395
- end
396
-
397
- it 'should not raise a stack error from posts to category' do
398
- expect { Comment.includes(post: :category).each { |com| com.post.category } }.not_to raise_error
399
- end
400
- end
401
- end
402
-
403
- describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
404
- context 'posts <=> deals' do
405
- it 'should detect preload associations with join tables that have identifier' do
406
- Post.includes(:deals).each { |post| post.deals.map(&:name) }
407
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
408
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
409
-
410
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
411
- end
412
- end
413
- context 'students <=> teachers' do
414
- it 'should detect non preload associations' do
415
- Student.all.each { |student| student.teachers.map(&:name) }
416
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
417
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
418
-
419
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
420
- end
421
-
422
- it 'should detect preload associations' do
423
- Student.includes(:teachers).each { |student| student.teachers.map(&:name) }
424
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
425
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
426
-
427
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
428
- end
429
-
430
- it 'should detect unused preload associations' do
431
- Student.includes(:teachers).map(&:name)
432
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
433
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Student, :teachers)
434
-
435
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
436
- end
437
-
438
- it 'should detect no unused preload associations' do
439
- Student.all.map(&:name)
440
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
441
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
442
-
443
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
444
- end
445
-
446
- it 'should detect non preload student => teachers with empty?' do
447
- Student.all.each { |student| student.teachers.empty? }
448
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
449
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
450
-
451
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Student, :teachers)
452
- end
453
- end
454
-
455
- context 'user => roles' do
456
- it 'should detect preload associations' do
457
- User.first.roles.includes(:resource).each { |role| role.resource }
458
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
459
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
460
-
461
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
462
- end
463
- end
464
- end
465
-
466
- describe Bullet::Detector::Association, 'has_many :through' do
467
- context 'firm => clients' do
468
- it 'should detect non preload associations' do
469
- Firm.all.each { |firm| firm.clients.map(&:name) }
470
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
471
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
472
-
473
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Firm, :clients)
474
- end
475
-
476
- it 'should detect preload associations' do
477
- Firm.includes(:clients).each { |firm| firm.clients.map(&:name) }
478
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
479
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
480
-
481
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
482
- end
483
-
484
- it 'should not detect preload associations' do
485
- Firm.all.map(&:name)
486
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
487
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
488
-
489
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
490
- end
491
-
492
- it 'should detect unused preload associations' do
493
- Firm.includes(:clients).map(&:name)
494
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
495
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Firm, :clients)
496
-
497
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
498
- end
499
- end
500
-
501
- context 'firm => clients => groups' do
502
- it 'should detect non preload associations' do
503
- Firm.all.each { |firm| firm.groups.map(&:name) }
504
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
505
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
506
-
507
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Firm, :groups)
508
- end
509
-
510
- it 'should detect preload associations' do
511
- Firm.includes(:groups).each { |firm| firm.groups.map(&:name) }
512
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
513
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
514
-
515
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
516
- end
517
-
518
- it 'should not detect preload associations' do
519
- Firm.all.map(&:name)
520
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
521
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
522
-
523
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
524
- end
525
-
526
- it 'should detect unused preload associations' do
527
- Firm.includes(:groups).map(&:name)
528
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
529
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Firm, :groups)
530
-
531
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
532
- end
533
- end
534
- end
535
-
536
- describe Bullet::Detector::Association, 'has_one' do
537
- context 'company => address' do
538
- it 'should detect non preload association' do
539
- Company.all.each { |company| company.address.name }
540
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
541
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
542
-
543
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Company, :address)
544
- end
545
-
546
- it 'should detect preload association' do
547
- Company.includes(:address).each { |company| company.address.name }
548
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
549
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
550
-
551
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
552
- end
553
-
554
- it 'should not detect preload association' do
555
- Company.all.map(&:name)
556
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
557
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
558
-
559
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
560
- end
561
-
562
- it 'should detect unused preload association' do
563
- Company.includes(:address).map(&:name)
564
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
565
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Company, :address)
566
-
567
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
568
- end
569
- end
570
- end
571
-
572
- describe Bullet::Detector::Association, 'has_one => has_many' do
573
- it 'should not detect preload association' do
574
- user = User.first
575
- user.submission.replies.map(&:name)
576
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
577
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
578
-
579
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
580
- end
581
- end
582
-
583
- describe Bullet::Detector::Association, 'has_one :through' do
584
- context 'user => attachment' do
585
- it 'should detect non preload associations' do
586
- User.all.each { |user| user.submission_attachment.file_name }
587
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
588
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
589
-
590
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(User, :submission_attachment)
591
- end
592
-
593
- it 'should detect preload associations' do
594
- User.includes(:submission_attachment).each { |user| user.submission_attachment.file_name }
595
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
596
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
597
-
598
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
599
- end
600
-
601
- it 'should not detect preload associations' do
602
- User.all.map(&:name)
603
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
604
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
605
-
606
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
607
- end
608
-
609
- it 'should detect unused preload associations' do
610
- User.includes(:submission_attachment).map(&:name)
611
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
612
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(User, :submission_attachment)
613
-
614
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
615
- end
616
- end
617
- end
618
-
619
- describe Bullet::Detector::Association, 'call one association that in possible objects' do
620
- it 'should not detect preload association' do
621
- Post.all
622
- Post.first.comments.map(&:name)
623
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
624
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
625
-
626
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
627
- end
628
- end
629
-
630
- describe Bullet::Detector::Association, 'query immediately after creation' do
631
- context 'with save' do
632
- context 'document => children' do
633
- it 'should not detect non preload associations' do
634
- document1 = Document.new
635
- document1.children.build
636
- document1.save
637
-
638
- document2 = Document.new(parent: document1)
639
- document2.save
640
- document2.parent
641
-
642
- document1.children.each.first
643
-
644
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
645
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
646
-
647
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
648
- end
649
- end
650
- end
651
-
652
- context 'with save!' do
653
- context 'document => children' do
654
- it 'should not detect non preload associations' do
655
- document1 = Document.new
656
- document1.children.build
657
- document1.save!
658
-
659
- document2 = Document.new(parent: document1)
660
- document2.save!
661
- document2.parent
662
-
663
- document1.children.each.first
664
-
665
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
666
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
667
-
668
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
669
- end
670
- end
671
- end
672
- end
673
-
674
- describe Bullet::Detector::Association, 'STI' do
675
- context 'page => author' do
676
- it 'should detect non preload associations' do
677
- Page.all.each { |page| page.author.name }
678
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
679
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
680
-
681
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Page, :author)
682
- end
683
-
684
- it 'should detect preload associations' do
685
- Page.includes(:author).each { |page| page.author.name }
686
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
687
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
688
-
689
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
690
- end
691
-
692
- it 'should detect unused preload associations' do
693
- Page.includes(:author).map(&:name)
694
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
695
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Page, :author)
696
-
697
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
698
- end
699
-
700
- it 'should not detect preload associations' do
701
- Page.all.map(&:name)
702
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
703
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
704
-
705
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
706
- end
707
- end
708
-
709
- context 'disable n plus one query' do
710
- before { Bullet.n_plus_one_query_enable = false }
711
- after { Bullet.n_plus_one_query_enable = true }
712
-
713
- it 'should not detect n plus one query' do
714
- Post.all.each { |post| post.comments.map(&:name) }
715
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
716
-
717
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
718
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
719
- end
720
-
721
- it 'should still detect unused eager loading' do
722
- Post.includes(:comments).map(&:name)
723
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
724
-
725
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
726
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
727
- end
728
- end
729
-
730
- context 'disable unused eager loading' do
731
- before { Bullet.unused_eager_loading_enable = false }
732
- after { Bullet.unused_eager_loading_enable = true }
733
-
734
- it 'should not detect unused eager loading' do
735
- Post.includes(:comments).map(&:name)
736
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
737
-
738
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
739
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
740
- end
741
-
742
- it 'should still detect n plus one query' do
743
- Post.all.each { |post| post.comments.map(&:name) }
744
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
745
-
746
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
747
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
748
- end
749
- end
750
-
751
- context 'add n plus one query to safelist' do
752
- before { Bullet.add_safelist type: :n_plus_one_query, class_name: 'Post', association: :comments }
753
- after { Bullet.clear_safelist }
754
-
755
- it 'should not detect n plus one query' do
756
- Post.all.each { |post| post.comments.map(&:name) }
757
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
758
-
759
- expect(Bullet::Detector::Association).not_to be_detecting_unpreloaded_association_for(Post, :comments)
760
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
761
- end
762
-
763
- it 'should still detect unused eager loading' do
764
- Post.includes(:comments).map(&:name)
765
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
766
-
767
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
768
- expect(Bullet::Detector::Association).to be_unused_preload_associations_for(Post, :comments)
769
- end
770
- end
771
-
772
- context 'add unused eager loading to safelist' do
773
- before { Bullet.add_safelist type: :unused_eager_loading, class_name: 'Post', association: :comments }
774
- after { Bullet.clear_safelist }
775
-
776
- it 'should not detect unused eager loading' do
777
- Post.includes(:comments).map(&:name)
778
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
779
-
780
- expect(Bullet::Detector::Association).to be_completely_preloading_associations
781
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
782
- end
783
-
784
- it 'should still detect n plus one query' do
785
- Post.all.each { |post| post.comments.map(&:name) }
786
- Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
787
-
788
- expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(Post, :comments)
789
- expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations
790
- end
791
- end
792
- end
793
- end