2010-02-23 20:31:17 -05:00
|
|
|
require File.expand_path('../../../../load_paths', __FILE__)
|
2009-11-10 00:28:36 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
require 'config'
|
2009-02-03 21:25:37 -05:00
|
|
|
|
2012-12-28 14:17:37 -05:00
|
|
|
require 'active_support/testing/autorun'
|
2009-05-17 14:51:35 -04:00
|
|
|
require 'stringio'
|
2008-01-21 12:20:51 -05:00
|
|
|
|
|
|
|
require 'active_record'
|
2012-08-02 15:34:44 -04:00
|
|
|
require 'cases/test_case'
|
2010-03-07 09:24:30 -05:00
|
|
|
require 'active_support/dependencies'
|
2011-12-20 00:05:28 -05:00
|
|
|
require 'active_support/logger'
|
2011-06-04 18:19:17 -04:00
|
|
|
|
|
|
|
require 'support/config'
|
|
|
|
require 'support/connection'
|
|
|
|
|
|
|
|
# TODO: Move all these random hacks into the ARTest namespace and into the support/ dir
|
2008-01-21 12:20:51 -05:00
|
|
|
|
2013-01-24 05:10:58 -05:00
|
|
|
Thread.abort_on_exception = true
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
# Show backtraces for deprecated behavior for quicker cleanup.
|
|
|
|
ActiveSupport::Deprecation.debug = true
|
|
|
|
|
2011-06-09 15:15:34 -04:00
|
|
|
# Connect to the database
|
|
|
|
ARTest.connect
|
|
|
|
|
|
|
|
# Quote "type" if it's a reserved word for the current connection.
|
|
|
|
QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type')
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
def current_adapter?(*types)
|
|
|
|
types.any? do |type|
|
|
|
|
ActiveRecord::ConnectionAdapters.const_defined?(type) &&
|
|
|
|
ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters.const_get(type))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-08 13:33:33 -05:00
|
|
|
def in_memory_db?
|
2012-04-27 02:31:37 -04:00
|
|
|
current_adapter?(:SQLite3Adapter) &&
|
2011-01-08 13:33:33 -05:00
|
|
|
ActiveRecord::Base.connection_pool.spec.config[:database] == ":memory:"
|
|
|
|
end
|
|
|
|
|
2011-01-09 10:52:41 -05:00
|
|
|
def supports_savepoints?
|
|
|
|
ActiveRecord::Base.connection.supports_savepoints?
|
|
|
|
end
|
|
|
|
|
2010-09-20 15:15:18 -04:00
|
|
|
def with_env_tz(new_tz = 'US/Eastern')
|
|
|
|
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_active_record_default_timezone(zone)
|
|
|
|
old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.default_timezone = old_zone
|
|
|
|
end
|
|
|
|
|
2008-09-14 15:40:09 -04:00
|
|
|
unless ENV['FIXTURE_DEBUG']
|
2008-11-07 15:40:56 -05:00
|
|
|
module ActiveRecord::TestFixtures::ClassMethods
|
|
|
|
def try_to_load_dependency_with_silence(*args)
|
2011-12-20 00:05:28 -05:00
|
|
|
old = ActiveRecord::Base.logger.level
|
|
|
|
ActiveRecord::Base.logger.level = ActiveSupport::Logger::ERROR
|
|
|
|
try_to_load_dependency_without_silence(*args)
|
|
|
|
ActiveRecord::Base.logger.level = old
|
2008-09-14 15:40:09 -04:00
|
|
|
end
|
2008-11-07 15:40:56 -05:00
|
|
|
|
|
|
|
alias_method_chain :try_to_load_dependency, :silence
|
2008-09-14 15:40:09 -04:00
|
|
|
end
|
2008-11-07 15:40:56 -05:00
|
|
|
end
|
2008-11-15 15:30:02 -05:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
require "cases/validations_repair_helper"
|
2008-11-15 15:30:02 -05:00
|
|
|
class ActiveSupport::TestCase
|
|
|
|
include ActiveRecord::TestFixtures
|
2009-12-23 07:30:58 -05:00
|
|
|
include ActiveRecord::ValidationsRepairHelper
|
2008-12-19 08:27:34 -05:00
|
|
|
|
2008-11-15 15:30:02 -05:00
|
|
|
self.fixture_path = FIXTURES_ROOT
|
|
|
|
self.use_instantiated_fixtures = false
|
|
|
|
self.use_transactional_fixtures = true
|
2008-11-15 15:31:54 -05:00
|
|
|
|
2012-01-01 12:54:52 -05:00
|
|
|
def create_fixtures(*fixture_set_names, &block)
|
2012-05-12 07:17:03 -04:00
|
|
|
ActiveRecord::FixtureSet.create_fixtures(ActiveSupport::TestCase.fixture_path, fixture_set_names, fixture_class_names, &block)
|
2008-09-14 15:40:09 -04:00
|
|
|
end
|
2008-11-15 15:30:02 -05:00
|
|
|
end
|
2009-05-17 14:51:35 -04:00
|
|
|
|
2011-01-08 13:33:33 -05:00
|
|
|
def load_schema
|
|
|
|
# silence verbose schema loading
|
|
|
|
original_stdout = $stdout
|
|
|
|
$stdout = StringIO.new
|
2009-05-17 14:51:35 -04:00
|
|
|
|
|
|
|
adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
|
|
|
|
adapter_specific_schema_file = SCHEMA_ROOT + "/#{adapter_name}_specific_schema.rb"
|
|
|
|
|
|
|
|
load SCHEMA_ROOT + "/schema.rb"
|
|
|
|
|
|
|
|
if File.exists?(adapter_specific_schema_file)
|
|
|
|
load adapter_specific_schema_file
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
$stdout = original_stdout
|
|
|
|
end
|
2010-07-26 08:06:14 -04:00
|
|
|
|
2011-01-08 13:33:33 -05:00
|
|
|
load_schema
|
|
|
|
|
2010-07-26 08:06:14 -04:00
|
|
|
class << Time
|
|
|
|
unless method_defined? :now_before_time_travel
|
|
|
|
alias_method :now_before_time_travel, :now
|
|
|
|
end
|
|
|
|
|
|
|
|
def now
|
|
|
|
(@now ||= nil) || now_before_time_travel
|
|
|
|
end
|
|
|
|
|
|
|
|
def travel_to(time, &block)
|
|
|
|
@now = time
|
|
|
|
block.call
|
|
|
|
ensure
|
|
|
|
@now = nil
|
|
|
|
end
|
|
|
|
end
|
2012-05-18 15:40:36 -04:00
|
|
|
|
|
|
|
module LogIntercepter
|
|
|
|
attr_accessor :logged, :intercepted
|
|
|
|
def self.extended(base)
|
|
|
|
base.logged = []
|
|
|
|
end
|
|
|
|
def log(sql, name, binds = [], &block)
|
|
|
|
if @intercepted
|
|
|
|
@logged << [sql, name, binds]
|
|
|
|
yield
|
|
|
|
else
|
|
|
|
super(sql, name,binds, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-17 11:01:35 -05:00
|
|
|
module InTimeZone
|
|
|
|
private
|
|
|
|
|
2012-11-17 10:35:39 -05:00
|
|
|
def in_time_zone(zone)
|
|
|
|
old_zone = Time.zone
|
|
|
|
old_tz = ActiveRecord::Base.time_zone_aware_attributes
|
|
|
|
|
|
|
|
Time.zone = zone ? ActiveSupport::TimeZone[zone] : nil
|
|
|
|
ActiveRecord::Base.time_zone_aware_attributes = !zone.nil?
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Time.zone = old_zone
|
|
|
|
ActiveRecord::Base.time_zone_aware_attributes = old_tz
|
|
|
|
end
|
|
|
|
end
|