aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorChris Hapgood <cch1@hapgoods.com>2010-01-15 18:34:49 -0500
committerMichael Koziarski <michael@koziarski.com>2010-01-16 15:21:36 +1300
commit717a2941e15b32d07cc456bb0d81742ecfc5b4a3 (patch)
tree63ec271188b01be0cb5ab08b7dc156ef4a400ef8 /activerecord/test/cases
parent81cd11259c52544dd1bc401b7097e4a0e5d34fe6 (diff)
downloadrails-717a2941e15b32d07cc456bb0d81742ecfc5b4a3.tar.gz
rails-717a2941e15b32d07cc456bb0d81742ecfc5b4a3.tar.bz2
rails-717a2941e15b32d07cc456bb0d81742ecfc5b4a3.zip
Fix #microseconds conversion and #fast_string_to_time
* Use direct integer parsing in #fast_string_to_time to avoid convoluted conversions and errors due to truncation. * Use Float#round in #microseconds to avoid truncation errors. Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/schema_definitions_test.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/activerecord/test/cases/schema_definitions_test.rb b/activerecord/test/cases/schema_definitions_test.rb
new file mode 100644
index 0000000000..d90a95cf58
--- /dev/null
+++ b/activerecord/test/cases/schema_definitions_test.rb
@@ -0,0 +1,43 @@
+require "cases/helper"
+
+class SchemaDefinitionsTest < ActiveRecord::TestCase
+
+ REGRESSION_SAMPLES = %w{000249 125014 003912 256051 524287}
+
+ test 'fast_string_to_time converts properly' do
+ converted = ActiveRecord::ConnectionAdapters::Column.send('fast_string_to_time', "2010-01-12 12:34:56.000249")
+ assert_equal Time.mktime(2010, 01, 12, 12, 34, 56, 249), converted
+ end
+
+ test 'fallback_string_to_time converts properly' do
+ converted = ActiveRecord::ConnectionAdapters::Column.send('fallback_string_to_time', "2010-01-12 12:34:56.000249")
+ assert_equal Time.mktime(2010, 01, 12, 12, 34, 56, 249), converted
+ end
+
+ test 'fallback_string_to_time converts properly with no microseconds' do
+ converted = ActiveRecord::ConnectionAdapters::Column.send('fallback_string_to_time', "2010-01-12 12:34:56")
+ assert_equal Time.mktime(2010, 01, 12, 12, 34, 56, 0), converted
+ end
+
+ test "fast_string_to_time can handle problematic microseconds" do
+ REGRESSION_SAMPLES.each do |u|
+ converted = ActiveRecord::ConnectionAdapters::Column.send('fast_string_to_time', "2010-01-12 12:34:56.#{u}")
+ assert_equal u.to_i, converted.usec
+ end
+ end
+
+ test "microseconds can handle problematic microseconds" do
+ REGRESSION_SAMPLES.each do |u|
+ i = u.to_i
+ converted = ActiveRecord::ConnectionAdapters::Column.send('microseconds', {:sec_fraction => Rational(i, 1_000_000)})
+ assert_equal i, converted
+
+ converted = ActiveRecord::ConnectionAdapters::Column.send('microseconds', {:sec_fraction => Rational(i, 1_000_000)})
+ assert_equal i, converted
+ end
+ end
+
+ test 'fast constant is equally restrictive' do
+ assert_match ActiveRecord::ConnectionAdapters::Column::Format::NEW_ISO_DATETIME, "2010-01-12 12:34:56.555493"
+ end
+end