Skip to content

Commit

Permalink
fix: add time type to timestamp_converter
Browse files Browse the repository at this point in the history
  • Loading branch information
p-eye committed Jul 27, 2024
1 parent 8fd7b4e commit 8e7df4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lib/embulk/output/bigquery/value_converter_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ def timestamp_converter
next nil if val.nil?
val.localtime(zone_offset).strftime("%Y-%m-%d %H:%M:%S.%6N")
}
when 'TIME'
Proc.new {|val|
next nil if val.nil?
val.localtime(zone_offset).strftime("%H:%M:%S.%6N")
}
else
raise NotSupportedType, "cannot take column type #{type} for timestamp column"
end
Expand Down
30 changes: 24 additions & 6 deletions test/test_value_converter_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,16 @@ def test_datetime
end

def test_time
converter = ValueConverterFactory.new(
SCHEMA_TYPE, 'TIME',
timestamp_format: '%H:%M:%S'
).create_converter
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'TIME').create_converter
assert_equal nil, converter.call(nil)
assert_equal "00:03:22.000000", converter.call("00:03:22")
assert_equal "15:22:00.000000", converter.call("3:22 PM")
assert_equal "03:22:00.000000", converter.call("3:22 AM")
assert_equal "15:22:00.000000", converter.call("15:22")
assert_equal "10:00:00.000000", converter.call("2024-07-24 10:00")

# Users must care of BQ datetime format by themselves with no timestamp_format
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'TIME').create_converter
assert_equal nil, converter.call(nil)
assert_equal "00:00:00.000000", converter.call("2016-02-26 00:00:00")
end

def test_record
Expand Down Expand Up @@ -363,6 +363,24 @@ def test_datetime
assert_raise { converter.call('foo') }
end

def test_time
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'TIME').create_converter
assert_equal nil, converter.call(nil)
timestamp = Time.parse("2016-02-26 00:00:00.500000 +00:00")
expected = "00:00:00.500000"
assert_equal expected, converter.call(timestamp)

converter = ValueConverterFactory.new(
SCHEMA_TYPE, 'TIME', timezone: 'Asia/Tokyo'
).create_converter
assert_equal nil, converter.call(nil)
timestamp = Time.parse("2016-02-25 15:00:00.500000 +00:00")
expected = "00:00:00.500000"
assert_equal expected, converter.call(timestamp)

assert_raise { converter.call('foo') }
end

def test_record
assert_raise { ValueConverterFactory.new(SCHEMA_TYPE, 'RECORD').create_converter }
end
Expand Down

0 comments on commit 8e7df4a

Please sign in to comment.