Skip to content

Commit

Permalink
Merge pull request #39 from Partyista/pr-fix-tests
Browse files Browse the repository at this point in the history
Updated gem to work (and successfully test) with Rails 5.0
  • Loading branch information
rafaelfranca committed Jan 1, 2016
2 parents 64740cf + 9037bd3 commit bca5db9
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test/tmp
test/version_tmp
tmp
.ruby-version
gemfiles/*.gemfile.lock
27 changes: 24 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
language: ruby
sudo: false
cache: bundler
rvm:
- 1.9.3
before_install: gem install bundler
script: bundle exec rake test
- 1.9
- 2.0
- 2.1
- 2.2.4
- 2.3.0
- ruby-head
gemfile:
- gemfiles/rails_4_0.gemfile
- gemfiles/rails_4_1.gemfile
- gemfiles/rails_4_2.gemfile
- gemfiles/rails_5_0.gemfile
matrix:
allow_failures:
- rvm: ruby-head
fast_finish: true
exclude:
- rvm: 1.9
gemfile: gemfiles/rails_5_0.gemfile
- rvm: 2.0
gemfile: gemfiles/rails_5_0.gemfile
- rvm: 2.1
gemfile: gemfiles/rails_5_0.gemfile
notifications:
email: false
irc:
Expand Down
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in active_record-observers.gemspec
gemspec

gem 'activerecord-deprecated_finders'

gem 'mocha', require: false
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ deps = `git ls-files`.split("\n") - [specname]

file specname => deps do
files = `git ls-files`.split("\n")
test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }

require 'erb'
Expand Down
9 changes: 9 additions & 0 deletions gemfiles/rails_4_0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal"
gem "mocha", :require => false
gem "rails", "~> 4.0.0"

gemspec :path => "../"
9 changes: 9 additions & 0 deletions gemfiles/rails_4_1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal"
gem "mocha", :require => false
gem "rails", "~> 4.1.0"

gemspec :path => "../"
9 changes: 9 additions & 0 deletions gemfiles/rails_4_2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal"
gem "mocha", :require => false
gem "rails", "~> 4.2.0"

gemspec :path => "../"
9 changes: 9 additions & 0 deletions gemfiles/rails_5_0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal"
gem "mocha", :require => false
gem "rails", ">= 5.0.0.beta1", "< 5.1"

gemspec :path => "../"
4 changes: 2 additions & 2 deletions lib/rails/observers/action_controller/caching/sweeping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def cache_sweeper(*sweepers)
sweeper_instance = (sweeper.is_a?(Symbol) ? Object.const_get(sweeper.to_s.classify) : sweeper).instance

if sweeper_instance.is_a?(Sweeper)
around_filter(sweeper_instance, :only => configuration[:only])
around_action(sweeper_instance, :only => configuration[:only])
else
after_filter(sweeper_instance, :only => configuration[:only])
after_action(sweeper_instance, :only => configuration[:only])
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/rails/observers/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ class Railtie < ::Rails::Railtie
initializer "active_record.observer", :before => "active_record.set_configs" do |app|
ActiveSupport.on_load(:active_record) do
require "rails/observers/activerecord/active_record"

if observers = app.config.respond_to?(:active_record) && app.config.active_record.delete(:observers)
send :observers=, observers
end
observers = app.config.active_record.delete(:observers)
self.observers = observers if observers
end
end

Expand All @@ -21,8 +19,6 @@ class Railtie < ::Rails::Railtie

config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.instantiate_observers

ActionDispatch::Reloader.to_prepare do
ActiveRecord::Base.instantiate_observers
end
Expand Down
13 changes: 6 additions & 7 deletions rails-observers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ Gem::Specification.new do |s|
s.version = Rails::Observers::VERSION
s.license = 'MIT'

s.files = [".gitignore",".travis.yml","Gemfile","LICENSE","README.md","Rakefile","lib/generators/active_record/observer/observer_generator.rb","lib/generators/active_record/observer/templates/observer.rb","lib/generators/rails/observer/USAGE","lib/generators/rails/observer/observer_generator.rb","lib/generators/test_unit/observer/observer_generator.rb","lib/generators/test_unit/observer/templates/unit_test.rb","lib/rails-observers.rb","lib/rails/observers/action_controller/caching.rb","lib/rails/observers/action_controller/caching/sweeper.rb","lib/rails/observers/action_controller/caching/sweeping.rb","lib/rails/observers/active_model.rb","lib/rails/observers/active_model/active_model.rb","lib/rails/observers/active_model/observer_array.rb","lib/rails/observers/active_model/observing.rb","lib/rails/observers/activerecord/active_record.rb","lib/rails/observers/activerecord/base.rb","lib/rails/observers/activerecord/observer.rb","lib/rails/observers/railtie.rb","lib/rails/observers/version.rb","rails-observers.gemspec","rails-observers.gemspec.erb","test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
s.test_files = ["test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
s.files = [".gitignore",".travis.yml","Appraisals","Gemfile","LICENSE","README.md","Rakefile","gemfiles/rails_4_0.gemfile","gemfiles/rails_4_1.gemfile","gemfiles/rails_4_2.gemfile","gemfiles/rails_5_0.gemfile","lib/generators/active_record/observer/observer_generator.rb","lib/generators/active_record/observer/templates/observer.rb","lib/generators/rails/observer/USAGE","lib/generators/rails/observer/observer_generator.rb","lib/generators/test_unit/observer/observer_generator.rb","lib/generators/test_unit/observer/templates/unit_test.rb","lib/rails-observers.rb","lib/rails/observers/action_controller/caching.rb","lib/rails/observers/action_controller/caching/sweeper.rb","lib/rails/observers/action_controller/caching/sweeping.rb","lib/rails/observers/active_model.rb","lib/rails/observers/active_model/active_model.rb","lib/rails/observers/active_model/observer_array.rb","lib/rails/observers/active_model/observing.rb","lib/rails/observers/activerecord/active_record.rb","lib/rails/observers/activerecord/base.rb","lib/rails/observers/activerecord/observer.rb","lib/rails/observers/railtie.rb","lib/rails/observers/version.rb","rails-observers.gemspec","rails-observers.gemspec.erb","test/configuration_test.rb","test/console_test.rb","test/fixtures/developers.yml","test/fixtures/minimalistics.yml","test/fixtures/topics.yml","test/generators/generators_test_helper.rb","test/generators/namespaced_generators_test.rb","test/generators/observer_generator_test.rb","test/helper.rb","test/isolation/abstract_unit.rb","test/lifecycle_test.rb","test/models/observers.rb","test/observer_array_test.rb","test/observing_test.rb","test/rake_test.rb","test/sweeper_test.rb","test/transaction_callbacks_test.rb"]
s.executables = []
s.require_paths = ["lib"]

s.add_dependency 'activemodel', '>= 4.0'
s.add_dependency 'activemodel', '>= 4.0', '< 5.1'

s.add_development_dependency 'minitest', '>= 3'
s.add_development_dependency 'railties', '>= 4.0'
s.add_development_dependency 'activerecord', '>= 4.0'
s.add_development_dependency 'actionmailer', '>= 4.0'
s.add_development_dependency 'actionpack', '>= 4.0'
s.add_development_dependency 'railties', '>= 4.0', '< 5.1'
s.add_development_dependency 'activerecord', '>= 4.0', '< 5.1'
s.add_development_dependency 'actionmailer', '>= 4.0', '< 5.1'
s.add_development_dependency 'actionpack', '>= 4.0', '< 5.1'
s.add_development_dependency 'sqlite3', '~> 1.3'
end
9 changes: 4 additions & 5 deletions rails-observers.gemspec.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.files = [<%= files.map(&:inspect).join ',' %>]
s.test_files = [<%= test_files.map(&:inspect).join ',' %>]
s.executables = [<%= executables.map(&:inspect).join ',' %>]
s.require_paths = ["lib"]

s.add_dependency 'activemodel', '>= 4.0'

s.add_development_dependency 'minitest', '>= 3'
s.add_development_dependency 'railties', '>= 4.0'
s.add_development_dependency 'activerecord', '>= 4.0'
s.add_development_dependency 'actionmailer', '>= 4.0'
s.add_development_dependency 'actionpack', '>= 4.0'
s.add_development_dependency 'railties', '>= 4.0', '< 5.1'
s.add_development_dependency 'activerecord', '>= 4.0', '< 5.1'
s.add_development_dependency 'actionmailer', '>= 4.0', '< 5.1'
s.add_development_dependency 'actionpack', '>= 4.0', '< 5.1'
s.add_development_dependency 'sqlite3', '~> 1.3'
end
5 changes: 0 additions & 5 deletions test/fixtures/topics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ first:
title: The First Topic
author_name: David
author_email_address: [email protected]
written_on: 2003-07-16t15:28:11.2233+01:00
last_read: 2004-04-15
bonus_time: 2005-01-30t15:28:00.00+01:00
content: Have a nice day
approved: false
replies_count: 1
Expand All @@ -14,7 +12,6 @@ second:
id: 2
title: The Second Topic of the day
author_name: Mary
written_on: 2004-07-15t15:28:00.0099+01:00
content: Have a nice day
approved: true
replies_count: 0
Expand All @@ -25,7 +22,6 @@ third:
id: 3
title: The Third Topic of the day
author_name: Carl
written_on: 2012-08-12t20:24:22.129346+00:00
content: I'm a troll
approved: true
replies_count: 1
Expand All @@ -34,7 +30,6 @@ fourth:
id: 4
title: The Fourth Topic of the day
author_name: Carl
written_on: 2006-07-15t15:28:00.0099+01:00
content: Why not?
approved: true
type: Reply
Expand Down
2 changes: 2 additions & 0 deletions test/generators/generators_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require 'rails/generators'
require 'rails/generators/test_case'

ActiveSupport.test_order = :random if ActiveSupport.respond_to?(:test_order=)

module TestApp
class Application < Rails::Application
end
Expand Down
21 changes: 19 additions & 2 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
require 'minitest/autorun'
require 'mocha/mini_test'
require 'active_record'
require 'rails'
require 'rails/observers/activerecord/active_record'

FIXTURES_ROOT = File.expand_path(File.dirname(__FILE__)) + "/fixtures"

module Rails
class << self
def version_matches?(requirement_str)
ver = Gem::Version.new(version)
req = Gem::Requirement.new(requirement_str)
req.satisfied_by?(ver)
end
end
end

class ActiveSupport::TestCase
include ActiveRecord::TestFixtures

self.test_order = :random if self.respond_to?(:test_order=)
self.fixture_path = FIXTURES_ROOT
self.use_instantiated_fixtures = false
self.use_transactional_fixtures = true
self.use_transactional_fixtures = true if Rails.version_matches?('~> 4.0')
self.use_transactional_tests = true if self.respond_to?(:use_transactional_tests=)
end

if ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks=) && Rails.version_matches?('~> 4.0')
ActiveRecord::Base.raise_in_transactional_callbacks = true
end

ActiveRecord::Base.configurations = { "test" => { adapter: 'sqlite3', database: ':memory:' } }
Expand All @@ -33,7 +50,7 @@ class ActiveSupport::TestCase
t.string :parent_title
t.string :type
t.string :group
t.timestamps
t.timestamps :null => false
end

create_table :comments do |t|
Expand Down
10 changes: 8 additions & 2 deletions test/lifecycle_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class SalaryChecker < ActiveRecord::Observer
attr_accessor :last_saved

def before_save(developer)
return developer.salary > 80000
should_abort = developer.salary <= 80000
begin
throw :abort if should_abort
rescue
return !should_abort
end
end

module Implementation
Expand Down Expand Up @@ -72,7 +77,8 @@ def observed_class_inherited_with_testing(subclass)
@@last_inherited = subclass
end

alias_method_chain :observed_class_inherited, :testing
alias_method :observed_class_inherited_without_testing, :observed_class_inherited
alias_method :observed_class_inherited, :observed_class_inherited_with_testing

def after_find(record)
@record = record
Expand Down
18 changes: 15 additions & 3 deletions test/sweeper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class SweeperTestController < ActionController::Base
cache_sweeper :app_sweeper

def show
render text: 'hello world'
if Rails.version_matches?('>= 5.0.0.beta1')
render plain: 'hello world'
else
render text: 'hello world'
end
end

def error
Expand Down Expand Up @@ -75,8 +79,16 @@ def test_sweeper_should_not_ignore_unknown_method_calls

def test_process(controller, action = "show")
@controller = controller.is_a?(Class) ? controller.new : controller
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
if ActionController::TestRequest.respond_to?(:create)
@request = ActionController::TestRequest.create
else
@request = ActionController::TestRequest.new
end
if ActionController.constants.include?(:TestResponse)
@response = ActionController::TestResponse.new
else
@response = ActionDispatch::TestResponse.new
end

process(action)
end
Expand Down
38 changes: 28 additions & 10 deletions test/transaction_callbacks_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "helper"

class TransactionCallbacksTest < ActiveSupport::TestCase
self.use_transactional_fixtures = false
self.use_transactional_fixtures = false if Rails.version_matches?('~> 4.0')
self.use_transactional_tests = false if self.respond_to?(:use_transactional_tests=)
fixtures :topics

class TopicWithCallbacks < ActiveRecord::Base
Expand Down Expand Up @@ -227,20 +228,37 @@ def @first.last_after_transaction_error; @last_transaction_error; end
@second.after_commit_block{|r| r.history << :after_commit}
@second.after_rollback_block{|r| r.history << :after_rollback}

Topic.transaction do
@first.save!
@second.save!
action_without_rollback = Proc.new do
Topic.transaction do
@first.save!
@second.save!
end
end

if Rails.version_matches?('>= 4.2')
assert_raises RuntimeError, &action_without_rollback
else
action_without_rollback.call
end
assert_equal :commit, @first.last_after_transaction_error
assert_equal [:after_commit], @second.history

@second.history.clear
Topic.transaction do
@first.save!
@second.save!
raise ActiveRecord::Rollback
action_with_rollback = Proc.new do
Topic.transaction do
@first.save!
@second.save!
raise ActiveRecord::Rollback
end
end
if Rails.version_matches?('>= 4.2')
assert_raises RuntimeError, &action_with_rollback
assert_equal :rollback, @first.last_after_transaction_error
assert_equal [], @second.history
else
action_with_rollback.call
assert_equal :rollback, @first.last_after_transaction_error
assert_equal [:after_rollback], @second.history
end
assert_equal :rollback, @first.last_after_transaction_error
assert_equal [:after_rollback], @second.history
end
end

0 comments on commit bca5db9

Please sign in to comment.