bullet 4.5.0 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,593 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if active_record2?
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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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::UnusedEagerLoading.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