annotate vendor/plugins/classic_pagination/test/.svn/text-base/helper.rb.svn-base @ 8:0c83d98252d9 yuya

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