2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "models/topic"
|
|
|
|
require "models/task"
|
2006-05-07 03:52:08 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class DateTimeTest < ActiveRecord::TestCase
|
2014-12-22 21:30:00 -05:00
|
|
|
include InTimeZone
|
|
|
|
|
2007-05-28 01:55:27 -04:00
|
|
|
def test_saves_both_date_and_time
|
2016-08-06 12:26:20 -04:00
|
|
|
with_env_tz "America/New_York" do
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config default: :utc do
|
2010-09-20 15:15:18 -04:00
|
|
|
time_values = [1807, 2, 10, 15, 30, 45]
|
|
|
|
# create DateTime value with local time zone offset
|
2012-12-11 08:57:05 -05:00
|
|
|
local_offset = Rational(Time.local(*time_values).utc_offset, 86400)
|
2010-09-20 15:15:18 -04:00
|
|
|
now = DateTime.civil(*(time_values + [local_offset]))
|
2007-05-29 04:29:33 -04:00
|
|
|
|
2010-09-20 15:15:18 -04:00
|
|
|
task = Task.new
|
|
|
|
task.starting = now
|
|
|
|
task.save!
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2012-12-11 08:57:05 -05:00
|
|
|
# check against Time.local, since some platforms will return a Time instead of a DateTime
|
|
|
|
assert_equal Time.local(*time_values), Task.find(task.id).starting
|
2010-09-20 15:15:18 -04:00
|
|
|
end
|
|
|
|
end
|
2007-05-28 01:55:27 -04:00
|
|
|
end
|
|
|
|
|
2006-05-07 03:52:08 -04:00
|
|
|
def test_assign_empty_date_time
|
|
|
|
task = Task.new
|
2016-08-06 12:26:20 -04:00
|
|
|
task.starting = ""
|
2006-05-07 03:52:08 -04:00
|
|
|
task.ending = nil
|
|
|
|
assert_nil task.starting
|
|
|
|
assert_nil task.ending
|
|
|
|
end
|
|
|
|
|
2014-12-22 21:30:00 -05:00
|
|
|
def test_assign_bad_date_time_with_timezone
|
|
|
|
in_time_zone "Pacific Time (US & Canada)" do
|
|
|
|
task = Task.new
|
2016-08-06 12:26:20 -04:00
|
|
|
task.starting = "2014-07-01T24:59:59GMT"
|
2014-12-22 21:30:00 -05:00
|
|
|
assert_nil task.starting
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-05-07 03:52:08 -04:00
|
|
|
def test_assign_empty_date
|
|
|
|
topic = Topic.new
|
2016-08-06 12:26:20 -04:00
|
|
|
topic.last_read = ""
|
2006-05-07 03:52:08 -04:00
|
|
|
assert_nil topic.last_read
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_empty_time
|
|
|
|
topic = Topic.new
|
2016-08-06 12:26:20 -04:00
|
|
|
topic.bonus_time = ""
|
2006-05-07 03:52:08 -04:00
|
|
|
assert_nil topic.bonus_time
|
|
|
|
end
|
2015-01-02 18:03:47 -05:00
|
|
|
|
|
|
|
def test_assign_in_local_timezone
|
2017-03-01 13:13:15 -05:00
|
|
|
now = DateTime.civil(2017, 3, 1, 12, 0, 0)
|
2015-01-02 18:03:47 -05:00
|
|
|
with_timezone_config default: :local do
|
|
|
|
task = Task.new starting: now
|
2015-01-17 11:06:10 -05:00
|
|
|
assert_equal now, task.starting
|
2015-01-02 18:03:47 -05:00
|
|
|
end
|
|
|
|
end
|
2017-07-04 14:22:35 -04:00
|
|
|
|
|
|
|
def test_date_time_with_string_value_with_subsecond_precision
|
2020-04-24 13:56:53 -04:00
|
|
|
skip unless supports_datetime_with_precision?
|
2017-07-04 14:22:35 -04:00
|
|
|
string_value = "2017-07-04 14:19:00.5"
|
|
|
|
topic = Topic.create(written_on: string_value)
|
|
|
|
assert_equal topic, Topic.find_by(written_on: string_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_date_time_with_string_value_with_non_iso_format
|
|
|
|
string_value = "04/07/2017 2:19pm"
|
|
|
|
topic = Topic.create(written_on: string_value)
|
|
|
|
assert_equal topic, Topic.find_by(written_on: string_value)
|
|
|
|
end
|
2006-05-07 03:52:08 -04:00
|
|
|
end
|