Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Fix output format of time fields #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tap_mysql/sync_strategies/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def row_to_singer_record(catalog_entry, version, row, columns, time_extracted):

elif isinstance(elem, datetime.timedelta):
if property_format == 'time':
row_to_persist += (str(elem),) # this should convert time column into 'HH:MM:SS' formatted string
_total_seconds = int(elem.total_seconds())
_hours, _remainder = divmod(_total_seconds, 3600)
_minutes, _seconds = divmod(_remainder, 60)
row_to_persist += (f"{_hours:02}:{_minutes:02}:{_seconds:02}",) # this should convert time column into 'HH:MM:SS' formatted string
else:
epoch = datetime.datetime.utcfromtimestamp(0)
timedelta_from_epoch = epoch + elem
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/sync_strategies/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from datetime import datetime, timedelta, timezone

from singer.catalog import CatalogEntry
from singer.schema import Schema

from tap_mysql.sync_strategies.common import row_to_singer_record


def test_row_to_singer_record():
catalog_entry = CatalogEntry(
stream='stream',
schema=Schema.from_dict({
'type': 'object',
'properties': {
'time': {
'type': 'string',
'format': 'time',
},
},
}),
)
message = row_to_singer_record(
catalog_entry,
version=1,
row=(timedelta(hours=8, minutes=30),),
columns=['time'],
time_extracted=datetime.now(timezone.utc),
)

assert message.stream == 'stream'
assert message.version == 1
assert message.record == {'time': '08:30:00'}
assert message.time_extracted is not None