3 unless defined?(ActiveRecord)
4 plugin_root = File.join(File.dirname(__FILE__), '..')
6 # first look for a symlink to a copy of the framework
7 if framework_root = ["#{plugin_root}/rails", "#{plugin_root}/../../rails"].find { |p| File.directory? p }
8 puts "found framework root: #{framework_root}"
9 # this allows for a plugin to be tested outside an app
10 $:.unshift "#{framework_root}/activesupport/lib", "#{framework_root}/activerecord/lib", "#{framework_root}/actionpack/lib"
12 # is the plugin installed in an application?
13 app_root = plugin_root + '/../../..'
15 if File.directory? app_root + '/config'
16 puts 'using config/boot.rb'
17 ENV['RAILS_ENV'] = 'test'
18 require File.expand_path(app_root + '/config/boot')
20 # simply use installed gems if available
23 gem 'actionpack'; gem 'activerecord'
27 %w(action_pack active_record action_controller active_record/fixtures action_controller/test_process).each {|f| require f}
29 Dependencies.load_paths.unshift "#{plugin_root}/lib"
32 # Define the connector
33 class ActiveRecordTestConnector
34 cattr_accessor :able_to_connect
35 cattr_accessor :connected
38 self.connected = false
39 self.able_to_connect = true
43 unless self.connected || !self.able_to_connect
46 require_fixture_models
49 rescue Exception => e # errors from ActiveRecord setup
50 $stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
51 #$stderr.puts " #{e.backtrace.join("\n ")}\n"
52 self.able_to_connect = false
58 if Object.const_defined?(:ActiveRecord)
59 defaults = { :database => ':memory:' }
61 options = defaults.merge :adapter => 'sqlite3', :timeout => 500
62 ActiveRecord::Base.establish_connection(options)
63 ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
64 ActiveRecord::Base.connection
65 rescue Exception # errors from establishing a connection
66 $stderr.puts 'SQLite 3 unavailable; trying SQLite 2.'
67 options = defaults.merge :adapter => 'sqlite'
68 ActiveRecord::Base.establish_connection(options)
69 ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options }
70 ActiveRecord::Base.connection
73 Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
75 raise "Can't setup connection since ActiveRecord isn't loaded."
79 # Load actionpack sqlite tables
81 File.read(File.dirname(__FILE__) + "/fixtures/schema.sql").split(';').each do |sql|
82 ActiveRecord::Base.connection.execute(sql) unless sql.blank?
86 def require_fixture_models
87 Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f}
92 # Test case for inheritance
93 class ActiveRecordTestCase < Test::Unit::TestCase
94 # Set our fixture path
95 if ActiveRecordTestConnector.able_to_connect
96 self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/"
97 self.use_transactional_fixtures = false
100 def self.fixtures(*args)
101 super if ActiveRecordTestConnector.connected
105 super if ActiveRecordTestConnector.connected
108 # Default so Test::Unit::TestCase doesn't complain
113 ActiveRecordTestConnector.setup
114 ActionController::Routing::Routes.reload rescue nil
115 ActionController::Routing::Routes.draw do |map|
116 map.connect ':controller/:action/:id'