Skip to content

Commit

Permalink
Move helper methods to helper.rb. Make test not depend on local TZ to…
Browse files Browse the repository at this point in the history
… pass or fail.

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
miloops authored and josevalim committed Sep 21, 2010
1 parent 07411ca commit ae24ce5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
22 changes: 13 additions & 9 deletions activerecord/test/cases/date_time_test.rb
Expand Up @@ -4,17 +4,21 @@

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
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/helper.rb
Expand Up @@ -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/]

Expand Down

0 comments on commit ae24ce5

Please sign in to comment.