1 require 'abstract_unit'
3 # Define the essentials
4 class ActiveRecordTestConnector
5 cattr_accessor
:able_to_connect
6 cattr_accessor
:connected
10 self.able_to_connect
= true
14 if defined?(ActiveRecord
) && defined?(Fixtures
)
15 $stderr.puts
'Active Record is already loaded, running tests'
17 $stderr.print
'Attempting to load Active Record... '
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'
26 $stderr.print
"failed. Skipping Active Record assertion tests: #{e}"
27 ActiveRecordTestConnector
.able_to_connect
= false
33 # Define the rest of the connector
34 class ActiveRecordTestConnector
37 unless self.connected
|| !self.able_to_connect
40 require_fixture_models
43 rescue Exception
=> e
# errors from ActiveRecord setup
44 $stderr.puts
"\nSkipping ActiveRecord assertion tests: #{e}"
45 #$stderr.puts " #{e.backtrace.join("\n ")}\n"
46 self.able_to_connect
= false
51 if Object
.const_defined
?(:ActiveRecord)
52 defaults
= { :database => ':memory:' }
54 options
= defaults
.merge
:adapter => 'sqlite3', :timeout => 500
55 ActiveRecord
::Base.establish_connection(options
)
56 ActiveRecord
::Base.configurations
= { 'sqlite3_ar_integration' => options
}
57 ActiveRecord
::Base.connection
58 rescue Exception
# errors from establishing a connection
59 $stderr.puts
'SQLite 3 unavailable; trying SQLite 2.'
60 options
= defaults
.merge
:adapter => 'sqlite'
61 ActiveRecord
::Base.establish_connection(options
)
62 ActiveRecord
::Base.configurations
= { 'sqlite2_ar_integration' => options
}
63 ActiveRecord
::Base.connection
66 Object
.send(:const_set, :QUOTED_TYPE, ActiveRecord
::Base.connection
.quote_column_name('type')) unless Object
.const_defined
?(:QUOTED_TYPE)
68 raise "Can't setup connection since ActiveRecord isn't loaded."
72 # Load actionpack sqlite tables
74 File
.read(File
.dirname(__FILE__
) + "/fixtures/db_definitions/sqlite.sql").split(';').each
do |sql
|
75 ActiveRecord
::Base.connection
.execute(sql
) unless sql
.blank
?
79 def require_fixture_models
80 Dir
.glob(File
.dirname(__FILE__
) + "/fixtures/*.rb").each
{|f
| require f
}
85 class ActiveRecordTestCase
< ActiveSupport
::TestCase
86 # Set our fixture path
87 if ActiveRecordTestConnector
.able_to_connect
88 self.fixture_path
= [FIXTURE_LOAD_PATH
]
89 self.use_transactional_fixtures
= false
92 def self.fixtures(*args
)
93 super if ActiveRecordTestConnector
.connected
97 super if ActiveRecordTestConnector
.connected
100 def default_test
; end
103 ActiveRecordTestConnector
.setup