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
|
|
|
|
PATH_TO_AR = "#{File.dirname(__FILE__)}/../../activerecord/lib"
|
|
|
|
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}"
|
|
|
|
#$stderr.puts " #{e.backtrace.join("\n ")}\n"
|
|
|
|
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
|
2016-08-06 12:50:17 -04:00
|
|
|
File.read(File.dirname(__FILE__) + "/fixtures/db_definitions/sqlite.sql").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
|
|
|
|
Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f}
|
|
|
|
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
|