2015-09-13 13:39:58 -04:00
|
|
|
begin
|
|
|
|
require 'pry-nav'
|
|
|
|
rescue LoadError
|
|
|
|
# It's OK, we don't include pry in e.g. gemfiles/3.0.gemfile
|
|
|
|
end
|
|
|
|
|
2011-02-08 12:16:35 -05:00
|
|
|
ENV["RAILS_ENV"] = "test"
|
2014-03-14 16:11:10 -04:00
|
|
|
ENV["DB"] ||= "sqlite"
|
2014-03-20 16:17:11 -04:00
|
|
|
|
|
|
|
unless File.exists?(File.expand_path('../../test/dummy/config/database.yml', __FILE__))
|
|
|
|
warn "WARNING: No database.yml detected for the dummy app, please run `rake prepare` first"
|
|
|
|
end
|
|
|
|
|
2014-05-09 11:05:58 -04:00
|
|
|
def using_mysql?
|
|
|
|
@using_mysql ||= ActiveRecord::Base.connection_config[:adapter].to_sym == :mysql2
|
|
|
|
end
|
|
|
|
|
2011-02-08 12:16:35 -05:00
|
|
|
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
|
|
|
require "rails/test_help"
|
|
|
|
require 'shoulda'
|
2013-01-22 18:13:58 -05:00
|
|
|
require 'ffaker'
|
2014-10-30 08:25:32 -04:00
|
|
|
require 'database_cleaner'
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2016-02-15 18:13:45 -05:00
|
|
|
def active_record_gem_version
|
|
|
|
Gem::Version.new(ActiveRecord::VERSION::STRING)
|
|
|
|
end
|
|
|
|
|
|
|
|
if active_record_gem_version >= Gem::Version.new('5.0.0.beta1')
|
2016-01-11 22:07:57 -05:00
|
|
|
# See https://github.com/rails/rails-controller-testing/issues/5
|
|
|
|
ActionController::TestCase.send(:include, Rails::Controller::Testing::TestProcess)
|
|
|
|
end
|
|
|
|
|
2013-10-03 10:06:24 -04:00
|
|
|
Rails.backtrace_cleaner.remove_silencers!
|
|
|
|
|
2011-02-08 12:16:35 -05:00
|
|
|
# Run any available migration
|
|
|
|
ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
|
2010-03-18 14:02:55 -04:00
|
|
|
|
2011-02-08 12:16:35 -05:00
|
|
|
# Load support files
|
|
|
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
|
|
|
2014-05-09 11:05:58 -04:00
|
|
|
# DatabaseCleaner is apparently necessary for doing proper transactions within MySQL (ugh)
|
2014-10-30 08:25:32 -04:00
|
|
|
DatabaseCleaner.strategy = :truncation
|
2014-05-09 11:05:58 -04:00
|
|
|
|
2011-04-06 20:21:57 -04:00
|
|
|
# global setup block resetting Thread.current
|
|
|
|
class ActiveSupport::TestCase
|
2014-05-09 11:05:58 -04:00
|
|
|
if using_mysql?
|
2016-01-11 22:07:57 -05:00
|
|
|
if respond_to? :use_transactional_tests=
|
|
|
|
self.use_transactional_tests = false
|
|
|
|
else
|
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
end
|
2014-05-09 11:05:58 -04:00
|
|
|
setup { DatabaseCleaner.start }
|
|
|
|
end
|
|
|
|
|
2011-04-06 20:21:57 -04:00
|
|
|
teardown do
|
2014-05-09 11:05:58 -04:00
|
|
|
DatabaseCleaner.clean if using_mysql?
|
2011-04-06 20:21:57 -04:00
|
|
|
Thread.current[:paper_trail] = nil
|
|
|
|
end
|
2015-12-21 08:00:57 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assert_attributes_equal(expected, actual)
|
|
|
|
if using_mysql?
|
|
|
|
expected, actual = expected.dup, actual.dup
|
|
|
|
|
|
|
|
# Adjust timestamps for missing fractional seconds precision.
|
|
|
|
%w(created_at updated_at).each do |timestamp|
|
|
|
|
expected[timestamp] = expected[timestamp].change(usec: 0)
|
|
|
|
actual[timestamp] = actual[timestamp].change(usec: 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal expected, actual
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_changes_equal(expected, actual)
|
|
|
|
if using_mysql?
|
|
|
|
expected, actual = expected.dup, actual.dup
|
|
|
|
|
|
|
|
# Adjust timestamps for missing fractional seconds precision.
|
|
|
|
%w(created_at updated_at).each do |timestamp|
|
|
|
|
expected[timestamp][1] = expected[timestamp][1].change(usec: 0)
|
|
|
|
actual[timestamp][1] = actual[timestamp][1].change(usec: 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal expected, actual
|
|
|
|
end
|
2011-04-06 20:21:57 -04:00
|
|
|
end
|
2011-02-08 12:16:35 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# Helpers
|
|
|
|
#
|
2010-03-18 14:02:55 -04:00
|
|
|
|
2011-02-08 12:16:35 -05:00
|
|
|
def change_schema
|
|
|
|
ActiveRecord::Migration.verbose = false
|
|
|
|
ActiveRecord::Schema.define do
|
|
|
|
remove_column :widgets, :sacrificial_column
|
2012-03-12 07:12:27 -04:00
|
|
|
add_column :versions, :custom_created_at, :datetime
|
2010-03-19 14:53:49 -04:00
|
|
|
end
|
2011-02-08 12:16:35 -05:00
|
|
|
ActiveRecord::Migration.verbose = true
|
2014-03-18 15:56:53 -04:00
|
|
|
reset_version_class_column_info!
|
2010-03-19 14:53:49 -04:00
|
|
|
end
|
2014-03-14 23:52:35 -04:00
|
|
|
|
2016-01-11 22:07:57 -05:00
|
|
|
# Wrap args in a hash to support the ActionController::TestCase and
|
|
|
|
# ActionDispatch::Integration HTTP request method switch to keyword args
|
|
|
|
# (see https://github.com/rails/rails/blob/master/actionpack/CHANGELOG.md)
|
|
|
|
def params_wrapper(args)
|
2016-02-15 18:13:45 -05:00
|
|
|
if defined?(::Rails) && active_record_gem_version >= Gem::Version.new('5.0.0.beta1')
|
2016-01-11 22:07:57 -05:00
|
|
|
{ params: args }
|
|
|
|
else
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_version_class_column_info!
|
|
|
|
PaperTrail::Version.connection.schema_cache.clear!
|
|
|
|
PaperTrail::Version.reset_column_information
|
|
|
|
end
|
|
|
|
|
2014-03-14 23:52:35 -04:00
|
|
|
def restore_schema
|
|
|
|
ActiveRecord::Migration.verbose = false
|
|
|
|
ActiveRecord::Schema.define do
|
|
|
|
add_column :widgets, :sacrificial_column, :string
|
|
|
|
remove_column :versions, :custom_created_at
|
|
|
|
end
|
|
|
|
ActiveRecord::Migration.verbose = true
|
2014-03-18 15:56:53 -04:00
|
|
|
reset_version_class_column_info!
|
2016-02-15 18:13:45 -05:00
|
|
|
end
|