1 $:.unshift(ENV['AR_LOAD_PATH']) if ENV['AR_LOAD_PATH']
6 require 'active_record'
7 require 'active_record/fixtures'
10 rescue MissingSourceFile => e
11 adapter = 'postgresql' #'sqlite'
12 require "#{File.dirname(__FILE__)}/connections/native_#{adapter}/connection"
14 require 'composite_primary_keys'
16 QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type') unless Object.const_defined?(:QUOTED_TYPE)
18 class Test::Unit::TestCase #:nodoc:
19 self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
20 self.use_instantiated_fixtures = false
21 self.use_transactional_fixtures = true
23 def assert_date_from_db(expected, actual, message = nil)
24 # SQL Server doesn't have a separate column type just for dates,
25 # so the time is in the string and incorrectly formatted
26 if current_adapter?(:SQLServerAdapter)
27 assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00")
28 elsif current_adapter?(:SybaseAdapter)
29 assert_equal expected.to_s, actual.to_date.to_s, message
31 assert_equal expected.to_s, actual.to_s, message
35 def assert_queries(num = 1)
36 ActiveRecord::Base.connection.class.class_eval do
38 alias_method :execute, :execute_with_query_counting
42 ActiveRecord::Base.connection.class.class_eval do
43 alias_method :execute, :execute_without_query_counting
45 assert_equal num, ActiveRecord::Base.connection.query_count, "#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed."
48 def assert_no_queries(&block)
49 assert_queries(0, &block)
52 cattr_accessor :classes
55 def testing_with(&block)
56 classes.keys.each do |@key_test|
57 @klass_info = classes[@key_test]
58 @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
59 order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
60 @first = @klass.find(:first, :order => order)
66 ids = (1..@primary_keys.length).map {|num| 1}
67 composite? ? ids.to_composite_ids : ids.first
71 composite? ? first_id.join(CompositePrimaryKeys::ID_SEP) : first_id.to_s
79 def current_adapter?(type)
80 ActiveRecord::ConnectionAdapters.const_defined?(type) &&
81 ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
84 ActiveRecord::Base.connection.class.class_eval do
85 cattr_accessor :query_count
86 alias_method :execute_without_query_counting, :execute
87 def execute_with_query_counting(sql, name = nil)
89 execute_without_query_counting(sql, name)
93 #ActiveRecord::Base.logger = Logger.new(STDOUT)
94 #ActiveRecord::Base.colorize_logging = false