From 10b81fb51acc600926613e9ea78125635950a1f0 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 12 Jun 2016 01:03:10 +0900 Subject: Fix `Type::Date#serialize` to return a date object correctly Currently `Type::Date#serialize` does not cast a value to a date object. It should be cast to a date object for finding by date column correctly working. Fixes #25354. --- activerecord/test/cases/date_test.rb | 44 ++++++++++++++++++++++++++++ activerecord/test/cases/invalid_date_test.rb | 32 -------------------- 2 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 activerecord/test/cases/date_test.rb delete mode 100644 activerecord/test/cases/invalid_date_test.rb (limited to 'activerecord') diff --git a/activerecord/test/cases/date_test.rb b/activerecord/test/cases/date_test.rb new file mode 100644 index 0000000000..f112081c14 --- /dev/null +++ b/activerecord/test/cases/date_test.rb @@ -0,0 +1,44 @@ +require 'cases/helper' +require 'models/topic' + +class DateTest < ActiveRecord::TestCase + def test_date_with_time_value + time_value = Time.new(2016, 05, 11, 19, 0, 0) + topic = Topic.create(last_read: time_value) + assert_equal topic, Topic.find_by(last_read: time_value) + end + + def test_date_with_string_value + string_value = '2016-05-11 19:00:00' + topic = Topic.create(last_read: string_value) + assert_equal topic, Topic.find_by(last_read: string_value) + end + + def test_assign_valid_dates + valid_dates = [[2007, 11, 30], [1993, 2, 28], [2008, 2, 29]] + + invalid_dates = [[2007, 11, 31], [1993, 2, 29], [2007, 2, 29]] + + valid_dates.each do |date_src| + topic = Topic.new("last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s) + # Oracle DATE columns are datetime columns and Oracle adapter returns Time value + if current_adapter?(:OracleAdapter) + assert_equal(topic.last_read.to_date, Date.new(*date_src)) + else + assert_equal(topic.last_read, Date.new(*date_src)) + end + end + + invalid_dates.each do |date_src| + assert_nothing_raised do + topic = Topic.new({"last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s}) + # Oracle DATE columns are datetime columns and Oracle adapter returns Time value + if current_adapter?(:OracleAdapter) + assert_equal(topic.last_read.to_date, Time.local(*date_src).to_date, "The date should be modified according to the behavior of the Time object") + else + assert_equal(topic.last_read, Time.local(*date_src).to_date, "The date should be modified according to the behavior of the Time object") + end + end + end + end +end diff --git a/activerecord/test/cases/invalid_date_test.rb b/activerecord/test/cases/invalid_date_test.rb deleted file mode 100644 index 426a350379..0000000000 --- a/activerecord/test/cases/invalid_date_test.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'cases/helper' -require 'models/topic' - -class InvalidDateTest < ActiveRecord::TestCase - def test_assign_valid_dates - valid_dates = [[2007, 11, 30], [1993, 2, 28], [2008, 2, 29]] - - invalid_dates = [[2007, 11, 31], [1993, 2, 29], [2007, 2, 29]] - - valid_dates.each do |date_src| - topic = Topic.new("last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s) - # Oracle DATE columns are datetime columns and Oracle adapter returns Time value - if current_adapter?(:OracleAdapter) - assert_equal(topic.last_read.to_date, Date.new(*date_src)) - else - assert_equal(topic.last_read, Date.new(*date_src)) - end - end - - invalid_dates.each do |date_src| - assert_nothing_raised do - topic = Topic.new({"last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s}) - # Oracle DATE columns are datetime columns and Oracle adapter returns Time value - if current_adapter?(:OracleAdapter) - assert_equal(topic.last_read.to_date, Time.local(*date_src).to_date, "The date should be modified according to the behavior of the Time object") - else - assert_equal(topic.last_read, Time.local(*date_src).to_date, "The date should be modified according to the behavior of the Time object") - end - end - end - end -end -- cgit v1.2.3