2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2006-02-09 04:17:40 -05:00
|
|
|
|
|
|
|
# Define the essentials
|
|
|
|
class ActiveRecordTestConnector
|
|
|
|
cattr_accessor :able_to_connect
|
|
|
|
cattr_accessor :connected
|
2006-08-22 00:38:03 -04:00
|
|
|
|
2006-02-09 04:17:40 -05:00
|
|
|
# Set our defaults
|
|
|
|
self.connected = false
|
|
|
|
self.able_to_connect = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Try to grab AR
|
2006-08-22 00:38:03 -04:00
|
|
|
if defined?(ActiveRecord) && defined?(Fixtures)
|
|
|
|
$stderr.puts 'Active Record is already loaded, running tests'
|
|
|
|
else
|
|
|
|
$stderr.print 'Attempting to load Active Record... '
|
|
|
|
begin
|
2009-05-22 19:21:21 -04:00
|
|
|
PATH_TO_AR = "#{File.dirname(__FILE__)}/../../../activerecord/lib"
|
2006-08-30 23:16:28 -04:00
|
|
|
raise LoadError, "#{PATH_TO_AR} doesn't exist" unless File.directory?(PATH_TO_AR)
|
2006-08-22 00:38:03 -04:00
|
|
|
$LOAD_PATH.unshift PATH_TO_AR
|
|
|
|
require 'active_record'
|
|
|
|
require 'active_record/fixtures'
|
|
|
|
$stderr.puts 'success'
|
2006-08-30 23:16:28 -04:00
|
|
|
rescue LoadError => e
|
2006-08-22 00:38:03 -04:00
|
|
|
$stderr.print "failed. Skipping Active Record assertion tests: #{e}"
|
|
|
|
ActiveRecordTestConnector.able_to_connect = false
|
|
|
|
end
|
2006-02-09 04:17:40 -05:00
|
|
|
end
|
2006-08-22 00:38:03 -04:00
|
|
|
$stderr.flush
|
|
|
|
|
|
|
|
|
2006-02-09 04:17:40 -05:00
|
|
|
# Define the rest of the connector
|
2006-08-22 00:38:03 -04:00
|
|
|
class ActiveRecordTestConnector
|
2006-08-22 05:19:55 -04:00
|
|
|
class << self
|
|
|
|
def setup
|
|
|
|
unless self.connected || !self.able_to_connect
|
|
|
|
setup_connection
|
|
|
|
load_schema
|
|
|
|
require_fixture_models
|
|
|
|
self.connected = true
|
|
|
|
end
|
2006-08-30 23:16:28 -04:00
|
|
|
rescue Exception => e # errors from ActiveRecord setup
|
2006-08-22 05:19:55 -04:00
|
|
|
$stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
|
|
|
|
#$stderr.puts " #{e.backtrace.join("\n ")}\n"
|
|
|
|
self.able_to_connect = false
|
2006-02-09 04:17:40 -05:00
|
|
|
end
|
2006-08-22 00:38:03 -04:00
|
|
|
|
2006-08-22 05:19:55 -04:00
|
|
|
private
|
2008-06-25 16:24:12 -04:00
|
|
|
def setup_connection
|
|
|
|
if Object.const_defined?(:ActiveRecord)
|
|
|
|
defaults = { :database => ':memory:' }
|
|
|
|
begin
|
2009-02-06 16:27:50 -05:00
|
|
|
adapter = defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3'
|
|
|
|
options = defaults.merge :adapter => adapter, :timeout => 500
|
2008-06-25 16:24:12 -04:00
|
|
|
ActiveRecord::Base.establish_connection(options)
|
|
|
|
ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
|
|
|
|
ActiveRecord::Base.connection
|
|
|
|
rescue Exception # errors from establishing a connection
|
|
|
|
$stderr.puts 'SQLite 3 unavailable; trying SQLite 2.'
|
|
|
|
options = defaults.merge :adapter => 'sqlite'
|
|
|
|
ActiveRecord::Base.establish_connection(options)
|
|
|
|
ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options }
|
|
|
|
ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
|
|
|
Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
|
|
|
|
else
|
|
|
|
raise "Can't setup connection since ActiveRecord isn't loaded."
|
2006-08-22 05:19:55 -04:00
|
|
|
end
|
2006-02-09 04:17:40 -05:00
|
|
|
end
|
2006-08-22 00:38:03 -04:00
|
|
|
|
2008-06-25 16:24:12 -04:00
|
|
|
# Load actionpack sqlite tables
|
|
|
|
def load_schema
|
2009-05-22 19:21:21 -04:00
|
|
|
File.read(File.dirname(__FILE__) + "/../fixtures/db_definitions/sqlite.sql").split(';').each do |sql|
|
2008-06-25 16:24:12 -04:00
|
|
|
ActiveRecord::Base.connection.execute(sql) unless sql.blank?
|
|
|
|
end
|
2006-08-22 05:19:55 -04:00
|
|
|
end
|
2006-08-22 00:38:03 -04:00
|
|
|
|
2008-06-25 16:24:12 -04:00
|
|
|
def require_fixture_models
|
2009-05-22 19:21:21 -04:00
|
|
|
Dir.glob(File.dirname(__FILE__) + "/../fixtures/*.rb").each {|f| require f}
|
2008-06-25 16:24:12 -04:00
|
|
|
end
|
2006-02-09 04:17:40 -05:00
|
|
|
end
|
|
|
|
end
|
2006-08-22 00:38:03 -04:00
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class ActiveRecordTestCase < ActionController::TestCase
|
2008-11-07 15:51:50 -05:00
|
|
|
include ActiveRecord::TestFixtures
|
|
|
|
|
2006-02-09 04:17:40 -05:00
|
|
|
# Set our fixture path
|
2006-08-22 00:38:03 -04:00
|
|
|
if ActiveRecordTestConnector.able_to_connect
|
2008-06-25 16:24:12 -04:00
|
|
|
self.fixture_path = [FIXTURE_LOAD_PATH]
|
2006-08-22 00:38:03 -04:00
|
|
|
self.use_transactional_fixtures = false
|
|
|
|
end
|
|
|
|
|
2006-08-22 05:19:55 -04:00
|
|
|
def self.fixtures(*args)
|
|
|
|
super if ActiveRecordTestConnector.connected
|
|
|
|
end
|
|
|
|
|
2006-11-15 07:43:34 -05:00
|
|
|
def run(*args)
|
|
|
|
super if ActiveRecordTestConnector.connected
|
2006-02-09 04:17:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-22 00:38:03 -04:00
|
|
|
ActiveRecordTestConnector.setup
|