| 1 |
$:.unshift(File.dirname(__FILE__) + '/../lib') |
|---|
| 2 |
$:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib') |
|---|
| 3 |
|
|---|
| 4 |
require 'test/unit' |
|---|
| 5 |
require 'active_record' |
|---|
| 6 |
require 'active_record/fixtures' |
|---|
| 7 |
require 'active_support/test_case' |
|---|
| 8 |
require 'connection' |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
ActiveSupport::Deprecation.debug = true |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type') unless Object.const_defined?(:QUOTED_TYPE) |
|---|
| 15 |
|
|---|
| 16 |
class Test::Unit::TestCase |
|---|
| 17 |
self.fixture_path = File.dirname(__FILE__) + "/fixtures/" |
|---|
| 18 |
self.use_instantiated_fixtures = false |
|---|
| 19 |
self.use_transactional_fixtures = (ENV['AR_NO_TX_FIXTURES'] != "yes") |
|---|
| 20 |
|
|---|
| 21 |
def create_fixtures(*table_names, &block) |
|---|
| 22 |
Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, {}, &block) |
|---|
| 23 |
end |
|---|
| 24 |
|
|---|
| 25 |
def assert_date_from_db(expected, actual, message = nil) |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
if current_adapter?(:SQLServerAdapter) |
|---|
| 29 |
assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00") |
|---|
| 30 |
elsif current_adapter?(:SybaseAdapter) |
|---|
| 31 |
assert_equal expected.to_s, actual.to_date.to_s, message |
|---|
| 32 |
else |
|---|
| 33 |
assert_equal expected.to_s, actual.to_s, message |
|---|
| 34 |
end |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
def assert_queries(num = 1) |
|---|
| 38 |
$query_count = 0 |
|---|
| 39 |
yield |
|---|
| 40 |
ensure |
|---|
| 41 |
assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed." |
|---|
| 42 |
end |
|---|
| 43 |
|
|---|
| 44 |
def assert_no_queries(&block) |
|---|
| 45 |
assert_queries(0, &block) |
|---|
| 46 |
end |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
def current_adapter?(*types) |
|---|
| 50 |
types.any? do |type| |
|---|
| 51 |
ActiveRecord::ConnectionAdapters.const_defined?(type) && |
|---|
| 52 |
ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters.const_get(type)) |
|---|
| 53 |
end |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
def uses_mocha(test_name) |
|---|
| 57 |
require 'rubygems' |
|---|
| 58 |
require 'mocha' |
|---|
| 59 |
yield |
|---|
| 60 |
rescue LoadError |
|---|
| 61 |
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again." |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
ActiveRecord::Base.connection.class.class_eval do |
|---|
| 65 |
unless defined? IGNORED_SQL |
|---|
| 66 |
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/] |
|---|
| 67 |
|
|---|
| 68 |
def execute_with_counting(sql, name = nil, &block) |
|---|
| 69 |
$query_count ||= 0 |
|---|
| 70 |
$query_count += 1 unless IGNORED_SQL.any? { |r| sql =~ r } |
|---|
| 71 |
execute_without_counting(sql, name, &block) |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
alias_method_chain :execute, :counting |
|---|
| 75 |
end |
|---|
| 76 |
end |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
class << ActiveRecord::Base |
|---|
| 80 |
public :with_scope, :with_exclusive_scope |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|