2017-07-09 08:06:36 -04:00
# frozen_string_literal: true
2017-07-10 09:39:13 -04:00
2018-09-29 20:50:43 -04:00
require_relative " abstract_unit "
2016-08-06 12:03:25 -04:00
require " active_support/core_ext/date_time "
require " active_support/core_ext/numeric/time "
2020-11-26 03:35:23 -05:00
require " active_support/core_ext/string/conversions "
2018-09-29 20:50:43 -04:00
require_relative " time_zone_test_helpers "
2014-08-19 16:17:23 -04:00
2014-08-19 14:29:26 -04:00
class TimeTravelTest < ActiveSupport :: TestCase
2018-12-20 19:20:08 -05:00
include TimeZoneTestHelpers
2017-01-09 05:24:53 -05:00
class TimeSubclass < :: Time ; end
class DateSubclass < :: Date ; end
class DateTimeSubclass < :: DateTime ; end
2014-08-19 14:29:26 -04:00
def test_time_helper_travel
2015-06-04 14:25:45 -04:00
Time . stub ( :now , Time . now ) do
2018-12-20 12:44:01 -05:00
expected_time = Time . now + 1 . day
travel 1 . day
2014-08-19 14:29:26 -04:00
2018-12-20 12:44:01 -05:00
assert_equal expected_time . to_s ( :db ) , Time . now . to_s ( :db )
assert_equal expected_time . to_date , Date . today
assert_equal expected_time . to_datetime . to_s ( :db ) , DateTime . now . to_s ( :db )
ensure
travel_back
2014-08-19 14:29:26 -04:00
end
end
2015-06-04 14:25:45 -04:00
def test_time_helper_travel_with_block
Time . stub ( :now , Time . now ) do
expected_time = Time . now + 1 . day
travel 1 . day do
assert_equal expected_time . to_s ( :db ) , Time . now . to_s ( :db )
assert_equal expected_time . to_date , Date . today
assert_equal expected_time . to_datetime . to_s ( :db ) , DateTime . now . to_s ( :db )
end
assert_not_equal expected_time . to_s ( :db ) , Time . now . to_s ( :db )
assert_not_equal expected_time . to_date , Date . today
assert_not_equal expected_time . to_datetime . to_s ( :db ) , DateTime . now . to_s ( :db )
end
2014-08-19 14:29:26 -04:00
end
2015-06-04 14:25:45 -04:00
def test_time_helper_travel_to
Time . stub ( :now , Time . now ) do
2020-01-15 05:57:38 -05:00
expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
2018-12-20 12:44:01 -05:00
travel_to expected_time
2014-08-19 14:29:26 -04:00
2018-12-20 12:44:01 -05:00
assert_equal expected_time , Time . now
assert_equal Date . new ( 2004 , 11 , 24 ) , Date . today
assert_equal expected_time . to_datetime , DateTime . now
ensure
travel_back
2014-08-19 14:29:26 -04:00
end
2015-06-04 14:25:45 -04:00
end
2014-08-19 14:29:26 -04:00
2015-06-04 14:25:45 -04:00
def test_time_helper_travel_to_with_block
Time . stub ( :now , Time . now ) do
2020-01-15 05:57:38 -05:00
expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
2015-06-04 14:25:45 -04:00
travel_to expected_time do
assert_equal expected_time , Time . now
assert_equal Date . new ( 2004 , 11 , 24 ) , Date . today
assert_equal expected_time . to_datetime , DateTime . now
end
assert_not_equal expected_time , Time . now
assert_not_equal Date . new ( 2004 , 11 , 24 ) , Date . today
assert_not_equal expected_time . to_datetime , DateTime . now
end
2014-08-19 14:29:26 -04:00
end
2018-12-20 19:20:08 -05:00
def test_time_helper_travel_to_with_time_zone
with_env_tz " US/Eastern " do
with_tz_default ActiveSupport :: TimeZone [ " UTC " ] do
Time . stub ( :now , Time . now ) do
expected_time = 5 . minutes . ago
travel_to 5 . minutes . ago do
assert_equal expected_time . to_s ( :db ) , Time . zone . now . to_s ( :db )
end
end
end
end
end
2020-11-26 03:35:23 -05:00
def test_time_helper_travel_to_with_string_for_time_zone
with_env_tz " US/Eastern " do
with_tz_default ActiveSupport :: TimeZone [ " UTC " ] do
Time . stub ( :now , Time . now ) do
expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
travel_to " 2004-11-24 01:04:44 " do
assert_equal expected_time . to_s ( :db ) , Time . zone . now . to_s ( :db )
end
end
end
end
end
2014-08-19 14:29:26 -04:00
def test_time_helper_travel_back
2015-06-04 14:25:45 -04:00
Time . stub ( :now , Time . now ) do
2020-01-15 05:57:38 -05:00
expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
2014-08-19 14:29:26 -04:00
2018-12-20 12:44:01 -05:00
travel_to expected_time
assert_equal expected_time , Time . now
assert_equal Date . new ( 2004 , 11 , 24 ) , Date . today
assert_equal expected_time . to_datetime , DateTime . now
travel_back
2014-08-19 14:29:26 -04:00
2018-12-20 12:44:01 -05:00
assert_not_equal expected_time , Time . now
assert_not_equal Date . new ( 2004 , 11 , 24 ) , Date . today
assert_not_equal expected_time . to_datetime , DateTime . now
2019-10-17 11:15:15 -04:00
ensure
travel_back
end
end
def test_time_helper_travel_back_with_block
Time . stub ( :now , Time . now ) do
2020-01-15 05:57:38 -05:00
expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
2019-10-17 11:15:15 -04:00
travel_to expected_time
assert_equal expected_time , Time . now
assert_equal Date . new ( 2004 , 11 , 24 ) , Date . today
assert_equal expected_time . to_datetime , DateTime . now
travel_back do
assert_not_equal expected_time , Time . now
assert_not_equal Date . new ( 2004 , 11 , 24 ) , Date . today
assert_not_equal expected_time . to_datetime , DateTime . now
end
assert_equal expected_time , Time . now
assert_equal Date . new ( 2004 , 11 , 24 ) , Date . today
assert_equal expected_time . to_datetime , DateTime . now
2018-12-20 12:44:01 -05:00
ensure
travel_back
2015-06-04 14:25:45 -04:00
end
2014-08-19 14:29:26 -04:00
end
2016-05-06 12:39:32 -04:00
def test_time_helper_travel_to_with_nested_calls_with_blocks
Time . stub ( :now , Time . now ) do
2020-01-15 05:57:38 -05:00
outer_expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
inner_expected_time = Time . new ( 2004 , 10 , 24 , 1 , 4 , 44 )
2016-05-06 12:39:32 -04:00
travel_to outer_expected_time do
2017-01-24 19:46:29 -05:00
e = assert_raises ( RuntimeError ) do
2016-05-06 12:39:32 -04:00
travel_to ( inner_expected_time ) do
2017-12-14 03:30:54 -05:00
# noop
2016-05-06 12:39:32 -04:00
end
end
2017-05-06 15:08:58 -04:00
assert_match ( / Calling `travel_to` with a block, when we have previously already made a call to `travel_to`, can lead to confusing time stubbing \ . / , e . message )
2016-05-06 12:39:32 -04:00
end
end
end
def test_time_helper_travel_to_with_nested_calls
Time . stub ( :now , Time . now ) do
2020-01-15 05:57:38 -05:00
outer_expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
inner_expected_time = Time . new ( 2004 , 10 , 24 , 1 , 4 , 44 )
2016-05-06 12:39:32 -04:00
travel_to outer_expected_time do
assert_nothing_raised do
travel_to ( inner_expected_time )
assert_equal inner_expected_time , Time . now
end
end
end
end
def test_time_helper_travel_to_with_subsequent_calls
Time . stub ( :now , Time . now ) do
2020-01-15 05:57:38 -05:00
initial_expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
subsequent_expected_time = Time . new ( 2004 , 10 , 24 , 1 , 4 , 44 )
2018-12-20 12:44:01 -05:00
assert_nothing_raised do
travel_to initial_expected_time
travel_to subsequent_expected_time
2016-05-06 12:39:32 -04:00
2018-12-20 12:44:01 -05:00
assert_equal subsequent_expected_time , Time . now
2016-05-06 12:39:32 -04:00
travel_back
end
2018-12-20 12:44:01 -05:00
ensure
travel_back
2016-05-06 12:39:32 -04:00
end
end
2021-05-11 08:46:30 -04:00
def test_time_helper_travel_with_subsequent_block
Time . stub ( :now , Time . now ) do
outer_expected_time = Time . new ( 2004 , 11 , 24 , 1 , 4 , 44 )
inner_expected_time = Time . new ( 2004 , 10 , 24 , 1 , 4 , 44 )
travel_to outer_expected_time
2021-06-08 10:54:42 -04:00
assert_equal outer_expected_time , Time . now
2021-05-11 08:46:30 -04:00
assert_nothing_raised do
travel_to ( inner_expected_time ) do
assert_equal inner_expected_time , Time . now
end
end
2021-06-08 10:54:42 -04:00
assert_equal outer_expected_time , Time . now
ensure
travel_back
2021-05-11 08:46:30 -04:00
end
end
2019-02-01 11:47:10 -05:00
def test_travel_to_will_reset_the_usec_to_avoid_mysql_rounding
2015-06-04 14:25:45 -04:00
Time . stub ( :now , Time . now ) do
travel_to Time . utc ( 2014 , 10 , 10 , 10 , 10 , 50 , 999999 ) do
assert_equal 50 , Time . now . sec
assert_equal 0 , Time . now . usec
assert_equal 50 , DateTime . now . sec
assert_equal 0 , DateTime . now . usec
end
2014-08-19 14:29:26 -04:00
end
end
2017-01-09 05:24:53 -05:00
def test_time_helper_travel_with_time_subclass
assert_equal TimeSubclass , TimeSubclass . now . class
assert_equal DateSubclass , DateSubclass . today . class
assert_equal DateTimeSubclass , DateTimeSubclass . now . class
travel 1 . day do
assert_equal TimeSubclass , TimeSubclass . now . class
assert_equal DateSubclass , DateSubclass . today . class
assert_equal DateTimeSubclass , DateTimeSubclass . now . class
assert_equal Time . now . to_s , TimeSubclass . now . to_s
assert_equal Date . today . to_s , DateSubclass . today . to_s
assert_equal DateTime . now . to_s , DateTimeSubclass . now . to_s
end
end
2017-07-10 06:13:37 -04:00
def test_time_helper_freeze_time
expected_time = Time . now
freeze_time
sleep ( 1 )
assert_equal expected_time . to_s ( :db ) , Time . now . to_s ( :db )
ensure
travel_back
end
def test_time_helper_freeze_time_with_block
expected_time = Time . now
freeze_time do
sleep ( 1 )
assert_equal expected_time . to_s ( :db ) , Time . now . to_s ( :db )
end
assert_operator expected_time . to_s ( :db ) , :< , Time . now . to_s ( :db )
end
2018-09-07 05:09:46 -04:00
def test_time_helper_unfreeze_time
assert_equal method ( :travel_back ) , method ( :unfreeze_time )
end
2014-08-19 14:29:26 -04:00
end