Skip to content
Browse files

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>
  • Loading branch information...
1 parent 07411ca commit ae24ce52aec97f4fc11508a3c3edf98d35f028c1 @miloops miloops committed with josevalim Sep 20, 2010
Showing with 27 additions and 9 deletions.
  1. +13 −9 activerecord/test/cases/date_time_test.rb
  2. +14 −0 activerecord/test/cases/helper.rb
View
22 activerecord/test/cases/date_time_test.rb
@@ -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
View
14 activerecord/test/cases/helper.rb
@@ -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/]

0 comments on commit ae24ce5

Please sign in to comment.
Something went wrong with that request. Please try again.