flyerhzm-bullet 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,679 @@
1
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
2
+
3
+ describe Bullet::Association, 'has_many' do
4
+ def setup_db
5
+ ActiveRecord::Schema.define(:version => 1) do
6
+ create_table :categories do |t|
7
+ t.column :name, :string
8
+ end
9
+
10
+ create_table :posts do |t|
11
+ t.column :name, :string
12
+ t.column :category_id, :integer
13
+ end
14
+
15
+ create_table :comments do |t|
16
+ t.column :name, :string
17
+ t.column :post_id, :integer
18
+ end
19
+
20
+ create_table :entries do |t|
21
+ t.column :name, :string
22
+ t.column :category_id, :integer
23
+ end
24
+ end
25
+ end
26
+
27
+ def teardown_db
28
+ ActiveRecord::Base.connection.tables.each do |table|
29
+ ActiveRecord::Base.connection.drop_table(table)
30
+ end
31
+ end
32
+
33
+ class Category < ActiveRecord::Base
34
+ has_many :posts
35
+ has_many :entries
36
+ end
37
+
38
+ class Post < ActiveRecord::Base
39
+ belongs_to :category
40
+ has_many :comments
41
+ end
42
+
43
+ class Entry < ActiveRecord::Base
44
+ belongs_to :category
45
+ end
46
+
47
+ class Comment < ActiveRecord::Base
48
+ belongs_to :post
49
+ end
50
+
51
+ before(:all) do
52
+ setup_db
53
+
54
+ category1 = Category.create(:name => 'first')
55
+ category2 = Category.create(:name => 'second')
56
+
57
+ post1 = category1.posts.create(:name => 'first')
58
+ post2 = category1.posts.create(:name => 'second')
59
+ post3 = category2.posts.create(:name => 'third')
60
+ post4 = category2.posts.create(:name => 'fourth')
61
+
62
+ comment1 = post1.comments.create(:name => 'first')
63
+ comment2 = post1.comments.create(:name => 'second')
64
+ comment3 = post2.comments.create(:name => 'third')
65
+ comment4 = post2.comments.create(:name => 'fourth')
66
+
67
+ entry1 = category1.entries.create(:name => 'first')
68
+ entry2 = category1.entries.create(:name => 'second')
69
+ end
70
+
71
+ after(:all) do
72
+ teardown_db
73
+ end
74
+
75
+ before(:each) do
76
+ Bullet::Association.start_request
77
+ end
78
+
79
+ after(:each) do
80
+ Bullet::Association.end_request
81
+ end
82
+
83
+ context "post => comments" do
84
+ it "should detect preload with post => comments" do
85
+ Post.find(:all, :include => :comments).each do |post|
86
+ post.comments.collect(&:name)
87
+ end
88
+ Bullet::Association.should_not be_has_unpreload_associations
89
+ end
90
+
91
+ it "should detect no preload post => comments" do
92
+ Post.find(:all).each do |post|
93
+ post.comments.collect(&:name)
94
+ end
95
+ Bullet::Association.should be_has_unpreload_associations
96
+ end
97
+
98
+ it "should detect unused preload post => comments" do
99
+ Post.find(:all, :include => :comments).collect(&:name)
100
+ Bullet::Association.check_unused_preload_associations
101
+ Bullet::Association.should be_has_unused_preload_associations
102
+ end
103
+
104
+ it "should no detect unused preload post => comments" do
105
+ Post.find(:all).collect(&:name)
106
+ Bullet::Association.check_unused_preload_associations
107
+ Bullet::Association.should_not be_has_unused_preload_associations
108
+ end
109
+ end
110
+
111
+ context "category => posts => comments" do
112
+ it "should detect preload with category => posts => comments" do
113
+ Category.find(:all, :include => {:posts => :comments}) do |category|
114
+ category.posts.each do |post|
115
+ post.comments.collect(&:name)
116
+ end
117
+ end
118
+ Bullet::Association.should_not be_has_unpreload_associations
119
+ end
120
+
121
+ it "should detect preload category => posts, but no post => comments" do
122
+ Category.find(:all, :include => :posts).each do |category|
123
+ category.posts.each do |post|
124
+ post.comments.collect(&:name)
125
+ end
126
+ end
127
+ Bullet::Association.should be_has_unpreload_associations
128
+ end
129
+
130
+ it "should detect no preload category => posts => comments" do
131
+ Category.find(:all).each do |category|
132
+ category.posts.each do |post|
133
+ post.comments.collect(&:name)
134
+ end
135
+ end
136
+ Bullet::Association.should be_has_unpreload_associations
137
+ end
138
+
139
+ it "should detect unused preload with category => posts => comments" do
140
+ Category.find(:all, :include => {:posts => :comments}).collect(&:name)
141
+ Bullet::Association.check_unused_preload_associations
142
+ Bullet::Association.should be_has_unused_preload_associations
143
+ end
144
+
145
+ it "should detect unused preload with post => commnets, no category => posts" do
146
+ Category.find(:all, :include => {:posts => :comments}).each do |category|
147
+ category.posts.collect(&:name)
148
+ end
149
+ Bullet::Association.check_unused_preload_associations
150
+ Bullet::Association.should be_has_unused_preload_associations
151
+ end
152
+
153
+ it "should no detect preload with category => posts => comments" do
154
+ Category.find(:all).each do |category|
155
+ category.posts.each do |post|
156
+ post.comments.collect(&:name)
157
+ end
158
+ end
159
+ Bullet::Association.check_unused_preload_associations
160
+ Bullet::Association.should_not be_has_unused_preload_associations
161
+ end
162
+ end
163
+
164
+ context "category => posts, category => entries" do
165
+ it "should detect preload with category => [posts, entries]" do
166
+ Category.find(:all, :include => [:posts, :entries]).each do |category|
167
+ category.posts.collect(&:name)
168
+ category.entries.collect(&:name)
169
+ end
170
+ Bullet::Association.should_not be_has_unpreload_associations
171
+ end
172
+
173
+ it "should detect preload with category => posts, but no category => entries" do
174
+ Category.find(:all, :include => :posts).each do |category|
175
+ category.posts.collect(&:name)
176
+ category.entries.collect(&:name)
177
+ end
178
+ Bullet::Association.should be_has_unpreload_associations
179
+ end
180
+
181
+ it "should detect no preload with category => [posts, entries]" do
182
+ Category.find(:all).each do |category|
183
+ category.posts.collect(&:name)
184
+ category.entries.collect(&:name)
185
+ end
186
+ Bullet::Association.should be_has_unpreload_associations
187
+ end
188
+
189
+ it "should detect unused with category => [posts, entries]" do
190
+ Category.find(:all, :include => [:posts, :entries]).collect(&:name)
191
+ Bullet::Association.check_unused_preload_associations
192
+ Bullet::Association.should be_has_unused_preload_associations
193
+ end
194
+
195
+ it "should detect unused preload with category => entries, but no category => posts" do
196
+ Category.find(:all, :include => [:posts, :entries]).each do |category|
197
+ category.posts.collect(&:name)
198
+ end
199
+ Bullet::Association.check_unused_preload_associations
200
+ Bullet::Association.should be_has_unused_preload_associations
201
+ end
202
+
203
+ it "should detect no unused preload" do
204
+ Category.find(:all).each do |category|
205
+ category.posts.collect(&:name)
206
+ category.entries.collect(&:name)
207
+ end
208
+ Bullet::Association.check_unused_preload_associations
209
+ Bullet::Association.should_not be_has_unused_preload_associations
210
+ end
211
+ end
212
+
213
+ context "no preload" do
214
+ it "should no preload only one post => commnets" do
215
+ Post.first.comments.collect(&:name)
216
+ Bullet::Association.should_not be_has_unpreload_associations
217
+ end
218
+ end
219
+
220
+ context "belongs_to" do
221
+ it "should preload comments => post" do
222
+ Comment.find(:all).each do |comment|
223
+ comment.post.name
224
+ end
225
+ Bullet::Association.should be_has_unpreload_associations
226
+ end
227
+
228
+ it "should no preload comment => post" do
229
+ Comment.first.post.name
230
+ Bullet::Association.should_not be_has_unpreload_associations
231
+ end
232
+
233
+ it "should no preload comments => post" do
234
+ Comment.find(:all, :include => :post).each do |comment|
235
+ comment.post.name
236
+ end
237
+ Bullet::Association.should_not be_has_unpreload_associations
238
+ end
239
+
240
+ it "should detect no unused preload comments => post" do
241
+ Comment.find(:all).collect(&:name)
242
+ Bullet::Association.check_unused_preload_associations
243
+ Bullet::Association.should_not be_has_unused_preload_associations
244
+ end
245
+
246
+ it "should detect unused preload comments => post" do
247
+ Comment.find(:all, :include => :post).collect(&:name)
248
+ Bullet::Association.check_unused_preload_associations
249
+ Bullet::Association.should be_has_unused_preload_associations
250
+ end
251
+ end
252
+ end
253
+
254
+ describe Bullet::Association, 'has_and_belongs_to_many' do
255
+ def setup_db
256
+ ActiveRecord::Schema.define(:version => 1) do
257
+ create_table :students do |t|
258
+ t.column :name, :string
259
+ end
260
+
261
+ create_table :teachers do |t|
262
+ t.column :name, :string
263
+ end
264
+
265
+ create_table :students_teachers, :id => false do |t|
266
+ t.column :student_id, :integer
267
+ t.column :teacher_id, :integer
268
+ end
269
+ end
270
+ end
271
+
272
+ def teardown_db
273
+ ActiveRecord::Base.connection.tables.each do |table|
274
+ ActiveRecord::Base.connection.drop_table(table)
275
+ end
276
+ end
277
+
278
+ class Student < ActiveRecord::Base
279
+ has_and_belongs_to_many :teachers
280
+ end
281
+
282
+ class Teacher < ActiveRecord::Base
283
+ has_and_belongs_to_many :students
284
+ end
285
+
286
+ before(:all) do
287
+ setup_db
288
+ student1 = Student.create(:name => 'first')
289
+ student2 = Student.create(:name => 'second')
290
+ teacher1 = Teacher.create(:name => 'first')
291
+ teacher2 = Teacher.create(:name => 'second')
292
+ student1.teachers = [teacher1, teacher2]
293
+ student2.teachers = [teacher1, teacher2]
294
+ teacher1.students << student1
295
+ teacher2.students << student2
296
+ end
297
+
298
+ after(:all) do
299
+ teardown_db
300
+ end
301
+
302
+ before(:each) do
303
+ Bullet::Association.start_request
304
+ end
305
+
306
+ after(:each) do
307
+ Bullet::Association.end_request
308
+ end
309
+
310
+ it "should detect unpreload associatoins" do
311
+ Student.find(:all).each do |student|
312
+ student.teachers.collect(&:name)
313
+ end
314
+ Bullet::Association.should be_has_unpreload_associations
315
+ end
316
+
317
+ it "should detect no unpreload associatoins" do
318
+ Student.find(:all, :include => :teachers).each do |student|
319
+ student.teachers.collect(&:name)
320
+ end
321
+ Bullet::Association.should_not be_has_unpreload_associations
322
+ end
323
+
324
+ it "should detect unused preload associatoins" do
325
+ Student.find(:all, :include => :teachers).collect(&:name)
326
+ Bullet::Association.check_unused_preload_associations
327
+ Bullet::Association.should be_has_unused_preload_associations
328
+ end
329
+
330
+ it "should detect no unused preload associatoins" do
331
+ Student.find(:all).collect(&:name)
332
+ Bullet::Association.check_unused_preload_associations
333
+ Bullet::Association.should_not be_has_unused_preload_associations
334
+ end
335
+ end
336
+
337
+ describe Bullet::Association, 'has_many :through' do
338
+ def setup_db
339
+ ActiveRecord::Schema.define(:version => 1) do
340
+ create_table :firms do |t|
341
+ t.column :name, :string
342
+ end
343
+
344
+ create_table :clients do |t|
345
+ t.column :name, :string
346
+ end
347
+
348
+ create_table :relations do |t|
349
+ t.column :firm_id, :integer
350
+ t.column :client_id, :integer
351
+ end
352
+ end
353
+ end
354
+
355
+ def teardown_db
356
+ ActiveRecord::Base.connection.tables.each do |table|
357
+ ActiveRecord::Base.connection.drop_table(table)
358
+ end
359
+ end
360
+
361
+ class Firm < ActiveRecord::Base
362
+ has_many :relations
363
+ has_many :clients, :through => :relations
364
+ end
365
+
366
+ class Client < ActiveRecord::Base
367
+ has_many :relations
368
+ has_many :firms, :through => :relations
369
+ end
370
+
371
+ class Relation < ActiveRecord::Base
372
+ belongs_to :firm
373
+ belongs_to :client
374
+ end
375
+
376
+ before(:all) do
377
+ setup_db
378
+ firm1 = Firm.create(:name => 'first')
379
+ firm2 = Firm.create(:name => 'second')
380
+ client1 = Client.create(:name => 'first')
381
+ client2 = Client.create(:name => 'second')
382
+ firm1.clients = [client1, client2]
383
+ firm2.clients = [client1, client2]
384
+ client1.firms << firm1
385
+ client2.firms << firm2
386
+ end
387
+
388
+ after(:all) do
389
+ teardown_db
390
+ end
391
+
392
+ before(:each) do
393
+ Bullet::Association.start_request
394
+ end
395
+
396
+ after(:each) do
397
+ Bullet::Association.end_request
398
+ end
399
+
400
+ it "should detect unpreload associatoins" do
401
+ Firm.find(:all).each do |firm|
402
+ firm.clients.collect(&:name)
403
+ end
404
+ Bullet::Association.should be_has_unpreload_associations
405
+ end
406
+
407
+ it "should detect no unpreload associatoins" do
408
+ Firm.find(:all, :include => :clients).each do |firm|
409
+ firm.clients.collect(&:name)
410
+ end
411
+ Bullet::Association.should_not be_has_unpreload_associations
412
+ end
413
+
414
+ it "should detect no unused preload associatoins" do
415
+ Firm.find(:all).collect(&:name)
416
+ Bullet::Association.check_unused_preload_associations
417
+ Bullet::Association.should_not be_has_unused_preload_associations
418
+ end
419
+
420
+ it "should detect unused preload associatoins" do
421
+ Firm.find(:all, :include => :clients).collect(&:name)
422
+ Bullet::Association.check_unused_preload_associations
423
+ Bullet::Association.should be_has_unused_preload_associations
424
+ end
425
+ end
426
+
427
+ describe Bullet::Association, 'has_many :as' do
428
+ def setup_db
429
+ ActiveRecord::Schema.define(:version => 1) do
430
+ create_table :votes do |t|
431
+ t.column :vote, :integer
432
+ t.references :voteable, :polymorphic => true
433
+ end
434
+
435
+ create_table :users do |t|
436
+ t.column :name, :string
437
+ end
438
+
439
+ create_table :news do |t|
440
+ t.column :name, :string
441
+ end
442
+ end
443
+ end
444
+
445
+ def teardown_db
446
+ ActiveRecord::Base.connection.tables.each do |table|
447
+ ActiveRecord::Base.connection.drop_table(table)
448
+ end
449
+ end
450
+
451
+ class Vote < ActiveRecord::Base
452
+ belongs_to :voteable, :polymorphic => true
453
+ end
454
+
455
+ class User < ActiveRecord::Base
456
+ has_many :votes, :as => :voteable
457
+ end
458
+
459
+ class News < ActiveRecord::Base
460
+ has_many :votes, :as => :voteable
461
+ end
462
+
463
+ before(:all) do
464
+ setup_db
465
+ user1 = User.create(:name => 'first')
466
+ user2 = User.create(:name => 'second')
467
+ user1.votes << Vote.create(:vote => 10)
468
+ user1.votes << Vote.create(:vote => 20)
469
+ user2.votes << Vote.create(:vote => 10)
470
+ user2.votes << Vote.create(:vote => 20)
471
+
472
+ news1 = News.create(:name => 'first')
473
+ news2 = News.create(:name => 'second')
474
+ news1.votes << Vote.create(:vote => 10)
475
+ news1.votes << Vote.create(:vote => 20)
476
+ news2.votes << Vote.create(:vote => 10)
477
+ news2.votes << Vote.create(:vote => 20)
478
+ end
479
+
480
+ after(:all) do
481
+ teardown_db
482
+ end
483
+
484
+ before(:each) do
485
+ Bullet::Association.start_request
486
+ end
487
+
488
+ after(:each) do
489
+ Bullet::Association.end_request
490
+ end
491
+
492
+ it "should detect unpreload associatoins" do
493
+ User.find(:all).each do |user|
494
+ user.votes.collect(&:vote)
495
+ end
496
+ Bullet::Association.should be_has_unpreload_associations
497
+ end
498
+
499
+ it "should detect no unpreload associatoins" do
500
+ User.find(:all, :include => :votes).each do |user|
501
+ user.votes.collect(&:vote)
502
+ end
503
+ Bullet::Association.should_not be_has_unpreload_associations
504
+ end
505
+
506
+ it "should detect unpreload associatoins with voteable" do
507
+ Vote.find(:all).each do |vote|
508
+ vote.voteable.name
509
+ end
510
+ Bullet::Association.should be_has_unpreload_associations
511
+ end
512
+
513
+ it "should detect no unpreload associatoins with voteable" do
514
+ Vote.find(:all, :include => :voteable).each do |vote|
515
+ vote.voteable.name
516
+ end
517
+ Bullet::Association.should_not be_has_unpreload_associations
518
+ end
519
+
520
+ it "should detect no unused preload associations" do
521
+ User.find(:all).collect(&:name)
522
+ Bullet::Association.check_unused_preload_associations
523
+ Bullet::Association.should_not be_has_unused_preload_associations
524
+ end
525
+
526
+ it "should detect unused preload associations" do
527
+ User.find(:all, :include => :votes).collect(&:name)
528
+ Bullet::Association.check_unused_preload_associations
529
+ Bullet::Association.should be_has_unused_preload_associations
530
+ end
531
+
532
+ it "should detect no unused preload associations with voteable" do
533
+ Vote.find(:all).collect(&:vote)
534
+ Bullet::Association.check_unused_preload_associations
535
+ Bullet::Association.should_not be_has_unused_preload_associations
536
+ end
537
+
538
+ it "should detect unused preload associatoins with voteable" do
539
+ Vote.find(:all, :include => :voteable).collect(&:vote)
540
+ Bullet::Association.check_unused_preload_associations
541
+ Bullet::Association.should be_has_unused_preload_associations
542
+ end
543
+ end
544
+
545
+ describe Bullet::Association, "has_one" do
546
+ def setup_db
547
+ ActiveRecord::Schema.define(:version => 1) do
548
+ create_table :companies do |t|
549
+ t.column :name, :string
550
+ end
551
+
552
+ create_table :addresses do |t|
553
+ t.column :name, :string
554
+ t.column :company_id, :integer
555
+ end
556
+ end
557
+ end
558
+
559
+ def teardown_db
560
+ ActiveRecord::Base.connection.tables.each do |table|
561
+ ActiveRecord::Base.connection.drop_table(table)
562
+ end
563
+ end
564
+
565
+ class Company < ActiveRecord::Base
566
+ has_one :address
567
+ end
568
+
569
+ class Address < ActiveRecord::Base
570
+ belongs_to :company
571
+ end
572
+
573
+ before(:all) do
574
+ setup_db
575
+
576
+ company1 = Company.create(:name => 'first')
577
+ company2 = Company.create(:name => 'second')
578
+
579
+ Address.create(:name => 'first', :company => company1)
580
+ Address.create(:name => 'second', :company => company2)
581
+ end
582
+
583
+ after(:all) do
584
+ teardown_db
585
+ end
586
+
587
+ before(:each) do
588
+ Bullet::Association.start_request
589
+ end
590
+
591
+ after(:each) do
592
+ Bullet::Association.end_request
593
+ end
594
+
595
+ it "should detect unpreload association" do
596
+ Company.find(:all).each do |company|
597
+ company.address.name
598
+ end
599
+ Bullet::Association.should be_has_unpreload_associations
600
+ end
601
+
602
+ it "should detect no unpreload association" do
603
+ Company.find(:all, :include => :address).each do |company|
604
+ company.address.name
605
+ end
606
+ Bullet::Association.should_not be_has_unpreload_associations
607
+ end
608
+
609
+ it "should detect no unused preload association" do
610
+ Company.find(:all).collect(&:name)
611
+ Bullet::Association.check_unused_preload_associations
612
+ Bullet::Association.should_not be_has_unused_preload_associations
613
+ end
614
+
615
+ it "should detect unused preload association" do
616
+ Company.find(:all, :include => :address).collect(&:name)
617
+ Bullet::Association.check_unused_preload_associations
618
+ Bullet::Association.should be_has_unused_preload_associations
619
+ end
620
+ end
621
+
622
+ describe Bullet::Association, "call one association that in possiable objects" do
623
+ def setup_db
624
+ ActiveRecord::Schema.define(:version => 1) do
625
+ create_table :contacts do |t|
626
+ t.column :name, :string
627
+ end
628
+
629
+ create_table :emails do |t|
630
+ t.column :name, :string
631
+ t.column :contact_id, :integer
632
+ end
633
+ end
634
+ end
635
+
636
+ def teardown_db
637
+ ActiveRecord::Base.connection.tables.each do |table|
638
+ ActiveRecord::Base.connection.drop_table(table)
639
+ end
640
+ end
641
+
642
+ class Contact < ActiveRecord::Base
643
+ has_many :emails
644
+ end
645
+
646
+ class Email < ActiveRecord::Base
647
+ belongs_to :contact
648
+ end
649
+
650
+ before(:all) do
651
+ setup_db
652
+
653
+ contact1 = Contact.create(:name => 'first')
654
+ contact2 = Contact.create(:name => 'second')
655
+
656
+ email1 = contact1.emails.create(:name => 'first')
657
+ email2 = contact1.emails.create(:name => 'second')
658
+ email3 = contact2.emails.create(:name => 'third')
659
+ email4 = contact2.emails.create(:name => 'fourth')
660
+ end
661
+
662
+ after(:all) do
663
+ teardown_db
664
+ end
665
+
666
+ before(:each) do
667
+ Bullet::Association.start_request
668
+ end
669
+
670
+ after(:each) do
671
+ Bullet::Association.end_request
672
+ end
673
+
674
+ it "should detect no unpreload association" do
675
+ Contact.find(:all)
676
+ Contact.first.emails.collect(&:name)
677
+ Bullet::Association.should_not be_has_unpreload_associations
678
+ end
679
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,8 @@
1
+ --colour
2
+ --format
3
+ specdoc
4
+ --reverse
5
+ --timeout
6
+ 20
7
+ --loadby
8
+ mtime
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'spec/autorun'
3
+ require 'active_record'
4
+
5
+ RAILS_ROOT = File.expand_path(__FILE__).split('/')[0..-3].join('/')
6
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/bullet/logger'))
7
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/bullet/association'))
8
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/bullet'))
9
+ Bullet.enable = true
10
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/hack/active_record'))
@@ -0,0 +1,9 @@
1
+ namespace :bullet do
2
+ namespace :log do
3
+ desc "Truncates the bullet log file to zero bytes"
4
+ task :clear do
5
+ f = File.open("log/bullet.log", "w")
6
+ f.close
7
+ end
8
+ end
9
+ end