Skip to content

Commit 2877bc8

Browse files
committed
Fix datetime precision tests
1 parent 521d480 commit 2877bc8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test/cases/coerced_tests.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,53 @@ def test_invalid_datetime_precision_raises_error_coerced
930930
end
931931
end
932932
end
933+
934+
# datetime is rounded to increments of .000, .003, or .007 seconds
935+
coerce_tests! :test_datetime_precision_is_truncated_on_assignment
936+
def test_datetime_precision_is_truncated_on_assignment_coerced
937+
@connection.create_table(:foos, force: true)
938+
@connection.add_column :foos, :created_at, :datetime, precision: 0
939+
@connection.add_column :foos, :updated_at, :datetime, precision: 6
940+
941+
time = ::Time.now.change(nsec: 123456789)
942+
foo = Foo.new(created_at: time, updated_at: time)
943+
944+
assert_equal 0, foo.created_at.nsec
945+
assert_equal 123457000, foo.updated_at.nsec
946+
947+
foo.save!
948+
foo.reload
949+
950+
assert_equal 0, foo.created_at.nsec
951+
assert_equal 123457000, foo.updated_at.nsec
952+
end
933953
end
934954

935955

936956

957+
class TimePrecisionTest < ActiveRecord::TestCase
958+
# datetime is rounded to increments of .000, .003, or .007 seconds
959+
coerce_tests! :test_time_precision_is_truncated_on_assignment
960+
def test_time_precision_is_truncated_on_assignment_coerced
961+
@connection.create_table(:foos, force: true)
962+
@connection.add_column :foos, :start, :time, precision: 0
963+
@connection.add_column :foos, :finish, :time, precision: 6
964+
965+
time = ::Time.now.change(nsec: 123456789)
966+
foo = Foo.new(start: time, finish: time)
967+
968+
assert_equal 0, foo.start.nsec
969+
assert_equal 123457000, foo.finish.nsec
970+
971+
foo.save!
972+
foo.reload
973+
974+
assert_equal 0, foo.start.nsec
975+
assert_equal 123457000, foo.finish.nsec
976+
end
977+
end
978+
979+
937980

938981
class DefaultNumbersTest < ActiveRecord::TestCase
939982
# We do better with native types and do not return strings for everything.

0 commit comments

Comments
 (0)