From ad4cd99983533c39fb5ab32d42a4cc270b7613a5 Mon Sep 17 00:00:00 2001 From: Aaron Stone Date: Tue, 14 Nov 2017 00:51:40 -0800 Subject: [PATCH] Test five significant figures of microseconds to reduce off-by-one-microsecond errors --- spec/mysql2/result_spec.rb | 24 ++++++++++++++++++++++++ spec/mysql2/statement_spec.rb | 6 ++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spec/mysql2/result_spec.rb b/spec/mysql2/result_spec.rb index 35da16b91..c8e26c530 100644 --- a/spec/mysql2/result_spec.rb +++ b/spec/mysql2/result_spec.rb @@ -292,6 +292,30 @@ expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00') end + it "should return Time values with microseconds" do + now = Time.now + if RUBY_VERSION =~ /1.8/ || @client.server_info[:id] / 100 < 506 + result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a") + expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z')) + else + result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a") + # microseconds is 6 digits after the decimal, but only test on 5 significant figures + expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z')) + end + end + + it "should return DateTime values with microseconds" do + now = DateTime.now + if RUBY_VERSION =~ /1.8/ || @client.server_info[:id] / 100 < 506 + result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a") + expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z')) + else + result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a") + # microseconds is 6 digits after the decimal, but only test on 5 significant figures + expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z')) + end + end + if 1.size == 4 # 32bit klass = if RUBY_VERSION =~ /1.8/ DateTime diff --git a/spec/mysql2/statement_spec.rb b/spec/mysql2/statement_spec.rb index c0a16af47..e0fccad53 100644 --- a/spec/mysql2/statement_spec.rb +++ b/spec/mysql2/statement_spec.rb @@ -150,7 +150,8 @@ def stmt_count if RUBY_VERSION =~ /1.8/ expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z')) else - expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z')) + # microseconds is six digits after the decimal, but only test on 5 significant figures + expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z')) end end @@ -161,7 +162,8 @@ def stmt_count if RUBY_VERSION =~ /1.8/ expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z')) else - expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z')) + # microseconds is six digits after the decimal, but only test on 5 significant figures + expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z')) end end