annotate .svn/pristine/cf/cf164880642b001e32b75655aabc9764c7865713.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 require 'test/unit'
Chris@909 2
Chris@909 3 unless defined?(ActiveRecord)
Chris@909 4 plugin_root = File.join(File.dirname(__FILE__), '..')
Chris@909 5
Chris@909 6 # first look for a symlink to a copy of the framework
Chris@909 7 if framework_root = ["#{plugin_root}/rails", "#{plugin_root}/../../rails"].find { |p| File.directory? p }
Chris@909 8 puts "found framework root: #{framework_root}"
Chris@909 9 # this allows for a plugin to be tested outside an app
Chris@909 10 $:.unshift "#{framework_root}/activesupport/lib", "#{framework_root}/activerecord/lib", "#{framework_root}/actionpack/lib"
Chris@909 11 else
Chris@909 12 # is the plugin installed in an application?
Chris@909 13 app_root = plugin_root + '/../../..'
Chris@909 14
Chris@909 15 if File.directory? app_root + '/config'
Chris@909 16 puts 'using config/boot.rb'
Chris@909 17 ENV['RAILS_ENV'] = 'test'
Chris@909 18 require File.expand_path(app_root + '/config/boot')
Chris@909 19 else
Chris@909 20 # simply use installed gems if available
Chris@909 21 puts 'using rubygems'
Chris@909 22 require 'rubygems'
Chris@909 23 gem 'actionpack'; gem 'activerecord'
Chris@909 24 end
Chris@909 25 end
Chris@909 26
Chris@909 27 %w(action_pack active_record action_controller active_record/fixtures action_controller/test_process).each {|f| require f}
Chris@909 28
Chris@909 29 Dependencies.load_paths.unshift "#{plugin_root}/lib"
Chris@909 30 end
Chris@909 31
Chris@909 32 # Define the connector
Chris@909 33 class ActiveRecordTestConnector
Chris@909 34 cattr_accessor :able_to_connect
Chris@909 35 cattr_accessor :connected
Chris@909 36
Chris@909 37 # Set our defaults
Chris@909 38 self.connected = false
Chris@909 39 self.able_to_connect = true
Chris@909 40
Chris@909 41 class << self
Chris@909 42 def setup
Chris@909 43 unless self.connected || !self.able_to_connect
Chris@909 44 setup_connection
Chris@909 45 load_schema
Chris@909 46 require_fixture_models
Chris@909 47 self.connected = true
Chris@909 48 end
Chris@909 49 rescue Exception => e # errors from ActiveRecord setup
Chris@909 50 $stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
Chris@909 51 #$stderr.puts " #{e.backtrace.join("\n ")}\n"
Chris@909 52 self.able_to_connect = false
Chris@909 53 end
Chris@909 54
Chris@909 55 private
Chris@909 56
Chris@909 57 def setup_connection
Chris@909 58 if Object.const_defined?(:ActiveRecord)
Chris@909 59 defaults = { :database => ':memory:' }
Chris@909 60 begin
Chris@909 61 options = defaults.merge :adapter => 'sqlite3', :timeout => 500
Chris@909 62 ActiveRecord::Base.establish_connection(options)
Chris@909 63 ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
Chris@909 64 ActiveRecord::Base.connection
Chris@909 65 rescue Exception # errors from establishing a connection
Chris@909 66 $stderr.puts 'SQLite 3 unavailable; trying SQLite 2.'
Chris@909 67 options = defaults.merge :adapter => 'sqlite'
Chris@909 68 ActiveRecord::Base.establish_connection(options)
Chris@909 69 ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options }
Chris@909 70 ActiveRecord::Base.connection
Chris@909 71 end
Chris@909 72
Chris@909 73 Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
Chris@909 74 else
Chris@909 75 raise "Can't setup connection since ActiveRecord isn't loaded."
Chris@909 76 end
Chris@909 77 end
Chris@909 78
Chris@909 79 # Load actionpack sqlite tables
Chris@909 80 def load_schema
Chris@909 81 File.read(File.dirname(__FILE__) + "/fixtures/schema.sql").split(';').each do |sql|
Chris@909 82 ActiveRecord::Base.connection.execute(sql) unless sql.blank?
Chris@909 83 end
Chris@909 84 end
Chris@909 85
Chris@909 86 def require_fixture_models
Chris@909 87 Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f}
Chris@909 88 end
Chris@909 89 end
Chris@909 90 end
Chris@909 91
Chris@909 92 # Test case for inheritance
Chris@909 93 class ActiveRecordTestCase < Test::Unit::TestCase
Chris@909 94 # Set our fixture path
Chris@909 95 if ActiveRecordTestConnector.able_to_connect
Chris@909 96 self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/"
Chris@909 97 self.use_transactional_fixtures = false
Chris@909 98 end
Chris@909 99
Chris@909 100 def self.fixtures(*args)
Chris@909 101 super if ActiveRecordTestConnector.connected
Chris@909 102 end
Chris@909 103
Chris@909 104 def run(*args)
Chris@909 105 super if ActiveRecordTestConnector.connected
Chris@909 106 end
Chris@909 107
Chris@909 108 # Default so Test::Unit::TestCase doesn't complain
Chris@909 109 def test_truth
Chris@909 110 end
Chris@909 111 end
Chris@909 112
Chris@909 113 ActiveRecordTestConnector.setup
Chris@909 114 ActionController::Routing::Routes.reload rescue nil
Chris@909 115 ActionController::Routing::Routes.draw do |map|
Chris@909 116 map.connect ':controller/:action/:id'
Chris@909 117 end