1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move helper methods to helper.rb. Make test not depend on local TZ to pass or fail.

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Emilio Tagua 2010-09-20 16:15:18 -03:00 committed by José Valim
parent 07411ca490
commit ae24ce52ae
2 changed files with 27 additions and 9 deletions

View file

@ -4,17 +4,21 @@ require 'models/task'
class DateTimeTest < ActiveRecord::TestCase
def test_saves_both_date_and_time
time_values = [1807, 2, 10, 15, 30, 45]
# create DateTime value with local time zone offset
local_offset = Rational(Time.local_time(*time_values).utc_offset, 86400)
now = DateTime.civil(*(time_values + [local_offset]))
with_env_tz 'America/New_York' do
with_active_record_default_timezone :utc do
time_values = [1807, 2, 10, 15, 30, 45]
# create DateTime value with local time zone offset
local_offset = Rational(Time.local_time(*time_values).utc_offset, 86400)
now = DateTime.civil(*(time_values + [local_offset]))
task = Task.new
task.starting = now
task.save!
task = Task.new
task.starting = now
task.save!
# check against Time.local_time, since some platforms will return a Time instead of a DateTime
assert_equal Time.local_time(*time_values), Task.find(task.id).starting
# check against Time.local_time, since some platforms will return a Time instead of a DateTime
assert_equal Time.local_time(*time_values), Task.find(task.id).starting
end
end
end
def test_assign_empty_date_time

View file

@ -31,6 +31,20 @@ def current_adapter?(*types)
end
end
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
ActiveRecord::Base.connection.class.class_eval do
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]