9e0c66055d98238b62c9a38029b9e197c5256407
[feedcatcher.git] / vendor / rails / actionpack / test / active_record_unit.rb
1 require 'abstract_unit'
2
3 # Define the essentials
4 class ActiveRecordTestConnector
5 cattr_accessor :able_to_connect
6 cattr_accessor :connected
7
8 # Set our defaults
9 self.connected = false
10 self.able_to_connect = true
11 end
12
13 # Try to grab AR
14 if defined?(ActiveRecord) && defined?(Fixtures)
15 $stderr.puts 'Active Record is already loaded, running tests'
16 else
17 $stderr.print 'Attempting to load Active Record... '
18 begin
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'
25 rescue LoadError => e
26 $stderr.print "failed. Skipping Active Record assertion tests: #{e}"
27 ActiveRecordTestConnector.able_to_connect = false
28 end
29 end
30 $stderr.flush
31
32
33 # Define the rest of the connector
34 class ActiveRecordTestConnector
35 class << self
36 def setup
37 unless self.connected || !self.able_to_connect
38 setup_connection
39 load_schema
40 require_fixture_models
41 self.connected = true
42 end
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
47 end
48
49 private
50 def setup_connection
51 if Object.const_defined?(:ActiveRecord)
52 defaults = { :database => ':memory:' }
53 begin
54 adapter = defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3'
55 options = defaults.merge :adapter => adapter, :timeout => 500
56 ActiveRecord::Base.establish_connection(options)
57 ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
58 ActiveRecord::Base.connection
59 rescue Exception # errors from establishing a connection
60 $stderr.puts 'SQLite 3 unavailable; trying SQLite 2.'
61 options = defaults.merge :adapter => 'sqlite'
62 ActiveRecord::Base.establish_connection(options)
63 ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options }
64 ActiveRecord::Base.connection
65 end
66
67 Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
68 else
69 raise "Can't setup connection since ActiveRecord isn't loaded."
70 end
71 end
72
73 # Load actionpack sqlite tables
74 def load_schema
75 File.read(File.dirname(__FILE__) + "/fixtures/db_definitions/sqlite.sql").split(';').each do |sql|
76 ActiveRecord::Base.connection.execute(sql) unless sql.blank?
77 end
78 end
79
80 def require_fixture_models
81 Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f}
82 end
83 end
84 end
85
86 class ActiveRecordTestCase < ActionController::TestCase
87 include ActiveRecord::TestFixtures
88
89 # Set our fixture path
90 if ActiveRecordTestConnector.able_to_connect
91 self.fixture_path = [FIXTURE_LOAD_PATH]
92 self.use_transactional_fixtures = false
93 end
94
95 def self.fixtures(*args)
96 super if ActiveRecordTestConnector.connected
97 end
98
99 def run(*args)
100 super if ActiveRecordTestConnector.connected
101 end
102 end
103
104 ActiveRecordTestConnector.setup