| 1 |
require 'abstract_unit' |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
class ActiveRecordTestConnector |
|---|
| 5 |
cattr_accessor :able_to_connect |
|---|
| 6 |
cattr_accessor :connected |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
self.connected = false |
|---|
| 10 |
self.able_to_connect = true |
|---|
| 11 |
end |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
if defined?(ActiveRecord) && defined?(Fixtures) |
|---|
| 15 |
$stderr.puts 'Active Record is already loaded, running tests' |
|---|
| 16 |
else |
|---|
| 17 |
$stderr.print 'Attempting to load Active Record... ' |
|---|
| 18 |
begin |
|---|
| 19 |
PATH_TO_AR = "#{File.dirname(__FILE__)}/../../activerecord/lib" |
|---|
| 20 |
raise LoadError, "#{PATH_TO_AR} doesn't exist" unless File.directory?(PATH_TO_AR) |
|---|
| 21 |
$LOAD_PATH.unshift PATH_TO_AR |
|---|
| 22 |
require 'active_record' |
|---|
| 23 |
require 'active_record/fixtures' |
|---|
| 24 |
$stderr.puts 'success' |
|---|
| 25 |
rescue LoadError => e |
|---|
| 26 |
$stderr.print "failed. Skipping Active Record assertion tests: #{e}" |
|---|
| 27 |
ActiveRecordTestConnector.able_to_connect = false |
|---|
| 28 |
end |
|---|
| 29 |
end |
|---|
| 30 |
$stderr.flush |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
class ActiveRecordTestConnector |
|---|
| 36 |
class << self |
|---|
| 37 |
def setup |
|---|
| 38 |
unless self.connected || !self.able_to_connect |
|---|
| 39 |
setup_connection |
|---|
| 40 |
load_schema |
|---|
| 41 |
require_fixture_models |
|---|
| 42 |
self.connected = true |
|---|
| 43 |
end |
|---|
| 44 |
rescue Exception => e |
|---|
| 45 |
$stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}" |
|---|
| 46 |
|
|---|
| 47 |
self.able_to_connect = false |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
private |
|---|
| 51 |
|
|---|
| 52 |
def setup_connection |
|---|
| 53 |
if Object.const_defined?(:ActiveRecord) |
|---|
| 54 |
defaults = { :database => ':memory:' } |
|---|
| 55 |
begin |
|---|
| 56 |
options = defaults.merge :adapter => 'sqlite3', :timeout => 500 |
|---|
| 57 |
ActiveRecord::Base.establish_connection(options) |
|---|
| 58 |
ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options } |
|---|
| 59 |
ActiveRecord::Base.connection |
|---|
| 60 |
rescue Exception |
|---|
| 61 |
$stderr.puts 'SQLite 3 unavailable; trying SQLite 2.' |
|---|
| 62 |
options = defaults.merge :adapter => 'sqlite' |
|---|
| 63 |
ActiveRecord::Base.establish_connection(options) |
|---|
| 64 |
ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options } |
|---|
| 65 |
ActiveRecord::Base.connection |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE) |
|---|
| 69 |
else |
|---|
| 70 |
raise "Can't setup connection since ActiveRecord isn't loaded." |
|---|
| 71 |
end |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
def load_schema |
|---|
| 76 |
File.read(File.dirname(__FILE__) + "/fixtures/db_definitions/sqlite.sql").split(';').each do |sql| |
|---|
| 77 |
ActiveRecord::Base.connection.execute(sql) unless sql.blank? |
|---|
| 78 |
end |
|---|
| 79 |
end |
|---|
| 80 |
|
|---|
| 81 |
def require_fixture_models |
|---|
| 82 |
Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f} |
|---|
| 83 |
end |
|---|
| 84 |
end |
|---|
| 85 |
end |
|---|
| 86 |
|
|---|
| 87 |
class ActiveRecordTestCase < ActiveSupport::TestCase |
|---|
| 88 |
|
|---|
| 89 |
if ActiveRecordTestConnector.able_to_connect |
|---|
| 90 |
self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/" |
|---|
| 91 |
self.use_transactional_fixtures = false |
|---|
| 92 |
end |
|---|
| 93 |
|
|---|
| 94 |
def self.fixtures(*args) |
|---|
| 95 |
super if ActiveRecordTestConnector.connected |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
def run(*args) |
|---|
| 99 |
super if ActiveRecordTestConnector.connected |
|---|
| 100 |
end |
|---|
| 101 |
|
|---|
| 102 |
def default_test; end |
|---|
| 103 |
end |
|---|
| 104 |
|
|---|
| 105 |
ActiveRecordTestConnector.setup |
|---|