bullet 2.3.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. data/.travis.yml +5 -0
  2. data/Gemfile +3 -1
  3. data/Gemfile.lock +44 -33
  4. data/Gemfile.rails-2.3.14 +16 -0
  5. data/Gemfile.rails-2.3.14.lock +65 -0
  6. data/Gemfile.rails-3.0.12 +17 -0
  7. data/Gemfile.rails-3.0.12.lock +116 -0
  8. data/Gemfile.rails-3.1.4 +18 -0
  9. data/Gemfile.rails-3.1.4.lock +134 -0
  10. data/README.textile +6 -6
  11. data/lib/bullet/active_record2.rb +12 -0
  12. data/lib/bullet/active_record3.rb +12 -0
  13. data/lib/bullet/detector/association.rb +11 -12
  14. data/lib/bullet/detector/counter.rb +6 -6
  15. data/lib/bullet/detector/n_plus_one_query.rb +9 -9
  16. data/lib/bullet/detector/unused_eager_association.rb +9 -9
  17. data/lib/bullet/ext/object.rb +5 -0
  18. data/lib/bullet/ext/string.rb +5 -0
  19. data/lib/bullet/mongoid.rb +56 -0
  20. data/lib/bullet/registry/object.rb +4 -6
  21. data/lib/bullet/version.rb +1 -1
  22. data/lib/bullet.rb +15 -9
  23. data/spec/bullet/detector/association_spec.rb +20 -16
  24. data/spec/bullet/detector/counter_spec.rb +8 -9
  25. data/spec/bullet/detector/n_plus_one_query_spec.rb +22 -22
  26. data/spec/bullet/detector/unused_eager_association_spec.rb +11 -11
  27. data/spec/bullet/ext/object_spec.rb +20 -0
  28. data/spec/bullet/ext/string_spec.rb +13 -0
  29. data/spec/bullet/registry/object_spec.rb +4 -4
  30. data/spec/integration/association_spec.rb +463 -401
  31. data/spec/integration/counter_spec.rb +27 -25
  32. data/spec/integration/mongoid/association_spec.rb +276 -0
  33. data/spec/integration/rails2/association_spec.rb +593 -0
  34. data/spec/integration/rails2/counter_spec.rb +39 -0
  35. data/spec/models/mongoid/address.rb +5 -0
  36. data/spec/models/mongoid/category.rb +6 -0
  37. data/spec/models/mongoid/comment.rb +5 -0
  38. data/spec/models/mongoid/company.rb +5 -0
  39. data/spec/models/mongoid/entry.rb +5 -0
  40. data/spec/models/mongoid/post.rb +8 -0
  41. data/spec/models/post.rb +12 -6
  42. data/spec/spec_helper.rb +32 -8
  43. data/spec/support/bullet_ext.rb +1 -1
  44. data/spec/support/mongo_seed.rb +41 -0
  45. data/spec/support/{seed.rb → sqlite_seed.rb} +1 -22
  46. data/test.result +2293 -0
  47. metadata +42 -22
  48. data/spec/integration/association_for_chris_spec.rb +0 -37
  49. data/spec/integration/association_for_peschkaj_spec.rb +0 -26
  50. data/spec/models/contact.rb +0 -3
  51. data/spec/models/deal.rb +0 -4
  52. data/spec/models/email.rb +0 -3
  53. data/spec/models/hotel.rb +0 -4
  54. data/spec/models/location.rb +0 -3
@@ -1,37 +1,39 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Bullet::Detector::Counter do
4
- before(:each) do
5
- Bullet.start_request
6
- end
3
+ if Rails.version.to_i > 2
4
+ describe Bullet::Detector::Counter do
5
+ before(:each) do
6
+ Bullet.start_request
7
+ end
7
8
 
8
- after(:each) do
9
- Bullet.end_request
10
- end
9
+ after(:each) do
10
+ Bullet.end_request
11
+ end
11
12
 
12
- it "should need counter cache with all cities" do
13
- Country.all.each do |country|
14
- country.cities.size
13
+ it "should need counter cache with all cities" do
14
+ Country.all.each do |country|
15
+ country.cities.size
16
+ end
17
+ Bullet.collected_counter_cache_notifications.should_not be_empty
15
18
  end
16
- Bullet.collected_counter_cache_notifications.should_not be_empty
17
- end
18
19
 
19
- it "should not need counter cache if already define counter_cache" do
20
- Person.all.each do |person|
21
- person.pets.size
20
+ it "should not need counter cache if already define counter_cache" do
21
+ Person.all.each do |person|
22
+ person.pets.size
23
+ end
24
+ Bullet.collected_counter_cache_notifications.should be_empty
22
25
  end
23
- Bullet.collected_counter_cache_notifications.should be_empty
24
- end
25
26
 
26
- it "should not need counter cache with only one object" do
27
- Country.first.cities.size
28
- Bullet.collected_counter_cache_notifications.should be_empty
29
- end
27
+ it "should not need counter cache with only one object" do
28
+ Country.first.cities.size
29
+ Bullet.collected_counter_cache_notifications.should be_empty
30
+ end
30
31
 
31
- it "should not need counter cache with part of cities" do
32
- Country.all.each do |country|
33
- country.cities.where(:name => 'first').size
32
+ it "should not need counter cache with part of cities" do
33
+ Country.all.each do |country|
34
+ country.cities.where(:name => 'first').size
35
+ end
36
+ Bullet.collected_counter_cache_notifications.should be_empty
34
37
  end
35
- Bullet.collected_counter_cache_notifications.should be_empty
36
38
  end
37
39
  end
@@ -0,0 +1,276 @@
1
+ require 'spec_helper'
2
+
3
+ begin
4
+ require 'mongoid'
5
+ describe Bullet::Detector::Association, 'has_many' do
6
+ before(:each) do
7
+ Bullet.clear
8
+ Bullet.start_request
9
+ end
10
+ after(:each) do
11
+ Bullet.end_request
12
+ end
13
+
14
+ context "posts => comments" do
15
+ it "should detect non preload posts => comments" do
16
+ Mongoid::Post.all.each do |post|
17
+ post.comments.map(&:name)
18
+ end
19
+ Bullet::Detector::UnusedEagerAssociation.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(Mongoid::Post, :comments)
23
+ end
24
+
25
+ it "should detect preload post => comments" do
26
+ Mongoid::Post.includes(:comments).each do |post|
27
+ post.comments.map(&:name)
28
+ end
29
+ Bullet::Detector::UnusedEagerAssociation.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
+ Mongoid::Post.includes(:comments).map(&:name)
37
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
38
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::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
+ Mongoid::Post.all.map(&:name)
45
+ Bullet::Detector::UnusedEagerAssociation.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, category => entries" do
53
+ it "should detect non preload with category => [posts, entries]" do
54
+ Mongoid::Category.all.each do |category|
55
+ category.posts.map(&:name)
56
+ category.entries.map(&:name)
57
+ end
58
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
59
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
60
+
61
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :posts)
62
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
63
+ end
64
+
65
+ it "should detect preload with category => posts, but not with category => entries" do
66
+ Mongoid::Category.includes(:posts).each do |category|
67
+ category.posts.collect(&:name)
68
+ category.entries.collect(&:name)
69
+ end
70
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
71
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
72
+
73
+ Bullet::Detector::Association.should_not be_detecting_unpreloaded_association_for(Mongoid::Category, :posts)
74
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Category, :entries)
75
+ end
76
+
77
+ it "should detect preload with category => [posts, entries]" do
78
+ Mongoid::Category.includes([:posts, :entries]).each do |category|
79
+ category.posts.map(&:name)
80
+ category.entries.map(&:name)
81
+ end
82
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
83
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
84
+
85
+ Bullet::Detector::Association.should be_completely_preloading_associations
86
+ end
87
+
88
+ it "should detect unused preload with category => [posts, entries]" do
89
+ Mongoid::Category.includes([:posts, :entries]).map(&:name)
90
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
91
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :posts)
92
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :entries)
93
+
94
+ Bullet::Detector::Association.should be_completely_preloading_associations
95
+ end
96
+
97
+ it "should detect unused preload with category => entries, but not with category => posts" do
98
+ Mongoid::Category.includes([:posts, :entries]).each do |category|
99
+ category.posts.map(&:name)
100
+ end
101
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
102
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Mongoid::Category, :posts)
103
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Category, :entries)
104
+
105
+ Bullet::Detector::Association.should be_completely_preloading_associations
106
+ end
107
+ end
108
+
109
+ context "post => comment" do
110
+ it "should detect unused preload with post => comments" do
111
+ Mongoid::Post.includes(:comments).each do |post|
112
+ post.comments.first.name
113
+ end
114
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
115
+ Bullet::Detector::Association.should_not be_unused_preload_associations_for(Mongoid::Post, :comments)
116
+
117
+ Bullet::Detector::Association.should be_completely_preloading_associations
118
+ end
119
+
120
+ it "should detect preload with post => commnets" do
121
+ Mongoid::Post.first.comments.collect(&:name)
122
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
123
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
124
+
125
+ Bullet::Detector::Association.should be_completely_preloading_associations
126
+ end
127
+ end
128
+
129
+ context "scope preload_comments" do
130
+ it "should detect preload post => comments with scope" do
131
+ Mongoid::Post.preload_comments.each do |post|
132
+ post.comments.map(&:name)
133
+ end
134
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
135
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
136
+
137
+ Bullet::Detector::Association.should be_completely_preloading_associations
138
+ end
139
+
140
+ it "should detect unused preload with scope" do
141
+ Mongoid::Post.preload_comments.map(&:name)
142
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
143
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Post, :comments)
144
+
145
+ Bullet::Detector::Association.should be_completely_preloading_associations
146
+ end
147
+ end
148
+ end
149
+
150
+ describe Bullet::Detector::Association, 'belongs_to' do
151
+ before(:each) do
152
+ Bullet.clear
153
+ Bullet.start_request
154
+ end
155
+
156
+ after(:each) do
157
+ Bullet.end_request
158
+ end
159
+
160
+ context "comment => post" do
161
+ it "should detect non preload with comment => post" do
162
+ Mongoid::Comment.all.each do |comment|
163
+ comment.post.name
164
+ end
165
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
166
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
167
+
168
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Comment, :post)
169
+ end
170
+
171
+ it "should detect preload with one comment => post" do
172
+ Mongoid::Comment.first.post.name
173
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
174
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
175
+
176
+ Bullet::Detector::Association.should be_completely_preloading_associations
177
+ end
178
+
179
+ it "should detect preload with comment => post" do
180
+ Mongoid::Comment.includes(:post).each do |comment|
181
+ comment.post.name
182
+ end
183
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
184
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
185
+
186
+ Bullet::Detector::Association.should be_completely_preloading_associations
187
+ end
188
+
189
+ it "should not detect preload with comment => post" do
190
+ Mongoid::Comment.all.collect(&:name)
191
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
192
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
193
+
194
+ Bullet::Detector::Association.should be_completely_preloading_associations
195
+ end
196
+
197
+ it "should detect unused preload with comments => post" do
198
+ Mongoid::Comment.includes(:post).map(&:name)
199
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
200
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Comment, :post)
201
+
202
+ Bullet::Detector::Association.should be_completely_preloading_associations
203
+ end
204
+ end
205
+ end
206
+
207
+ describe Bullet::Detector::Association, "has_one" do
208
+ before(:each) do
209
+ Bullet.clear
210
+ Bullet.start_request
211
+ end
212
+
213
+ after(:each) do
214
+ Bullet.end_request
215
+ end
216
+
217
+ context "company => address" do
218
+ it "should detect non preload association" do
219
+ Mongoid::Company.all.each do |company|
220
+ company.address.name
221
+ end
222
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
223
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
224
+
225
+ Bullet::Detector::Association.should be_detecting_unpreloaded_association_for(Mongoid::Company, :address)
226
+ end
227
+
228
+ it "should detect preload association" do
229
+ Mongoid::Company.includes(:address).each do |company|
230
+ company.address.name
231
+ end
232
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
233
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
234
+
235
+ Bullet::Detector::Association.should be_completely_preloading_associations
236
+ end
237
+
238
+ it "should not detect preload association" do
239
+ Mongoid::Company.all.collect(&:name)
240
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
241
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
242
+
243
+ Bullet::Detector::Association.should be_completely_preloading_associations
244
+ end
245
+
246
+ it "should detect unused preload association" do
247
+ Mongoid::Company.includes(:address).collect(&:name)
248
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
249
+ Bullet::Detector::Association.should be_unused_preload_associations_for(Mongoid::Company, :address)
250
+
251
+ Bullet::Detector::Association.should be_completely_preloading_associations
252
+ end
253
+ end
254
+ end
255
+
256
+ describe Bullet::Detector::Association, "call one association that in possible objects" do
257
+ before(:each) do
258
+ Bullet.clear
259
+ Bullet.start_request
260
+ end
261
+
262
+ after(:each) do
263
+ Bullet.end_request
264
+ end
265
+
266
+ it "should not detect preload association" do
267
+ Mongoid::Post.all
268
+ Mongoid::Post.first.comments.map(&:name)
269
+ Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations
270
+ Bullet::Detector::Association.should_not be_has_unused_preload_associations
271
+
272
+ Bullet::Detector::Association.should be_completely_preloading_associations
273
+ end
274
+ end
275
+ rescue LoadError
276
+ end