diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2014-12-17 16:01:28 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2015-01-02 22:30:07 +0900 |
commit | 4157f5d172830df07b44a66d4096d152e068dc4a (patch) | |
tree | f62df286d1f9ca7137fa4149358370f0fa6ef315 /activerecord/test/cases/adapters | |
parent | ae419af6667125662a4324e8809e2a15aad686d2 (diff) | |
download | rails-4157f5d172830df07b44a66d4096d152e068dc4a.tar.gz rails-4157f5d172830df07b44a66d4096d152e068dc4a.tar.bz2 rails-4157f5d172830df07b44a66d4096d152e068dc4a.zip |
Format the datetime string according to the precision of the datetime field.
Incompatible to rounding behavior between MySQL 5.6 and earlier.
In 5.5, when you insert `2014-08-17 12:30:00.999999` the fractional part
is ignored. In 5.6, it's rounded to `2014-08-17 12:30:01`:
http://bugs.mysql.com/bug.php?id=68760
Diffstat (limited to 'activerecord/test/cases/adapters')
-rw-r--r-- | activerecord/test/cases/adapters/mysql/datetime_test.rb | 17 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/mysql2/datetime_test.rb | 17 |
2 files changed, 34 insertions, 0 deletions
diff --git a/activerecord/test/cases/adapters/mysql/datetime_test.rb b/activerecord/test/cases/adapters/mysql/datetime_test.rb index 1705fecce0..fadca1fcf4 100644 --- a/activerecord/test/cases/adapters/mysql/datetime_test.rb +++ b/activerecord/test/cases/adapters/mysql/datetime_test.rb @@ -2,6 +2,9 @@ require 'cases/helper' if mysql_56? class DateTimeTest < ActiveRecord::TestCase + self.use_transactional_fixtures = false + + class Foo < ActiveRecord::Base; end def test_default_datetime_precision ActiveRecord::Base.connection.create_table(:foos, force: true) @@ -50,6 +53,20 @@ if mysql_56? assert_equal 4, mysql_datetime_precision('foos', 'updated_at') end + def test_formatting_datetime_according_to_precision + ActiveRecord::Base.connection.create_table(:foos, force: true) do |t| + t.datetime :created_at, precision: 0 + t.datetime :updated_at, precision: 4 + end + date = ::Time.utc(2014, 8, 17, 12, 30, 0, 999999) + Foo.create!(created_at: date, updated_at: date) + assert foo = Foo.find_by(created_at: date) + assert_equal date.to_s, foo.created_at.to_s + assert_equal date.to_s, foo.updated_at.to_s + assert_equal 000000, foo.created_at.usec + assert_equal 999900, foo.updated_at.usec + end + private def mysql_datetime_precision(table_name, column_name) diff --git a/activerecord/test/cases/adapters/mysql2/datetime_test.rb b/activerecord/test/cases/adapters/mysql2/datetime_test.rb index 1705fecce0..fadca1fcf4 100644 --- a/activerecord/test/cases/adapters/mysql2/datetime_test.rb +++ b/activerecord/test/cases/adapters/mysql2/datetime_test.rb @@ -2,6 +2,9 @@ require 'cases/helper' if mysql_56? class DateTimeTest < ActiveRecord::TestCase + self.use_transactional_fixtures = false + + class Foo < ActiveRecord::Base; end def test_default_datetime_precision ActiveRecord::Base.connection.create_table(:foos, force: true) @@ -50,6 +53,20 @@ if mysql_56? assert_equal 4, mysql_datetime_precision('foos', 'updated_at') end + def test_formatting_datetime_according_to_precision + ActiveRecord::Base.connection.create_table(:foos, force: true) do |t| + t.datetime :created_at, precision: 0 + t.datetime :updated_at, precision: 4 + end + date = ::Time.utc(2014, 8, 17, 12, 30, 0, 999999) + Foo.create!(created_at: date, updated_at: date) + assert foo = Foo.find_by(created_at: date) + assert_equal date.to_s, foo.created_at.to_s + assert_equal date.to_s, foo.updated_at.to_s + assert_equal 000000, foo.created_at.usec + assert_equal 999900, foo.updated_at.usec + end + private def mysql_datetime_precision(table_name, column_name) |