rails-observers 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,183 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'active_model'
3
- require 'rails/observers/active_model/active_model'
4
-
5
- class ObservedModel
6
- include ActiveModel::Observing
7
-
8
- class Observer
9
- end
10
- end
11
-
12
- class FooObserver < ActiveModel::Observer
13
- class << self
14
- public :new
15
- end
16
-
17
- attr_accessor :stub
18
-
19
- def on_spec(record, *args)
20
- stub.event_with(record, *args) if stub
21
- end
22
-
23
- def around_save(record)
24
- yield :in_around_save
25
- end
26
- end
27
-
28
- class Foo
29
- include ActiveModel::Observing
30
- end
31
-
32
- class ObservingTest < ActiveModel::TestCase
33
- def setup
34
- ObservedModel.observers.clear
35
- end
36
-
37
- test "initializes model with no cached observers" do
38
- assert ObservedModel.observers.empty?, "Not empty: #{ObservedModel.observers.inspect}"
39
- end
40
-
41
- test "stores cached observers in an array" do
42
- ObservedModel.observers << :foo
43
- assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}"
44
- end
45
-
46
- test "flattens array of assigned cached observers" do
47
- ObservedModel.observers = [[:foo], :bar]
48
- assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}"
49
- assert ObservedModel.observers.include?(:bar), ":bar not in #{ObservedModel.observers.inspect}"
50
- end
51
-
52
- test "uses an ObserverArray so observers can be disabled" do
53
- ObservedModel.observers = [:foo, :bar]
54
- assert ObservedModel.observers.is_a?(ActiveModel::ObserverArray)
55
- end
56
-
57
- test "instantiates observer names passed as strings" do
58
- ObservedModel.observers << 'foo_observer'
59
- FooObserver.expects(:instance)
60
- ObservedModel.instantiate_observers
61
- end
62
-
63
- test "instantiates observer names passed as symbols" do
64
- ObservedModel.observers << :foo_observer
65
- FooObserver.expects(:instance)
66
- ObservedModel.instantiate_observers
67
- end
68
-
69
- test "instantiates observer classes" do
70
- ObservedModel.observers << ObservedModel::Observer
71
- ObservedModel::Observer.expects(:instance)
72
- ObservedModel.instantiate_observers
73
- end
74
-
75
- test "raises an appropriate error when a developer accidentally adds the wrong class (i.e. Widget instead of WidgetObserver)" do
76
- assert_raise ArgumentError do
77
- ObservedModel.observers = ['string']
78
- ObservedModel.instantiate_observers
79
- end
80
- assert_raise ArgumentError do
81
- ObservedModel.observers = [:string]
82
- ObservedModel.instantiate_observers
83
- end
84
- assert_raise ArgumentError do
85
- ObservedModel.observers = [String]
86
- ObservedModel.instantiate_observers
87
- end
88
- end
89
-
90
- test "passes observers to subclasses" do
91
- FooObserver.instance
92
- bar = Class.new(Foo)
93
- assert_equal Foo.observers_count, bar.observers_count
94
- end
95
- end
96
-
97
- class ObserverTest < ActiveModel::TestCase
98
- def setup
99
- ObservedModel.observers = :foo_observer
100
- FooObserver.singleton_class.instance_eval do
101
- alias_method :original_observed_classes, :observed_classes
102
- end
103
- end
104
-
105
- def teardown
106
- FooObserver.singleton_class.instance_eval do
107
- undef_method :observed_classes
108
- alias_method :observed_classes, :original_observed_classes
109
- end
110
- end
111
-
112
- test "guesses implicit observable model name" do
113
- assert_equal Foo, FooObserver.observed_class
114
- end
115
-
116
- test "tracks implicit observable models" do
117
- instance = FooObserver.new
118
- assert_equal [Foo], instance.observed_classes
119
- end
120
-
121
- test "tracks explicit observed model class" do
122
- FooObserver.observe ObservedModel
123
- instance = FooObserver.new
124
- assert_equal [ObservedModel], instance.observed_classes
125
- end
126
-
127
- test "tracks explicit observed model as string" do
128
- FooObserver.observe 'observed_model'
129
- instance = FooObserver.new
130
- assert_equal [ObservedModel], instance.observed_classes
131
- end
132
-
133
- test "tracks explicit observed model as symbol" do
134
- FooObserver.observe :observed_model
135
- instance = FooObserver.new
136
- assert_equal [ObservedModel], instance.observed_classes
137
- end
138
-
139
- test "calls existing observer event" do
140
- foo = Foo.new
141
- FooObserver.instance.stub = stub
142
- FooObserver.instance.stub.expects(:event_with).with(foo)
143
- Foo.notify_observers(:on_spec, foo)
144
- end
145
-
146
- test "calls existing observer event from the instance" do
147
- foo = Foo.new
148
- FooObserver.instance.stub = stub
149
- FooObserver.instance.stub.expects(:event_with).with(foo)
150
- foo.notify_observers(:on_spec)
151
- end
152
-
153
- test "passes extra arguments" do
154
- foo = Foo.new
155
- FooObserver.instance.stub = stub
156
- FooObserver.instance.stub.expects(:event_with).with(foo, :bar)
157
- Foo.send(:notify_observers, :on_spec, foo, :bar)
158
- end
159
-
160
- test "skips nonexistent observer event" do
161
- foo = Foo.new
162
- Foo.notify_observers(:whatever, foo)
163
- end
164
-
165
- test "update passes a block on to the observer" do
166
- yielded_value = nil
167
- FooObserver.instance.update(:around_save, Foo.new) do |val|
168
- yielded_value = val
169
- end
170
- assert_equal :in_around_save, yielded_value
171
- end
172
-
173
- test "observe redefines observed_classes class method" do
174
- class BarObserver < ActiveModel::Observer
175
- observe :foo
176
- end
177
-
178
- assert_equal [Foo], BarObserver.observed_classes
179
-
180
- BarObserver.observe(ObservedModel)
181
- assert_equal [ObservedModel], BarObserver.observed_classes
182
- end
183
- end
@@ -1,40 +0,0 @@
1
- require 'isolation/abstract_unit'
2
- require 'rails-observers'
3
-
4
- module ApplicationTests
5
- class RakeTest < ActiveSupport::TestCase
6
- include ActiveSupport::Testing::Isolation
7
-
8
- def setup
9
- build_app
10
- boot_rails
11
- FileUtils.rm_rf("#{app_path}/config/environments")
12
- end
13
-
14
- def teardown
15
- teardown_app
16
- end
17
-
18
- def test_load_activerecord_base_when_we_use_observers
19
- Dir.chdir(app_path) do
20
- `bundle exec rails g model user;
21
- bundle exec rake db:migrate;
22
- bundle exec rails g observer user;`
23
-
24
- add_to_config "config.active_record.observers = :user_observer"
25
-
26
- assert_equal "0", `bundle exec rails r "puts User.count"`.strip
27
-
28
- app_file "lib/tasks/count_user.rake", <<-RUBY
29
- namespace :user do
30
- task :count => :environment do
31
- puts User.count
32
- end
33
- end
34
- RUBY
35
-
36
- assert_equal "0", `bundle exec rake user:count`.strip
37
- end
38
- end
39
- end
40
- end
@@ -1,83 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'action_controller'
3
- require 'active_record'
4
- require 'rails/observers/activerecord/active_record'
5
- require 'rails/observers/action_controller/caching'
6
-
7
- SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
8
-
9
- class AppSweeper < ActionController::Caching::Sweeper; end
10
-
11
- class SweeperTestController < ActionController::Base
12
- include SharedTestRoutes.url_helpers
13
-
14
- cache_sweeper :app_sweeper
15
-
16
- def show
17
- render text: 'hello world'
18
- end
19
-
20
- def error
21
- raise StandardError.new
22
- end
23
- end
24
-
25
- class SweeperTest < ActionController::TestCase
26
- def setup
27
- @routes = SharedTestRoutes
28
-
29
- @routes.draw do
30
- get ':controller(/:action)'
31
- end
32
-
33
- super
34
- end
35
-
36
- def test_sweeper_should_not_ignore_no_method_error
37
- sweeper = ActionController::Caching::Sweeper.send(:new)
38
- assert_raise NoMethodError do
39
- sweeper.send_not_defined
40
- end
41
- end
42
-
43
- def test_sweeper_should_not_block_rendering
44
- response = test_process(SweeperTestController)
45
- assert_equal 'hello world', response.body
46
- end
47
-
48
- def test_sweeper_should_clean_up_if_exception_is_raised
49
- assert_raise StandardError do
50
- test_process(SweeperTestController, 'error')
51
- end
52
- assert_nil AppSweeper.instance.controller
53
- end
54
-
55
- def test_before_method_of_sweeper_should_always_return_true
56
- sweeper = ActionController::Caching::Sweeper.send(:new)
57
- assert sweeper.before(SweeperTestController.new)
58
- end
59
-
60
- def test_after_method_of_sweeper_should_always_return_nil
61
- sweeper = ActionController::Caching::Sweeper.send(:new)
62
- assert_nil sweeper.after(SweeperTestController.new)
63
- end
64
-
65
- def test_sweeper_should_not_ignore_unknown_method_calls
66
- sweeper = ActionController::Caching::Sweeper.send(:new)
67
- assert_raise NameError do
68
- sweeper.instance_eval do
69
- some_method_that_doesnt_exist
70
- end
71
- end
72
- end
73
-
74
- private
75
-
76
- def test_process(controller, action = "show")
77
- @controller = controller.is_a?(Class) ? controller.new : controller
78
- @request = ActionController::TestRequest.new
79
- @response = ActionController::TestResponse.new
80
-
81
- process(action)
82
- end
83
- end
@@ -1,246 +0,0 @@
1
- require "helper"
2
-
3
- class TransactionCallbacksTest < ActiveRecord::TestCase
4
- self.use_transactional_fixtures = false
5
- fixtures :topics
6
-
7
- class TopicWithCallbacks < ActiveRecord::Base
8
- self.table_name = :topics
9
-
10
- after_commit{|record| record.send(:do_after_commit, nil)}
11
- after_commit(:on => :create){|record| record.send(:do_after_commit, :create)}
12
- after_commit(:on => :update){|record| record.send(:do_after_commit, :update)}
13
- after_commit(:on => :destroy){|record| record.send(:do_after_commit, :destroy)}
14
- after_rollback{|record| record.send(:do_after_rollback, nil)}
15
- after_rollback(:on => :create){|record| record.send(:do_after_rollback, :create)}
16
- after_rollback(:on => :update){|record| record.send(:do_after_rollback, :update)}
17
- after_rollback(:on => :destroy){|record| record.send(:do_after_rollback, :destroy)}
18
-
19
- def history
20
- @history ||= []
21
- end
22
-
23
- def after_commit_block(on = nil, &block)
24
- @after_commit ||= {}
25
- @after_commit[on] ||= []
26
- @after_commit[on] << block
27
- end
28
-
29
- def after_rollback_block(on = nil, &block)
30
- @after_rollback ||= {}
31
- @after_rollback[on] ||= []
32
- @after_rollback[on] << block
33
- end
34
-
35
- def do_after_commit(on)
36
- blocks = @after_commit[on] if defined?(@after_commit)
37
- blocks.each{|b| b.call(self)} if blocks
38
- end
39
-
40
- def do_after_rollback(on)
41
- blocks = @after_rollback[on] if defined?(@after_rollback)
42
- blocks.each{|b| b.call(self)} if blocks
43
- end
44
- end
45
-
46
- def setup
47
- @first, @second = TopicWithCallbacks.find(1, 3).sort_by { |t| t.id }
48
- end
49
-
50
- def test_call_after_commit_after_transaction_commits
51
- @first.after_commit_block{|r| r.history << :after_commit}
52
- @first.after_rollback_block{|r| r.history << :after_rollback}
53
-
54
- @first.save!
55
- assert_equal [:after_commit], @first.history
56
- end
57
-
58
- def test_only_call_after_commit_on_update_after_transaction_commits_for_existing_record
59
- @first.after_commit_block(:create){|r| r.history << :commit_on_create}
60
- @first.after_commit_block(:update){|r| r.history << :commit_on_update}
61
- @first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
62
- @first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
63
- @first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
64
- @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
65
-
66
- @first.save!
67
- assert_equal [:commit_on_update], @first.history
68
- end
69
-
70
- def test_only_call_after_commit_on_destroy_after_transaction_commits_for_destroyed_record
71
- @first.after_commit_block(:create){|r| r.history << :commit_on_create}
72
- @first.after_commit_block(:update){|r| r.history << :commit_on_update}
73
- @first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
74
- @first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
75
- @first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
76
- @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
77
-
78
- @first.destroy
79
- assert_equal [:commit_on_destroy], @first.history
80
- end
81
-
82
- def test_only_call_after_commit_on_create_after_transaction_commits_for_new_record
83
- @new_record = TopicWithCallbacks.new(:title => "New topic", :written_on => Date.today)
84
- @new_record.after_commit_block(:create){|r| r.history << :commit_on_create}
85
- @new_record.after_commit_block(:update){|r| r.history << :commit_on_update}
86
- @new_record.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
87
- @new_record.after_rollback_block(:create){|r| r.history << :rollback_on_create}
88
- @new_record.after_rollback_block(:update){|r| r.history << :rollback_on_update}
89
- @new_record.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
90
-
91
- @new_record.save!
92
- assert_equal [:commit_on_create], @new_record.history
93
- end
94
-
95
- def test_call_after_rollback_after_transaction_rollsback
96
- @first.after_commit_block{|r| r.history << :after_commit}
97
- @first.after_rollback_block{|r| r.history << :after_rollback}
98
-
99
- Topic.transaction do
100
- @first.save!
101
- raise ActiveRecord::Rollback
102
- end
103
-
104
- assert_equal [:after_rollback], @first.history
105
- end
106
-
107
- def test_only_call_after_rollback_on_update_after_transaction_rollsback_for_existing_record
108
- @first.after_commit_block(:create){|r| r.history << :commit_on_create}
109
- @first.after_commit_block(:update){|r| r.history << :commit_on_update}
110
- @first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
111
- @first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
112
- @first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
113
- @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
114
-
115
- Topic.transaction do
116
- @first.save!
117
- raise ActiveRecord::Rollback
118
- end
119
-
120
- assert_equal [:rollback_on_update], @first.history
121
- end
122
-
123
- def test_only_call_after_rollback_on_destroy_after_transaction_rollsback_for_destroyed_record
124
- @first.after_commit_block(:create){|r| r.history << :commit_on_create}
125
- @first.after_commit_block(:update){|r| r.history << :commit_on_update}
126
- @first.after_commit_block(:destroy){|r| r.history << :commit_on_update}
127
- @first.after_rollback_block(:create){|r| r.history << :rollback_on_create}
128
- @first.after_rollback_block(:update){|r| r.history << :rollback_on_update}
129
- @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
130
-
131
- Topic.transaction do
132
- @first.destroy
133
- raise ActiveRecord::Rollback
134
- end
135
-
136
- assert_equal [:rollback_on_destroy], @first.history
137
- end
138
-
139
- def test_only_call_after_rollback_on_create_after_transaction_rollsback_for_new_record
140
- @new_record = TopicWithCallbacks.new(:title => "New topic", :written_on => Date.today)
141
- @new_record.after_commit_block(:create){|r| r.history << :commit_on_create}
142
- @new_record.after_commit_block(:update){|r| r.history << :commit_on_update}
143
- @new_record.after_commit_block(:destroy){|r| r.history << :commit_on_destroy}
144
- @new_record.after_rollback_block(:create){|r| r.history << :rollback_on_create}
145
- @new_record.after_rollback_block(:update){|r| r.history << :rollback_on_update}
146
- @new_record.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy}
147
-
148
- Topic.transaction do
149
- @new_record.save!
150
- raise ActiveRecord::Rollback
151
- end
152
-
153
- assert_equal [:rollback_on_create], @new_record.history
154
- end
155
-
156
- def test_call_after_rollback_when_commit_fails
157
- TopicWithCallbacks.connection.class.send(:alias_method, :real_method_commit_db_transaction, :commit_db_transaction)
158
- begin
159
- TopicWithCallbacks.connection.class.class_eval do
160
- def commit_db_transaction; raise "boom!"; end
161
- end
162
-
163
- @first.after_commit_block{|r| r.history << :after_commit}
164
- @first.after_rollback_block{|r| r.history << :after_rollback}
165
-
166
- assert !@first.save rescue nil
167
- assert_equal [:after_rollback], @first.history
168
- ensure
169
- TopicWithCallbacks.connection.class.send(:remove_method, :commit_db_transaction)
170
- TopicWithCallbacks.connection.class.send(:alias_method, :commit_db_transaction, :real_method_commit_db_transaction)
171
- end
172
- end
173
-
174
- def test_only_call_after_rollback_on_records_rolled_back_to_a_savepoint
175
- def @first.rollbacks(i=0); @rollbacks ||= 0; @rollbacks += i if i; end
176
- def @first.commits(i=0); @commits ||= 0; @commits += i if i; end
177
- @first.after_rollback_block{|r| r.rollbacks(1)}
178
- @first.after_commit_block{|r| r.commits(1)}
179
-
180
- def @second.rollbacks(i=0); @rollbacks ||= 0; @rollbacks += i if i; end
181
- def @second.commits(i=0); @commits ||= 0; @commits += i if i; end
182
- @second.after_rollback_block{|r| r.rollbacks(1)}
183
- @second.after_commit_block{|r| r.commits(1)}
184
-
185
- Topic.transaction do
186
- @first.save!
187
- Topic.transaction(:requires_new => true) do
188
- @second.save!
189
- raise ActiveRecord::Rollback
190
- end
191
- end
192
-
193
- assert_equal 1, @first.commits
194
- assert_equal 0, @first.rollbacks
195
- assert_equal 0, @second.commits
196
- assert_equal 1, @second.rollbacks
197
- end
198
-
199
- def test_only_call_after_rollback_on_records_rolled_back_to_a_savepoint_when_release_savepoint_fails
200
- def @first.rollbacks(i=0); @rollbacks ||= 0; @rollbacks += i if i; end
201
- def @first.commits(i=0); @commits ||= 0; @commits += i if i; end
202
-
203
- @first.after_rollback_block{|r| r.rollbacks(1)}
204
- @first.after_commit_block{|r| r.commits(1)}
205
-
206
- Topic.transaction do
207
- @first.save
208
- Topic.transaction(:requires_new => true) do
209
- @first.save!
210
- raise ActiveRecord::Rollback
211
- end
212
- Topic.transaction(:requires_new => true) do
213
- @first.save!
214
- raise ActiveRecord::Rollback
215
- end
216
- end
217
-
218
- assert_equal 1, @first.commits
219
- assert_equal 2, @first.rollbacks
220
- end
221
-
222
- def test_after_transaction_callbacks_should_prevent_callbacks_from_being_called
223
- def @first.last_after_transaction_error=(e); @last_transaction_error = e; end
224
- def @first.last_after_transaction_error; @last_transaction_error; end
225
- @first.after_commit_block{|r| r.last_after_transaction_error = :commit; raise "fail!";}
226
- @first.after_rollback_block{|r| r.last_after_transaction_error = :rollback; raise "fail!";}
227
- @second.after_commit_block{|r| r.history << :after_commit}
228
- @second.after_rollback_block{|r| r.history << :after_rollback}
229
-
230
- Topic.transaction do
231
- @first.save!
232
- @second.save!
233
- end
234
- assert_equal :commit, @first.last_after_transaction_error
235
- assert_equal [:after_commit], @second.history
236
-
237
- @second.history.clear
238
- Topic.transaction do
239
- @first.save!
240
- @second.save!
241
- raise ActiveRecord::Rollback
242
- end
243
- assert_equal :rollback, @first.last_after_transaction_error
244
- assert_equal [:after_rollback], @second.history
245
- end
246
- end