2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
2013-04-16 18:06:11 -04:00
|
|
|
|
|
|
|
# Define the essentials
|
|
|
|
class ActiveRecordTestConnector
|
|
|
|
cattr_accessor :able_to_connect
|
|
|
|
cattr_accessor :connected
|
|
|
|
|
|
|
|
# Set our defaults
|
|
|
|
self.connected = false
|
|
|
|
self.able_to_connect = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Try to grab AR
|
|
|
|
unless defined?(ActiveRecord) && defined?(FixtureSet)
|
|
|
|
begin
|
2017-05-15 10:17:28 -04:00
|
|
|
PATH_TO_AR = File.expand_path("../../activerecord/lib", __dir__)
|
2013-04-16 18:06:11 -04:00
|
|
|
raise LoadError, "#{PATH_TO_AR} doesn't exist" unless File.directory?(PATH_TO_AR)
|
|
|
|
$LOAD_PATH.unshift PATH_TO_AR
|
2016-08-06 12:50:17 -04:00
|
|
|
require "active_record"
|
2013-04-16 18:06:11 -04:00
|
|
|
rescue LoadError => e
|
|
|
|
$stderr.print "Failed to load Active Record. Skipping Active Record assertion tests: #{e}"
|
|
|
|
ActiveRecordTestConnector.able_to_connect = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
$stderr.flush
|
|
|
|
|
|
|
|
# Define the rest of the connector
|
|
|
|
class ActiveRecordTestConnector
|
|
|
|
class << self
|
|
|
|
def setup
|
2016-08-07 19:05:28 -04:00
|
|
|
unless connected || !able_to_connect
|
2013-04-16 18:06:11 -04:00
|
|
|
setup_connection
|
|
|
|
load_schema
|
|
|
|
require_fixture_models
|
|
|
|
self.connected = true
|
|
|
|
end
|
|
|
|
rescue Exception => e # errors from ActiveRecord setup
|
|
|
|
$stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
|
2017-12-14 03:30:54 -05:00
|
|
|
# $stderr.puts " #{e.backtrace.join("\n ")}\n"
|
2013-04-16 18:06:11 -04:00
|
|
|
self.able_to_connect = false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def setup_connection
|
|
|
|
if Object.const_defined?(:ActiveRecord)
|
2016-08-06 13:36:34 -04:00
|
|
|
defaults = { database: ":memory:" }
|
2016-08-06 12:50:17 -04:00
|
|
|
adapter = defined?(JRUBY_VERSION) ? "jdbcsqlite3" : "sqlite3"
|
2016-08-06 13:36:34 -04:00
|
|
|
options = defaults.merge adapter: adapter, timeout: 500
|
2013-04-16 18:06:11 -04:00
|
|
|
ActiveRecord::Base.establish_connection(options)
|
2016-08-06 12:50:17 -04:00
|
|
|
ActiveRecord::Base.configurations = { "sqlite3_ar_integration" => options }
|
2013-04-16 18:06:11 -04:00
|
|
|
ActiveRecord::Base.connection
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name("type")) unless Object.const_defined?(:QUOTED_TYPE)
|
2013-04-16 18:06:11 -04:00
|
|
|
else
|
|
|
|
raise "Can't setup connection since ActiveRecord isn't loaded."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-06 14:47:16 -04:00
|
|
|
# Load actionpack sqlite3 tables
|
2013-04-16 18:06:11 -04:00
|
|
|
def load_schema
|
2017-05-15 10:17:28 -04:00
|
|
|
File.read(File.expand_path("fixtures/db_definitions/sqlite.sql", __dir__)).split(";").each do |sql|
|
2013-04-16 18:06:11 -04:00
|
|
|
ActiveRecord::Base.connection.execute(sql) unless sql.blank?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_fixture_models
|
2017-05-15 10:17:28 -04:00
|
|
|
Dir.glob(File.expand_path("fixtures/*.rb", __dir__)).each { |f| require f }
|
2013-04-16 18:06:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ActiveRecordTestCase < ActionController::TestCase
|
|
|
|
include ActiveRecord::TestFixtures
|
|
|
|
|
|
|
|
# Set our fixture path
|
|
|
|
if ActiveRecordTestConnector.able_to_connect
|
|
|
|
self.fixture_path = [FIXTURE_LOAD_PATH]
|
2015-03-10 22:21:19 -04:00
|
|
|
self.use_transactional_tests = false
|
2013-04-16 18:06:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.fixtures(*args)
|
|
|
|
super if ActiveRecordTestConnector.connected
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(*args)
|
|
|
|
super if ActiveRecordTestConnector.connected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveRecordTestConnector.setup
|