Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/2-1-caching/actionpack/test/active_record_unit.rb

Revision 6029, 3.3 kB (checked in by bitsweat, 2 years ago)

Pass busy timeout for sqlite3 integration tests.

Line 
1 require  File.dirname(__FILE__) + '/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
34 # Define the rest of the connector
35 class ActiveRecordTestConnector
36   class << self
37     def setup
38       unless self.connected || !self.able_to_connect
39         setup_connection
40         load_schema
41         require_fixture_models
42         self.connected = true
43       end
44     rescue Exception => e  # errors from ActiveRecord setup
45       $stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
46       #$stderr.puts "  #{e.backtrace.join("\n  ")}\n"
47       self.able_to_connect = false
48     end
49
50     private
51
52     def setup_connection
53       if Object.const_defined?(:ActiveRecord)
54         defaults = { :database => ':memory:' }
55         begin
56           options = defaults.merge :adapter => 'sqlite3', :timeout => 500
57           ActiveRecord::Base.establish_connection(options)
58           ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
59           ActiveRecord::Base.connection
60         rescue Exception  # errors from establishing a connection
61           $stderr.puts 'SQLite 3 unavailable; trying SQLite 2.'
62           options = defaults.merge :adapter => 'sqlite'
63           ActiveRecord::Base.establish_connection(options)
64           ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options }
65           ActiveRecord::Base.connection
66         end
67
68         Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
69       else
70         raise "Can't setup connection since ActiveRecord isn't loaded."
71       end
72     end
73
74     # Load actionpack sqlite tables
75     def load_schema
76       File.read(File.dirname(__FILE__) + "/fixtures/db_definitions/sqlite.sql").split(';').each do |sql|
77         ActiveRecord::Base.connection.execute(sql) unless sql.blank?
78       end
79     end
80
81     def require_fixture_models
82       Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f}
83     end
84   end
85 end
86
87 # Test case for inheritance
88 class ActiveRecordTestCase < Test::Unit::TestCase
89   # Set our fixture path
90   if ActiveRecordTestConnector.able_to_connect
91     self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/"
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
103   # Default so Test::Unit::TestCase doesn't complain
104   def test_truth
105   end
106 end
107
108 ActiveRecordTestConnector.setup
Note: See TracBrowser for help on using the browser.