]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/classic_pagination/test/helper.rb
3f76d5a76606bdec2642c482069b19911a9c1149
[rails.git] / vendor / plugins / classic_pagination / test / helper.rb
1 require 'test/unit'
2
3 unless defined?(ActiveRecord)
4   plugin_root = File.join(File.dirname(__FILE__), '..')
5
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"
11   else
12     # is the plugin installed in an application?
13     app_root = plugin_root + '/../../..'
14
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')
19     else
20       # simply use installed gems if available
21       puts 'using rubygems'
22       require 'rubygems'
23       gem 'actionpack'; gem 'activerecord'
24     end
25   end
26
27   %w(action_pack active_record action_controller active_record/fixtures action_controller/test_process).each {|f| require f}
28
29   Dependencies.load_paths.unshift "#{plugin_root}/lib"
30 end
31
32 # Define the connector
33 class ActiveRecordTestConnector
34   cattr_accessor :able_to_connect
35   cattr_accessor :connected
36
37   # Set our defaults
38   self.connected = false
39   self.able_to_connect = true
40
41   class << self
42     def setup
43       unless self.connected || !self.able_to_connect
44         setup_connection
45         load_schema
46         require_fixture_models
47         self.connected = true
48       end
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
53     end
54
55     private
56
57     def setup_connection
58       if Object.const_defined?(:ActiveRecord)
59         defaults = { :database => ':memory:' }
60         begin
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
71         end
72
73         Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
74       else
75         raise "Can't setup connection since ActiveRecord isn't loaded."
76       end
77     end
78
79     # Load actionpack sqlite tables
80     def load_schema
81       File.read(File.dirname(__FILE__) + "/fixtures/schema.sql").split(';').each do |sql|
82         ActiveRecord::Base.connection.execute(sql) unless sql.blank?
83       end
84     end
85
86     def require_fixture_models
87       Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f}
88     end
89   end
90 end
91
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
98   end
99
100   def self.fixtures(*args)
101     super if ActiveRecordTestConnector.connected
102   end
103
104   def run(*args)
105     super if ActiveRecordTestConnector.connected
106   end
107
108   # Default so Test::Unit::TestCase doesn't complain
109   def test_truth
110   end
111 end
112
113 ActiveRecordTestConnector.setup
114 ActionController::Routing::Routes.reload rescue nil
115 ActionController::Routing::Routes.draw do |map|
116   map.connect ':controller/:action/:id'
117 end