2004-12-13 18:47:27 -05:00
|
|
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2005-02-17 14:28:13 -05:00
|
|
|
$:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib')
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
require 'active_record'
|
|
|
|
require 'active_record/fixtures'
|
|
|
|
require 'connection'
|
|
|
|
|
2006-09-03 23:38:13 -04:00
|
|
|
# Show backtraces for deprecated behavior for quicker cleanup.
|
|
|
|
ActiveSupport::Deprecation.debug = true
|
|
|
|
|
|
|
|
|
2005-11-16 03:16:54 -05:00
|
|
|
QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type') unless Object.const_defined?(:QUOTED_TYPE)
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
class Test::Unit::TestCase #:nodoc:
|
2005-09-29 21:00:04 -04:00
|
|
|
self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
|
|
|
self.use_instantiated_fixtures = false
|
|
|
|
self.use_transactional_fixtures = (ENV['AR_NO_TX_FIXTURES'] != "yes")
|
|
|
|
|
|
|
|
def create_fixtures(*table_names, &block)
|
2006-02-27 15:29:28 -05:00
|
|
|
Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, {}, &block)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-07-07 06:48:43 -04:00
|
|
|
|
2006-01-21 18:20:00 -05:00
|
|
|
def assert_date_from_db(expected, actual, message = nil)
|
2006-07-07 06:48:43 -04:00
|
|
|
# SQL Server doesn't have a separate column type just for dates,
|
2006-01-21 18:20:00 -05:00
|
|
|
# so the time is in the string and incorrectly formatted
|
2006-03-17 22:02:32 -05:00
|
|
|
if current_adapter?(:SQLServerAdapter)
|
2006-01-21 18:20:00 -05:00
|
|
|
assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00")
|
2006-03-17 22:02:32 -05:00
|
|
|
elsif current_adapter?(:SybaseAdapter)
|
|
|
|
assert_equal expected.to_s, actual.to_date.to_s, message
|
2006-01-21 18:20:00 -05:00
|
|
|
else
|
|
|
|
assert_equal expected.to_s, actual.to_s, message
|
|
|
|
end
|
|
|
|
end
|
2006-03-05 13:43:56 -05:00
|
|
|
|
2006-03-05 18:41:17 -05:00
|
|
|
def assert_queries(num = 1)
|
2007-02-06 16:16:07 -05:00
|
|
|
$query_count = 0
|
2006-03-05 13:43:56 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2007-02-06 16:16:07 -05:00
|
|
|
assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
|
2006-03-05 18:41:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_no_queries(&block)
|
|
|
|
assert_queries(0, &block)
|
2006-03-05 13:43:56 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-10-27 04:18:41 -04:00
|
|
|
|
2006-07-07 06:48:43 -04:00
|
|
|
def current_adapter?(*types)
|
|
|
|
types.any? do |type|
|
|
|
|
ActiveRecord::ConnectionAdapters.const_defined?(type) &&
|
2007-01-29 22:14:55 -05:00
|
|
|
ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters.const_get(type))
|
2006-07-07 06:48:43 -04:00
|
|
|
end
|
2005-10-27 04:18:41 -04:00
|
|
|
end
|
2005-11-19 04:53:36 -05:00
|
|
|
|
2007-02-06 16:16:07 -05:00
|
|
|
def uses_mocha(test_name)
|
2007-03-06 04:39:50 -05:00
|
|
|
require 'rubygems'
|
2007-02-06 16:16:07 -05:00
|
|
|
require 'mocha'
|
|
|
|
yield
|
|
|
|
rescue LoadError
|
|
|
|
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
|
|
|
|
end
|
2006-07-07 06:48:43 -04:00
|
|
|
|
2007-10-02 01:32:14 -04:00
|
|
|
ActiveRecord::Base.connection.class.class_eval do
|
|
|
|
unless defined? IGNORED_SQL
|
2007-02-06 16:16:07 -05:00
|
|
|
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/]
|
|
|
|
|
|
|
|
def execute_with_counting(sql, name = nil, &block)
|
|
|
|
$query_count ||= 0
|
|
|
|
$query_count += 1 unless IGNORED_SQL.any? { |r| sql =~ r }
|
|
|
|
execute_without_counting(sql, name, &block)
|
|
|
|
end
|
2006-07-07 06:48:43 -04:00
|
|
|
|
2007-02-06 16:16:07 -05:00
|
|
|
alias_method_chain :execute, :counting
|
2006-03-05 13:43:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-05-30 17:40:55 -04:00
|
|
|
# Make with_scope public for tests
|
|
|
|
class << ActiveRecord::Base
|
|
|
|
public :with_scope, :with_exclusive_scope
|
|
|
|
end
|
|
|
|
|
2005-11-19 04:53:36 -05:00
|
|
|
#ActiveRecord::Base.logger = Logger.new(STDOUT)
|
|
|
|
#ActiveRecord::Base.colorize_logging = false
|