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,136 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
-
3
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
-
5
- describe Bullet::Detector::Counter do
6
- def setup_db
7
- ActiveRecord::Schema.define(:version => 1) do
8
- create_table :countries do |t|
9
- t.string :name
10
- end
11
-
12
- create_table :cities do |t|
13
- t.string :name
14
- t.integer :country_id
15
- end
16
- end
17
- end
18
-
19
- def teardown_db
20
- ActiveRecord::Base.connection.tables.each do |table|
21
- ActiveRecord::Base.connection.drop_table(table)
22
- end
23
- end
24
-
25
- class Country < ActiveRecord::Base
26
- has_many :cities
27
- end
28
-
29
- class City < ActiveRecord::Base
30
- belongs_to :country
31
- end
32
-
33
- before(:all) do
34
- setup_db
35
-
36
- country1 = Country.create(:name => 'first')
37
- country2 = Country.create(:name => 'second')
38
-
39
- country1.cities.create(:name => 'first')
40
- country1.cities.create(:name => 'second')
41
- country2.cities.create(:name => 'third')
42
- country2.cities.create(:name => 'fourth')
43
- end
44
-
45
- after(:all) do
46
- teardown_db
47
- end
48
-
49
- before(:each) do
50
- Bullet.start_request
51
- end
52
-
53
- after(:each) do
54
- Bullet.end_request
55
- end
56
-
57
- it "should need counter cache with all cities" do
58
- Country.all.each do |country|
59
- country.cities.size
60
- end
61
- Bullet.collected_counter_cache_notifications.should_not be_empty
62
- end
63
-
64
- it "should not need coounter cache with only one object" do
65
- Country.first.cities.size
66
- Bullet.collected_counter_cache_notifications.should be_empty
67
- end
68
-
69
- it "should not need counter cache with part of cities" do
70
- Country.all.each do |country|
71
- country.cities.where(:name => 'first').size
72
- end
73
- Bullet.collected_counter_cache_notifications.should be_empty
74
- end
75
- end
76
-
77
- describe Bullet::Detector::Counter do
78
- def setup_db
79
- ActiveRecord::Schema.define(:version => 1) do
80
- create_table :people do |t|
81
- t.string :name
82
- t.integer :pets_count
83
- end
84
-
85
- create_table :pets do |t|
86
- t.string :name
87
- t.integer :person_id
88
- end
89
- end
90
- end
91
-
92
- def teardown_db
93
- ActiveRecord::Base.connection.tables.each do |table|
94
- ActiveRecord::Base.connection.drop_table(table)
95
- end
96
- end
97
-
98
- class Person < ActiveRecord::Base
99
- has_many :pets
100
- end
101
-
102
- class Pet < ActiveRecord::Base
103
- belongs_to :person, :counter_cache => true
104
- end
105
-
106
- before(:all) do
107
- setup_db
108
-
109
- person1 = Person.create(:name => 'first')
110
- person2 = Person.create(:name => 'second')
111
-
112
- person1.pets.create(:name => 'first')
113
- person1.pets.create(:name => 'second')
114
- person2.pets.create(:name => 'third')
115
- person2.pets.create(:name => 'fourth')
116
- end
117
-
118
- after(:all) do
119
- teardown_db
120
- end
121
-
122
- before(:each) do
123
- Bullet.start_request
124
- end
125
-
126
- after(:each) do
127
- Bullet.end_request
128
- end
129
-
130
- it "should not need counter cache" do
131
- Person.all.each do |person|
132
- person.pets.size
133
- end
134
- Bullet.collected_counter_cache_notifications.should be_empty
135
- end
136
- end
@@ -1,79 +0,0 @@
1
- require 'rubygems'
2
- require 'rspec'
3
- require 'rspec/autorun'
4
- require 'rails'
5
- require 'active_record'
6
- require 'action_controller'
7
-
8
- module Rails
9
- class <<self
10
- def root
11
- File.expand_path(__FILE__).split('/')[0..-3].join('/')
12
- end
13
- end
14
- end
15
-
16
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
17
- require 'bullet'
18
- Bullet.enable = true
19
- ActiveRecord::Migration.verbose = false
20
-
21
- module Bullet
22
- def self.collected_notifications_of_class( notification_class )
23
- Bullet.notification_collector.collection.select do |notification|
24
- notification.is_a? notification_class
25
- end
26
- end
27
-
28
- def self.collected_counter_cache_notifications
29
- collected_notifications_of_class Bullet::Notification::CounterCache
30
- end
31
-
32
- def self.collected_n_plus_one_query_notifications
33
- collected_notifications_of_class Bullet::Notification::NPlusOneQuery
34
- end
35
-
36
- def self.collected_unused_eager_association_notifications
37
- collected_notifications_of_class Bullet::Notification::UnusedEagerLoading
38
- end
39
- end
40
-
41
- module Bullet
42
- module Detector
43
- class Association
44
- class <<self
45
- # returns true if all associations are preloaded
46
- def completely_preloading_associations?
47
- Bullet.collected_n_plus_one_query_notifications.empty?
48
- end
49
-
50
- def has_unused_preload_associations?
51
- Bullet.collected_unused_eager_association_notifications.present?
52
- end
53
-
54
- # returns true if a given object has a specific association
55
- def creating_object_association_for?(object, association)
56
- object_associations[object].present? && object_associations[object].include?(association)
57
- end
58
-
59
- # returns true if a given class includes the specific unpreloaded association
60
- def detecting_unpreloaded_association_for?(klass, association)
61
- for_class_and_assoc = Bullet.collected_n_plus_one_query_notifications.select do |notification|
62
- notification.base_class == klass and
63
- notification.associations.include?( association )
64
- end
65
- for_class_and_assoc.present?
66
- end
67
-
68
- # returns true if the given class includes the specific unused preloaded association
69
- def unused_preload_associations_for?(klazz, association)
70
- for_class_and_assoc = Bullet.collected_unused_eager_association_notifications.select do |notification|
71
- notification.base_class == klass and
72
- notification.associations.include?( association )
73
- end
74
- for_class_and_assoc.present?
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,9 +0,0 @@
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