bullet 2.0.0.rc1 → 2.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +0,0 @@
1
- require 'bullet'
@@ -1,96 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
-
3
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
- # This test is just used for http://github.com/flyerhzm/bullet/issues/#issue/14
5
- describe Bullet::Detector::Association do
6
-
7
- describe "for chris" do
8
- def setup_db
9
- ActiveRecord::Schema.define(:version => 1) do
10
- create_table :locations do |t|
11
- t.column :name, :string
12
- end
13
-
14
- create_table :hotels do |t|
15
- t.column :name, :string
16
- t.column :location_id, :integer
17
- end
18
-
19
- create_table :deals do |t|
20
- t.column :name, :string
21
- t.column :hotel_id, :integer
22
- end
23
- end
24
- end
25
-
26
- def teardown_db
27
- ActiveRecord::Base.connection.tables.each do |table|
28
- ActiveRecord::Base.connection.drop_table(table)
29
- end
30
- end
31
-
32
- class Location < ActiveRecord::Base
33
- has_many :hotels
34
- end
35
-
36
- class Hotel < ActiveRecord::Base
37
- belongs_to :location
38
- has_many :deals
39
- end
40
-
41
- class Deal < ActiveRecord::Base
42
- belongs_to :hotel
43
- has_one :location, :through => :hotel
44
- end
45
-
46
- before(:all) do
47
- setup_db
48
-
49
- location1 = Location.create(:name => "location1")
50
- location2 = Location.create(:name => "location2")
51
-
52
- hotel1 = location1.hotels.create(:name => "hotel1")
53
- hotel2 = location1.hotels.create(:name => "hotel2")
54
- hotel3 = location2.hotels.create(:name => "hotel3")
55
- hotel4 = location2.hotels.create(:name => "hotel4")
56
-
57
- deal1 = hotel1.deals.create(:name => "deal1")
58
- deal2 = hotel2.deals.create(:name => "deal2")
59
- deal3 = hotel3.deals.create(:name => "deal3")
60
- deal4 = hotel4.deals.create(:name => "deal4")
61
- end
62
-
63
- after(:all) do
64
- teardown_db
65
- end
66
-
67
- before(:each) do
68
- Bullet.start_request
69
- end
70
-
71
- after(:each) do
72
- Bullet.end_request
73
- end
74
-
75
- it "should detect unpreload association from deal to hotel" do
76
- Deal.all.each do |deal|
77
- deal.hotel.location.name
78
- end
79
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Deal, :hotel)
80
- end
81
-
82
- it "should detect unpreload association from hotel to location" do
83
- Deal.includes(:hotel).each do |deal|
84
- deal.hotel.location.name
85
- end
86
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Hotel, :location)
87
- end
88
-
89
- it "should not detect unpreload association" do
90
- Deal.includes({:hotel => :location}).each do |deal|
91
- deal.hotel.location.name
92
- end
93
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
94
- end
95
- end
96
- end
@@ -1,86 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
-
3
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
- # This test is just used for http://github.com/flyerhzm/bullet/issues#issue/20
5
- describe Bullet::Detector::Association do
6
-
7
- describe "for peschkaj" do
8
- def setup_db
9
- ActiveRecord::Schema.define(:version => 1) do
10
- create_table :categories do |t|
11
- t.column :name, :string
12
- end
13
-
14
- create_table :submissions do |t|
15
- t.column :name, :string
16
- t.column :category_id, :integer
17
- t.column :user_id, :integer
18
- end
19
-
20
- create_table :users 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 :submissions
35
- has_many :users
36
- end
37
-
38
- class Submission < ActiveRecord::Base
39
- belongs_to :category
40
- belongs_to :user
41
- end
42
-
43
- class User < ActiveRecord::Base
44
- has_one :submission
45
- belongs_to :category
46
- end
47
-
48
- before(:all) do
49
- setup_db
50
-
51
- category1 = Category.create(:name => "category1")
52
- category2 = Category.create(:name => "category2")
53
-
54
- user1 = User.create(:name => 'user1', :category => category1)
55
- user2 = User.create(:name => 'user2', :category => category1)
56
-
57
- submission1 = category1.submissions.create(:name => "submission1", :user => user1)
58
- submission2 = category1.submissions.create(:name => "submission2", :user => user2)
59
- submission3 = category2.submissions.create(:name => "submission3", :user => user1)
60
- submission4 = category2.submissions.create(:name => "submission4", :user => user2)
61
- end
62
-
63
- after(:all) do
64
- teardown_db
65
- end
66
-
67
- before(:each) do
68
- Bullet.start_request
69
- end
70
-
71
- after(:each) do
72
- Bullet.end_request
73
- end
74
-
75
- it "should not detect unused preload associations" do
76
- category = Category.includes({:submissions => :user}).order("id DESC").find_by_name('category1')
77
- category.submissions.map do |submission|
78
- submission.name
79
- submission.user.name
80
- end
81
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
82
- Bullet::Detector::Association.should_not be_unused_preload_associations_for(Category, :submissions)
83
- Bullet::Detector::Association.should_not be_unused_preload_associations_for(Submission, :user)
84
- end
85
- end
86
- end
@@ -1,899 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
-
3
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
-
5
- describe Bullet::Detector::Association, 'has_many' do
6
-
7
- def setup_db
8
- ActiveRecord::Schema.define(:version => 1) do
9
- create_table :categories do |t|
10
- t.column :name, :string
11
- end
12
-
13
- create_table :posts do |t|
14
- t.column :name, :string
15
- t.column :category_id, :integer
16
- t.column :writer_id, :integer
17
- end
18
-
19
- create_table :comments do |t|
20
- t.column :name, :string
21
- t.column :post_id, :integer
22
- t.column :author_id, :integer
23
- end
24
-
25
- create_table :entries do |t|
26
- t.column :name, :string
27
- t.column :category_id, :integer
28
- end
29
-
30
- create_table :base_users do |t|
31
- t.column :name, :string
32
- t.column :type, :string
33
- t.column :newspaper_id, :integer
34
- end
35
- create_table :newspapers do |t|
36
- t.column :name, :string
37
- end
38
- end
39
- end
40
-
41
- def teardown_db
42
- ActiveRecord::Base.connection.tables.each do |table|
43
- ActiveRecord::Base.connection.drop_table(table)
44
- end
45
- end
46
-
47
- class Category < ActiveRecord::Base
48
- has_many :posts
49
- has_many :entries
50
- end
51
-
52
- class Post < ActiveRecord::Base
53
- belongs_to :category
54
- has_many :comments
55
- belongs_to :writer
56
-
57
-
58
- scope :preload_posts, lambda { includes(:comments) }
59
- scope :in_category_name, lambda { |name|
60
- where(['categories.name = ?', name]).includes(:category)
61
- }
62
- end
63
-
64
- class Entry < ActiveRecord::Base
65
- belongs_to :category
66
- end
67
-
68
- class Comment < ActiveRecord::Base
69
- belongs_to :post
70
- belongs_to :author, :class_name => "BaseUser"
71
- end
72
-
73
- class BaseUser < ActiveRecord::Base
74
- has_many :comments
75
- has_many :posts
76
- belongs_to :newspaper
77
- end
78
-
79
- class Newspaper < ActiveRecord::Base
80
- has_many :writers, :class_name => "BaseUser"
81
- end
82
-
83
- class Writer < BaseUser
84
- end
85
-
86
- before(:all) do
87
- setup_db
88
-
89
- newspaper1 = Newspaper.create(:name => "First Newspaper")
90
- newspaper2 = Newspaper.create(:name => "Second Newspaper")
91
-
92
- writer1 = Writer.create(:name => 'first', :newspaper => newspaper1)
93
- writer2 = Writer.create(:name => 'second', :newspaper => newspaper2)
94
- user1 = BaseUser.create(:name => 'third', :newspaper => newspaper1)
95
- user2 = BaseUser.create(:name => 'fourth', :newspaper => newspaper2)
96
-
97
-
98
- category1 = Category.create(:name => 'first')
99
- category2 = Category.create(:name => 'second')
100
-
101
- post1 = category1.posts.create(:name => 'first', :writer => writer1)
102
- post1a = category1.posts.create(:name => 'like first', :writer => writer2)
103
- post2 = category2.posts.create(:name => 'second', :writer => writer2)
104
-
105
- comment1 = post1.comments.create(:name => 'first', :author => writer1)
106
- comment2 = post1.comments.create(:name => 'first2', :author => writer1)
107
- comment3 = post1.comments.create(:name => 'first3', :author => writer1)
108
- comment4 = post1.comments.create(:name => 'second', :author => writer2)
109
- comment8 = post1a.comments.create(:name => "like first 1", :author => writer1)
110
- comment9 = post1a.comments.create(:name => "like first 2", :author => writer2)
111
- comment5 = post2.comments.create(:name => 'third', :author => user1)
112
- comment6 = post2.comments.create(:name => 'fourth', :author => user2)
113
- comment7 = post2.comments.create(:name => 'fourth', :author => writer1)
114
-
115
- entry1 = category1.entries.create(:name => 'first')
116
- entry2 = category1.entries.create(:name => 'second')
117
- end
118
-
119
- after(:all) do
120
- teardown_db
121
- end
122
-
123
- before(:each) do
124
- Bullet.start_request
125
- end
126
-
127
- after(:each) do
128
- Bullet.end_request
129
- end
130
-
131
- # FIXME: setup and teardown are not inherited by context
132
- # context "for unused cases" do
133
- #If you have the same record created twice with different includes
134
- # the hash value get's accumulated includes, which leads to false Unused eager loading
135
- #it "should not incorrectly mark associations as unused when multiple object instances" do
136
- #comments_with_author = Comment.includes(:author)
137
- #comments_with_post = Comment.includes(:post)
138
- #comments_with_author.each { |c| c.author.name }
139
- #comments_with_author.each { |c| c.post.name }
140
- #Bullet::Association.check_unused_preload_associations
141
- #Bullet::Association.should be_unused_preload_associations_for(Comment, :post)
142
- #Bullet::Association.should be_detecting_unpreloaded_association_for(Comment, :post)
143
- #end
144
-
145
- # same as above with different Models being queried
146
- #it "should not incorrectly mark associations as unused when multiple object instances different Model" do
147
- #post_with_comments = Post.includes(:comments)
148
- #comments_with_author = Comment.includes(:author)
149
- #post_with_comments.each { |p| p.comments.first.author.name }
150
- #comments_with_author.each { |c| c.name }
151
- #Bullet::Association.check_unused_preload_associations
152
- #Bullet::Association.should be_unused_preload_associations_for(Comment, :author)
153
- #Bullet::Association.should be_detecting_unpreloaded_association_for(Comment, :author)
154
- #end
155
-
156
- # this test passes right now. But is a regression test to ensure that if only a small set of returned records
157
- # is not used that a unused preload association error is not generated
158
- it "should not have unused when small set of returned records are discarded" do
159
- comments_with_author = Comment.includes(:author)
160
- comment_collection = comments_with_author.limit(2)
161
- comment_collection.collect { |com| com.author.name }
162
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
163
- Bullet::Detector::Association.should_not be_unused_preload_associations_for(Comment, :author)
164
- end
165
- # end
166
-
167
-
168
- # FIXME: setup and teardown are not inherited by context
169
- # context "comments => posts => category" do
170
-
171
- # this happens because the post isn't a possible object even though the writer is access through the post
172
- # which leads to an 1+N queries
173
- it "should detect unpreloaded writer" do
174
- Comment.includes([:author, :post]).where(["base_users.id = ?", BaseUser.first]).each do |com|
175
- com.post.writer.name
176
- end
177
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Post, :writer)
178
- end
179
-
180
- # this happens because the comment doesn't break down the hash into keys
181
- # properly creating an association from comment to post
182
- it "should detect preload of comment => post" do
183
- comments = Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |com|
184
- com.post.writer.name
185
- end
186
- Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Comment, :post)
187
- Bullet::Detector::Association.should be_completely_preloading_associations
188
- end
189
-
190
- it "should detect preload of post => writer" do
191
- comments = Comment.includes([:author, {:post => :writer}]).where(["base_users.id = ?", BaseUser.first]).each do |com|
192
- com.post.writer.name
193
- end
194
- Bullet::Detector::Association.should be_creating_object_association_for(comments.first, :author)
195
- Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Post, :writer)
196
- Bullet::Detector::Association.should be_completely_preloading_associations
197
- end
198
-
199
- # To flyerhzm: This does not detect that newspaper is unpreloaded. The association is
200
- # not within possible objects, and thus cannot be detected as unpreloaded
201
- it "should detect unpreloading of writer => newspaper" do
202
- comments = Comment.all(:include => {:post => :writer}, :conditions => "posts.name like '%first%'").each do |com|
203
- com.post.writer.newspaper.name
204
- end
205
- Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Writer, :newspaper)
206
- end
207
-
208
- # when we attempt to access category, there is an infinite overflow because load_target is hijacked leading to
209
- # a repeating loop of calls in this test
210
- it "should not raise a stack error from posts to category" do
211
- lambda {
212
- Comment.includes({:post => :category}).each do |com|
213
- com.post.category
214
- end
215
- }.should_not raise_error(SystemStackError)
216
- end
217
- # end
218
-
219
- # FIXME: setup and teardown are not inherited by context
220
- # context "post => comments" do
221
- #
222
- ### FIXME: Please double check semantic equivalence with original
223
- it "should detect preload with post => comments" do
224
- Post.includes(:comments).each do |post|
225
- post.comments.collect(&:name)
226
- end
227
- # Bullet::Detector::Association.should_not be_has_unpreload_associations
228
- Bullet::Detector::Association.should be_completely_preloading_associations
229
- end
230
-
231
- it "should detect no preload post => comments" do
232
- Post.all.each do |post|
233
- post.comments.collect(&:name)
234
- end
235
- # Bullet::Detector::Association.should be_has_unpreload_associations
236
- Bullet::Detector::Association.should_not be_completely_preloading_associations
237
- end
238
-
239
- it "should detect unused preload post => comments for post" do
240
- Post.includes(:comments).collect(&:name)
241
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
242
- Bullet::Detector::Association.should be_has_unused_preload_associations
243
- end
244
-
245
- it "should detect no unused preload post => comments for post" do
246
- Post.all.collect(&:name)
247
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
248
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
249
- end
250
-
251
- it "should detect no unused preload post => comments for comment" do
252
- Post.all.each do |post|
253
- post.comments.collect(&:name)
254
- end
255
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
256
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
257
-
258
- Bullet.end_request
259
- Bullet.start_request
260
-
261
- Post.all.each do |post|
262
- post.comments.collect(&:name)
263
- end
264
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
265
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
266
- end
267
- # end
268
-
269
- # FIXME: setup and teardown are not inherited by context
270
- # context "category => posts => comments" do
271
- it "should detect preload with category => posts => comments" do
272
- Category.includes({:posts => :comments}).each do |category|
273
- category.posts.each do |post|
274
- post.comments.collect(&:name)
275
- end
276
- end
277
- # Bullet::Detector::Association.should_not be_has_unpreload_associations
278
- Bullet::Detector::Association.should be_completely_preloading_associations
279
- end
280
-
281
- it "should detect preload category => posts, but no post => comments" do
282
- Category.includes(:posts).each do |category|
283
- category.posts.each do |post|
284
- post.comments.collect(&:name)
285
- end
286
- end
287
- # Bullet::Detector::Association.should be_has_unpreload_associations
288
- Bullet::Detector::Association.should_not be_completely_preloading_associations
289
- end
290
-
291
- it "should detect no preload category => posts => comments" do
292
- Category.all.each do |category|
293
- category.posts.each do |post|
294
- post.comments.collect(&:name)
295
- end
296
- end
297
- # Bullet::Detector::Association.should be_has_unpreload_associations
298
- Bullet::Detector::Association.should_not be_completely_preloading_associations
299
- end
300
-
301
- it "should detect unused preload with category => posts => comments" do
302
- Category.includes({:posts => :comments}).collect(&:name)
303
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
304
- Bullet::Detector::Association.should be_has_unused_preload_associations
305
- end
306
-
307
- it "should detect unused preload with post => commnets, no category => posts" do
308
- Category.includes({:posts => :comments}).each do |category|
309
- category.posts.collect(&:name)
310
- end
311
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
312
- Bullet::Detector::Association.should be_has_unused_preload_associations
313
- end
314
-
315
- it "should no detect preload with category => posts => comments" do
316
- Category.all.each do |category|
317
- category.posts.each do |post|
318
- post.comments.collect(&:name)
319
- end
320
- end
321
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
322
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
323
- end
324
- # end
325
-
326
- # FIXME: setup and teardown are not inherited by context
327
- # context "category => posts, category => entries" do
328
- it "should detect preload with category => [posts, entries]" do
329
- Category.includes([:posts, :entries]).each do |category|
330
- category.posts.collect(&:name)
331
- category.entries.collect(&:name)
332
- end
333
- Bullet::Detector::Association.should be_completely_preloading_associations
334
- end
335
-
336
- it "should detect preload with category => posts, but no category => entries" do
337
- Category.includes(:posts).each do |category|
338
- category.posts.collect(&:name)
339
- category.entries.collect(&:name)
340
- end
341
- Bullet::Detector::Association.should_not be_completely_preloading_associations
342
- end
343
-
344
- it "should detect no preload with category => [posts, entries]" do
345
- Category.all.each do |category|
346
- category.posts.collect(&:name)
347
- category.entries.collect(&:name)
348
- end
349
- Bullet::Detector::Association.should_not be_completely_preloading_associations
350
- end
351
-
352
- it "should detect unused with category => [posts, entries]" do
353
- Category.includes([:posts, :entries]).collect(&:name)
354
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
355
- Bullet::Detector::Association.should be_has_unused_preload_associations
356
- end
357
-
358
- it "should detect unused preload with category => entries, but no category => posts" do
359
- Category.includes([:posts, :entries]).each do |category|
360
- category.posts.collect(&:name)
361
- end
362
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
363
- Bullet::Detector::Association.should be_has_unused_preload_associations
364
- end
365
-
366
- it "should detect no unused preload" do
367
- Category.all.each do |category|
368
- category.posts.collect(&:name)
369
- category.entries.collect(&:name)
370
- end
371
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
372
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
373
- end
374
- # end
375
-
376
- # FIXME: setup and teardown are not inherited by context
377
- # context "no preload" do
378
- it "should no preload only display only one post => comment" do
379
- Post.includes(:comments).each do |post|
380
- post.comments.first.name
381
- end
382
- Bullet::Detector::Association.should be_completely_preloading_associations
383
- end
384
-
385
- it "should no preload only one post => commnets" do
386
- Post.first.comments.collect(&:name)
387
- Bullet::Detector::Association.should be_completely_preloading_associations
388
- end
389
- # end
390
-
391
- # FIXME: setup and teardown are not inherited by context
392
- # context "scope for_category_name" do
393
- it "should detect preload with post => category" do
394
- Post.in_category_name('first').all.each do |post|
395
- post.category.name
396
- end
397
- Bullet::Detector::Association.should be_completely_preloading_associations
398
- end
399
-
400
- it "should not be unused preload post => category" do
401
- Post.in_category_name('first').all.collect(&:name)
402
- Bullet::Detector::Association.should be_completely_preloading_associations
403
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
404
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
405
- end
406
- # end
407
-
408
- # FIXME: setup and teardown are not inherited by context
409
- # context "scope preload_posts" do
410
- it "should no preload post => comments with scope" do
411
- Post.preload_posts.each do |post|
412
- post.comments.collect(&:name)
413
- end
414
- Bullet::Detector::Association.should be_completely_preloading_associations
415
- end
416
-
417
- it "should unused preload with scope" do
418
- Post.preload_posts.collect(&:name)
419
- Bullet::Detector::Association.should be_completely_preloading_associations
420
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
421
- Bullet::Detector::Association.should be_has_unused_preload_associations
422
- end
423
- # end
424
-
425
- # FIXME: setup and teardown are not inherited by context
426
- # context "no unused" do
427
- it "should no unused only display only one post => comment" do
428
- Post.includes(:comments).each do |post|
429
- i = 0
430
- post.comments.each do |comment|
431
- if i == 0
432
- comment.name
433
- else
434
- i += 1
435
- end
436
- end
437
- end
438
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
439
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
440
- end
441
- # end
442
-
443
- # FIXME: setup and teardown are not inherited by context
444
- # context "belongs_to" do
445
- it "should preload comments => post" do
446
- Comment.all.each do |comment|
447
- comment.post.name
448
- end
449
- Bullet::Detector::Association.should_not be_completely_preloading_associations
450
- end
451
-
452
- it "should no preload comment => post" do
453
- Comment.first.post.name
454
- Bullet::Detector::Association.should be_completely_preloading_associations
455
- end
456
-
457
- it "should no preload comments => post" do
458
- Comment.includes(:post).each do |comment|
459
- comment.post.name
460
- end
461
- Bullet::Detector::Association.should be_completely_preloading_associations
462
- end
463
-
464
- it "should detect no unused preload comments => post" do
465
- Comment.all.collect(&:name)
466
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
467
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
468
- end
469
-
470
- it "should detect unused preload comments => post" do
471
- Comment.includes(:post).collect(&:name)
472
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
473
- Bullet::Detector::Association.should be_has_unused_preload_associations
474
- end
475
-
476
- it "should dectect no unused preload comments => post" do
477
- Comment.all.each do |comment|
478
- comment.post.name
479
- end
480
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
481
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
482
- end
483
-
484
- it "should dectect no unused preload comments => post" do
485
- Comment.includes(:post).each do |comment|
486
- comment.post.name
487
- end
488
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
489
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
490
- end
491
- # end
492
- end
493
-
494
- describe Bullet::Detector::Association, 'has_and_belongs_to_many' do
495
-
496
- def setup_db
497
- ActiveRecord::Schema.define(:version => 1) do
498
- create_table :students do |t|
499
- t.column :name, :string
500
- end
501
-
502
- create_table :teachers do |t|
503
- t.column :name, :string
504
- end
505
-
506
- create_table :students_teachers, :id => false do |t|
507
- t.column :student_id, :integer
508
- t.column :teacher_id, :integer
509
- end
510
- end
511
- end
512
-
513
- def teardown_db
514
- ActiveRecord::Base.connection.tables.each do |table|
515
- ActiveRecord::Base.connection.drop_table(table)
516
- end
517
- end
518
-
519
- class Student < ActiveRecord::Base
520
- has_and_belongs_to_many :teachers
521
- end
522
-
523
- class Teacher < ActiveRecord::Base
524
- has_and_belongs_to_many :students
525
- end
526
-
527
- before(:all) do
528
- setup_db
529
- student1 = Student.create(:name => 'first')
530
- student2 = Student.create(:name => 'second')
531
- teacher1 = Teacher.create(:name => 'first')
532
- teacher2 = Teacher.create(:name => 'second')
533
- student1.teachers = [teacher1, teacher2]
534
- student2.teachers = [teacher1, teacher2]
535
- teacher1.students << student1
536
- teacher2.students << student2
537
- end
538
-
539
- after(:all) do
540
- teardown_db
541
- end
542
-
543
- before(:each) do
544
- Bullet.start_request
545
- end
546
-
547
- after(:each) do
548
- Bullet.end_request
549
- end
550
-
551
- it "should detect unpreload associations" do
552
- Student.all.each do |student|
553
- student.teachers.collect(&:name)
554
- end
555
- Bullet::Detector::Association.should_not be_completely_preloading_associations
556
- end
557
-
558
- it "should detect no unpreload associations" do
559
- Student.includes(:teachers).each do |student|
560
- student.teachers.collect(&:name)
561
- end
562
- Bullet::Detector::Association.should be_completely_preloading_associations
563
- end
564
-
565
- it "should detect unused preload associations" do
566
- Student.includes(:teachers).collect(&:name)
567
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
568
- Bullet::Detector::Association.should be_has_unused_preload_associations
569
- end
570
-
571
- it "should detect no unused preload associations" do
572
- Student.all.collect(&:name)
573
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
574
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
575
- end
576
- end
577
-
578
- describe Bullet::Detector::Association, 'has_many :through' do
579
-
580
- def setup_db
581
- ActiveRecord::Schema.define(:version => 1) do
582
- create_table :firms do |t|
583
- t.column :name, :string
584
- end
585
-
586
- create_table :clients do |t|
587
- t.column :name, :string
588
- end
589
-
590
- create_table :relationships do |t|
591
- t.column :firm_id, :integer
592
- t.column :client_id, :integer
593
- end
594
- end
595
- end
596
-
597
- def teardown_db
598
- ActiveRecord::Base.connection.tables.each do |table|
599
- ActiveRecord::Base.connection.drop_table(table)
600
- end
601
- end
602
-
603
- class Firm < ActiveRecord::Base
604
- has_many :relationships
605
- has_many :clients, :through => :relationships
606
- end
607
-
608
- class Client < ActiveRecord::Base
609
- has_many :relationships
610
- has_many :firms, :through => :relationships
611
- end
612
-
613
- class Relationship < ActiveRecord::Base
614
- belongs_to :firm
615
- belongs_to :client
616
- end
617
-
618
- before(:all) do
619
- setup_db
620
- firm1 = Firm.create(:name => 'first')
621
- firm2 = Firm.create(:name => 'second')
622
- client1 = Client.create(:name => 'first')
623
- client2 = Client.create(:name => 'second')
624
- firm1.clients = [client1, client2]
625
- firm2.clients = [client1, client2]
626
- client1.firms << firm1
627
- client2.firms << firm2
628
- end
629
-
630
- after(:all) do
631
- teardown_db
632
- end
633
-
634
- before(:each) do
635
- Bullet.start_request
636
- end
637
-
638
- after(:each) do
639
- Bullet.end_request
640
- end
641
-
642
- it "should detect unpreload associations" do
643
- Firm.all.each do |firm|
644
- firm.clients.collect(&:name)
645
- end
646
- Bullet::Detector::Association.should_not be_completely_preloading_associations
647
- end
648
-
649
- it "should detect no unpreload associations" do
650
- Firm.includes(:clients).each do |firm|
651
- firm.clients.collect(&:name)
652
- end
653
- Bullet::Detector::Association.should be_completely_preloading_associations
654
- end
655
-
656
- it "should detect no unused preload associations" do
657
- Firm.all.collect(&:name)
658
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
659
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
660
- end
661
-
662
- it "should detect unused preload associations" do
663
- Firm.includes(:clients).collect(&:name)
664
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
665
- Bullet::Detector::Association.should be_has_unused_preload_associations
666
- end
667
- end
668
-
669
-
670
-
671
- describe Bullet::Detector::Association, "has_one" do
672
-
673
- def setup_db
674
- ActiveRecord::Schema.define(:version => 1) do
675
- create_table :companies do |t|
676
- t.column :name, :string
677
- end
678
-
679
- create_table :addresses do |t|
680
- t.column :name, :string
681
- t.column :company_id, :integer
682
- end
683
- end
684
- end
685
-
686
- def teardown_db
687
- ActiveRecord::Base.connection.tables.each do |table|
688
- ActiveRecord::Base.connection.drop_table(table)
689
- end
690
- end
691
-
692
- class Company < ActiveRecord::Base
693
- has_one :address
694
- end
695
-
696
- class Address < ActiveRecord::Base
697
- belongs_to :company
698
- end
699
-
700
- before(:all) do
701
- setup_db
702
-
703
- company1 = Company.create(:name => 'first')
704
- company2 = Company.create(:name => 'second')
705
-
706
- Address.create(:name => 'first', :company => company1)
707
- Address.create(:name => 'second', :company => company2)
708
- end
709
-
710
- after(:all) do
711
- teardown_db
712
- end
713
-
714
- before(:each) do
715
- Bullet.start_request
716
- end
717
-
718
- after(:each) do
719
- Bullet.end_request
720
- end
721
-
722
- it "should detect unpreload association" do
723
- Company.all.each do |company|
724
- company.address.name
725
- end
726
- Bullet::Detector::Association.should_not be_completely_preloading_associations
727
- end
728
-
729
- it "should detect no unpreload association" do
730
- Company.find(:all, :include => :address).each do |company|
731
- company.address.name
732
- end
733
- Bullet::Detector::Association.should be_completely_preloading_associations
734
- end
735
-
736
- it "should detect no unused preload association" do
737
- Company.all.collect(&:name)
738
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
739
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
740
- end
741
-
742
- it "should detect unused preload association" do
743
- Company.find(:all, :include => :address).collect(&:name)
744
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
745
- Bullet::Detector::Association.should be_has_unused_preload_associations
746
- end
747
- end
748
-
749
- describe Bullet::Detector::Association, "call one association that in possible objects" do
750
-
751
- def setup_db
752
- ActiveRecord::Schema.define(:version => 1) do
753
- create_table :contacts do |t|
754
- t.column :name, :string
755
- end
756
-
757
- create_table :emails do |t|
758
- t.column :name, :string
759
- t.column :contact_id, :integer
760
- end
761
- end
762
- end
763
-
764
- def teardown_db
765
- ActiveRecord::Base.connection.tables.each do |table|
766
- ActiveRecord::Base.connection.drop_table(table)
767
- end
768
- end
769
-
770
- class Contact < ActiveRecord::Base
771
- has_many :emails
772
- end
773
-
774
- class Email < ActiveRecord::Base
775
- belongs_to :contact
776
- end
777
-
778
- before(:all) do
779
- setup_db
780
-
781
- contact1 = Contact.create(:name => 'first')
782
- contact2 = Contact.create(:name => 'second')
783
-
784
- email1 = contact1.emails.create(:name => 'first')
785
- email2 = contact1.emails.create(:name => 'second')
786
- email3 = contact2.emails.create(:name => 'third')
787
- email4 = contact2.emails.create(:name => 'fourth')
788
- end
789
-
790
- after(:all) do
791
- teardown_db
792
- end
793
-
794
- before(:each) do
795
- Bullet.start_request
796
- end
797
-
798
- after(:each) do
799
- Bullet.end_request
800
- end
801
-
802
- it "should detect no unpreload association" do
803
- Contact.all
804
- Contact.first.emails.collect(&:name)
805
- Bullet::Detector::Association.should be_completely_preloading_associations
806
- end
807
- end
808
-
809
- describe Bullet::Detector::Association, "STI" do
810
-
811
- def setup_db
812
- ActiveRecord::Schema.define(:version => 1) do
813
- create_table :documents do |t|
814
- t.string :name
815
- t.string :type
816
- t.integer :parent_id
817
- t.integer :author_id
818
- end
819
-
820
- create_table :authors do |t|
821
- t.string :name
822
- end
823
- end
824
- end
825
-
826
- def teardown_db
827
- ActiveRecord::Base.connection.tables.each do |table|
828
- ActiveRecord::Base.connection.drop_table(table)
829
- end
830
- end
831
-
832
- class Document < ActiveRecord::Base
833
- has_many :children, :class_name => "Document", :foreign_key => 'parent_id'
834
- belongs_to :parent, :class_name => "Document", :foreign_key => 'parent_id'
835
- belongs_to :author
836
- end
837
-
838
- class Page < Document
839
- end
840
-
841
- class Folder < Document
842
- end
843
-
844
- class Author < ActiveRecord::Base
845
- has_many :documents
846
- end
847
-
848
- before(:all) do
849
- setup_db
850
- author1 = Author.create(:name => 'author1')
851
- author2 = Author.create(:name => 'author2')
852
- folder1 = Folder.create(:name => 'folder1', :author_id => author1.id)
853
- folder2 = Folder.create(:name => 'folder2', :author_id => author2.id)
854
- page1 = Page.create(:name => 'page1', :parent_id => folder1.id, :author_id => author1.id)
855
- page2 = Page.create(:name => 'page2', :parent_id => folder1.id, :author_id => author1.id)
856
- page3 = Page.create(:name => 'page3', :parent_id => folder2.id, :author_id => author2.id)
857
- page4 = Page.create(:name => 'page4', :parent_id => folder2.id, :author_id => author2.id)
858
- end
859
-
860
- before(:each) do
861
- Bullet.start_request
862
- end
863
-
864
- after(:each) do
865
- Bullet.end_request
866
- end
867
-
868
- it "should detect unpreload associations" do
869
- Page.all.each do |page|
870
- page.author.name
871
- end
872
- Bullet::Detector::Association.should_not be_completely_preloading_associations
873
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
874
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
875
- end
876
-
877
- it "should not detect unpreload associations" do
878
- Page.find(:all, :include => :author).each do |page|
879
- page.author.name
880
- end
881
- Bullet::Detector::Association.should be_completely_preloading_associations
882
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
883
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
884
- end
885
-
886
- it "should detect unused preload associations" do
887
- Page.find(:all, :include => :author).collect(&:name)
888
- Bullet::Detector::Association.should be_completely_preloading_associations
889
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
890
- Bullet::Detector::Association.should be_has_unused_preload_associations
891
- end
892
-
893
- it "should not detect unused preload associations" do
894
- Page.all.collect(&:name)
895
- Bullet::Detector::Association.should be_completely_preloading_associations
896
- Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
897
- Bullet::Detector::Association.should_not be_has_unused_preload_associations
898
- end
899
- end