2016-02-16 00:53:13 +00:00
|
|
|
require 'pry-nav'
|
2015-09-13 17:39:58 +00:00
|
|
|
|
2011-02-08 17:16:35 +00:00
|
|
|
ENV["RAILS_ENV"] = "test"
|
2014-03-14 20:11:10 +00:00
|
|
|
ENV["DB"] ||= "sqlite"
|
2014-03-20 20:17:11 +00:00
|
|
|
|
2016-02-16 00:30:17 +00:00
|
|
|
unless File.exist?(File.expand_path('../../test/dummy/config/database.yml', __FILE__))
|
2014-03-20 20:17:11 +00:00
|
|
|
warn "WARNING: No database.yml detected for the dummy app, please run `rake prepare` first"
|
|
|
|
end
|
|
|
|
|
2014-05-09 15:05:58 +00:00
|
|
|
def using_mysql?
|
|
|
|
@using_mysql ||= ActiveRecord::Base.connection_config[:adapter].to_sym == :mysql2
|
|
|
|
end
|
|
|
|
|
2011-02-08 17:16:35 +00:00
|
|
|
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
|
|
|
require "rails/test_help"
|
|
|
|
require 'shoulda'
|
2013-01-22 23:13:58 +00:00
|
|
|
require 'ffaker'
|
2014-10-30 12:25:32 +00:00
|
|
|
require 'database_cleaner'
|
2009-05-27 15:21:20 +00:00
|
|
|
|
2016-02-15 23:13:45 +00: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-12 03:07:57 +00:00
|
|
|
# See https://github.com/rails/rails-controller-testing/issues/5
|
|
|
|
ActionController::TestCase.send(:include, Rails::Controller::Testing::TestProcess)
|
|
|
|
end
|
|
|
|
|
2013-10-03 14:06:24 +00:00
|
|
|
Rails.backtrace_cleaner.remove_silencers!
|
|
|
|
|
2011-02-08 17:16:35 +00:00
|
|
|
# Run any available migration
|
|
|
|
ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
|
2010-03-18 18:02:55 +00:00
|
|
|
|
2011-02-08 17:16:35 +00:00
|
|
|
# Load support files
|
|
|
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
|
|
|
2014-05-09 15:05:58 +00:00
|
|
|
# DatabaseCleaner is apparently necessary for doing proper transactions within MySQL (ugh)
|
2014-10-30 12:25:32 +00:00
|
|
|
DatabaseCleaner.strategy = :truncation
|
2014-05-09 15:05:58 +00:00
|
|
|
|
2011-04-07 00:21:57 +00:00
|
|
|
# global setup block resetting Thread.current
|
2016-02-16 03:07:48 +00:00
|
|
|
module ActiveSupport
|
|
|
|
class TestCase
|
|
|
|
if using_mysql?
|
|
|
|
if respond_to? :use_transactional_tests=
|
|
|
|
self.use_transactional_tests = false
|
|
|
|
else
|
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
end
|
|
|
|
setup { DatabaseCleaner.start }
|
2016-01-12 03:07:57 +00:00
|
|
|
end
|
2014-05-09 15:05:58 +00:00
|
|
|
|
2016-02-16 03:07:48 +00:00
|
|
|
teardown do
|
|
|
|
DatabaseCleaner.clean if using_mysql?
|
|
|
|
Thread.current[:paper_trail] = nil
|
|
|
|
end
|
2015-12-21 13:00:57 +00:00
|
|
|
|
2016-02-16 03:07:48 +00:00
|
|
|
private
|
2015-12-21 13:00:57 +00:00
|
|
|
|
2016-02-16 03:07:48 +00:00
|
|
|
def assert_attributes_equal(expected, actual)
|
|
|
|
if using_mysql?
|
|
|
|
expected, actual = expected.dup, actual.dup
|
2015-12-21 13:00:57 +00:00
|
|
|
|
2016-02-16 03:07:48 +00:00
|
|
|
# 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
|
2015-12-21 13:00:57 +00:00
|
|
|
end
|
|
|
|
|
2016-02-16 03:07:48 +00:00
|
|
|
assert_equal expected, actual
|
|
|
|
end
|
2015-12-21 13:00:57 +00:00
|
|
|
|
2016-02-16 03:07:48 +00:00
|
|
|
def assert_changes_equal(expected, actual)
|
|
|
|
if using_mysql?
|
|
|
|
expected, actual = expected.dup, actual.dup
|
2015-12-21 13:00:57 +00:00
|
|
|
|
2016-02-16 03:07:48 +00:00
|
|
|
# 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
|
2015-12-21 13:00:57 +00:00
|
|
|
end
|
|
|
|
|
2016-02-16 03:07:48 +00:00
|
|
|
assert_equal expected, actual
|
|
|
|
end
|
2015-12-21 13:00:57 +00:00
|
|
|
end
|
2011-04-07 00:21:57 +00:00
|
|
|
end
|
2011-02-08 17:16:35 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Helpers
|
|
|
|
#
|
2010-03-18 18:02:55 +00:00
|
|
|
|
2011-02-08 17:16:35 +00:00
|
|
|
def change_schema
|
|
|
|
ActiveRecord::Migration.verbose = false
|
|
|
|
ActiveRecord::Schema.define do
|
|
|
|
remove_column :widgets, :sacrificial_column
|
2012-03-12 11:12:27 +00:00
|
|
|
add_column :versions, :custom_created_at, :datetime
|
2010-03-19 18:53:49 +00:00
|
|
|
end
|
2011-02-08 17:16:35 +00:00
|
|
|
ActiveRecord::Migration.verbose = true
|
2014-03-18 19:56:53 +00:00
|
|
|
reset_version_class_column_info!
|
2010-03-19 18:53:49 +00:00
|
|
|
end
|
2014-03-15 03:52:35 +00:00
|
|
|
|
2016-01-12 03:07:57 +00: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 23:13:45 +00:00
|
|
|
if defined?(::Rails) && active_record_gem_version >= Gem::Version.new('5.0.0.beta1')
|
2016-01-12 03:07:57 +00: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-15 03:52:35 +00: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 19:56:53 +00:00
|
|
|
reset_version_class_column_info!
|
2016-02-15 23:13:45 +00:00
|
|
|
end
|