-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve date/time hint handling for Redshift based on better testing #149
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f68b2c
Improve date/time hint handling for Redshift based on better testing
vinceatbluelabs 7bcc5d8
Improve date/time hint handling for Redshift based on better testing
vinceatbluelabs ecf509a
Ratchet coverage
vinceatbluelabs bf9e462
Remove original
vinceatbluelabs aa914b8
Merge branch 'master' into loosen_date_requirements_5
vinceatbluelabs 4b58105
Ratchet
vinceatbluelabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
93.3500 | ||
93.4000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import unittest | ||
from records_mover.db.redshift.records_copy import redshift_copy_options | ||
from records_mover.records import DelimitedRecordsFormat, AvroRecordsFormat | ||
from sqlalchemy_redshift.commands import Encoding, Format | ||
from ...records.datetime_cases import ( | ||
DATE_CASES, DATETIMETZ_CASES, DATETIME_CASES, TIMEONLY_CASES | ||
) | ||
|
||
|
||
class TestRecordsCopy(unittest.TestCase): | ||
def test_redshift_copy_options_encodings(self): | ||
tests = { | ||
'UTF16': Encoding.utf16, | ||
'UTF16LE': Encoding.utf16le, | ||
'UTF16BE': Encoding.utf16be | ||
} | ||
for hint_spelling, redshift_sqlalchemy_spelling in tests.items(): | ||
|
||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints={ | ||
'encoding': hint_spelling | ||
}) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
out = redshift_copy_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True, | ||
fail_if_row_invalid=True, | ||
max_failure_rows=0) | ||
self.assertIs(out['encoding'], redshift_sqlalchemy_spelling) | ||
|
||
def test_redshift_copy_options_avro(self): | ||
records_format = AvroRecordsFormat() | ||
unhandled_hints = set() | ||
out = redshift_copy_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True, | ||
fail_if_row_invalid=True, | ||
max_failure_rows=0) | ||
self.assertIs(out['format'], Format.avro) | ||
|
||
def test_redshift_copy_options_dateformat(self): | ||
# The records spec's date/time formats are based on Redshift's | ||
# spec originally, so it's expected that everything here would | ||
# be accepted as-is, but please double-check with Redshift's | ||
# docs as new test cases are added | ||
accept_as_is = { | ||
'YYYY-MM-DD': True, | ||
'MM-DD-YYYY': True, | ||
'DD-MM-YYYY': True, | ||
'MM/DD/YY': True, | ||
'DD/MM/YY': True, | ||
'DD-MM-YY': True, | ||
} | ||
for dateformat in DATE_CASES: | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints={ | ||
'dateformat': dateformat | ||
}) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
out = redshift_copy_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True, | ||
fail_if_row_invalid=True, | ||
max_failure_rows=0) | ||
if accept_as_is[dateformat]: | ||
self.assertIs(out['date_format'], dateformat) | ||
else: | ||
self.fail('define what to expect here') | ||
|
||
def test_redshift_copy_options_datetimeformattz(self): | ||
# Redshift's time_format doesn't support separate | ||
# configuration for datetimeformat vs datetimeformattz, but | ||
# the 'auto' flag seems to work with specific things (see | ||
# tests run in records_copy.py). | ||
# | ||
# Please verify new formats have a test run | ||
# and documented in records_copy.py before putting an entry in | ||
# here. | ||
expectations = { | ||
'YYYY-MM-DD HH:MI:SSOF': 'auto', | ||
'YYYY-MM-DD HH:MI:SS': 'YYYY-MM-DD HH:MI:SS', | ||
'YYYY-MM-DD HH24:MI:SSOF': 'auto', | ||
'MM/DD/YY HH24:MIOF': 'auto', | ||
'MM/DD/YY HH24:MI': 'MM/DD/YY HH24:MI', | ||
} | ||
for datetimeformattz in DATETIMETZ_CASES: | ||
hints = { | ||
'datetimeformattz': datetimeformattz, | ||
'datetimeformat': datetimeformattz.replace('OF', '') | ||
} | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints=hints) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
out = redshift_copy_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True, | ||
fail_if_row_invalid=True, | ||
max_failure_rows=0) | ||
self.assertEquals(out['time_format'], expectations[datetimeformattz]) | ||
|
||
def test_redshift_copy_options_datetimeformat(self): | ||
# Redshift's time_format doesn't support separate | ||
# configuration for datetimeformat vs datetimeformattz, but | ||
# the 'auto' flag seems to work with specific things (see | ||
# tests run in records_copy.py). | ||
# | ||
# Please verify new formats have a test run | ||
# and documented in records_copy.py before putting an entry in | ||
# here. | ||
for datetimeformat in DATETIME_CASES: | ||
hints = { | ||
'datetimeformattz': f"{datetimeformat}OF", | ||
'datetimeformat': datetimeformat, | ||
} | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints=hints) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
out = redshift_copy_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True, | ||
fail_if_row_invalid=True, | ||
max_failure_rows=0) | ||
self.assertEqual(out['time_format'], 'auto') | ||
for datetimeformat in DATETIME_CASES: | ||
hints = { | ||
'datetimeformattz': datetimeformat, | ||
'datetimeformat': datetimeformat, | ||
} | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints=hints) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
out = redshift_copy_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True, | ||
fail_if_row_invalid=True, | ||
max_failure_rows=0) | ||
self.assertEqual(out['time_format'], datetimeformat) | ||
|
||
def test_redshift_copy_options_timeonlyformat(self): | ||
# Redshift didn't used to have a time only format, so Records | ||
# Mover doesn't yet support anything here and just treats | ||
# these as strings. Nothing should raise. | ||
# | ||
# https://github.com/bluelabsio/records-mover/issues/141 | ||
for timeonlyformat in TIMEONLY_CASES: | ||
hints = { | ||
'timeonlyformat': timeonlyformat, | ||
} | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints=hints) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
redshift_copy_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True, | ||
fail_if_row_invalid=True, | ||
max_failure_rows=0) | ||
# hurray, we didn't raise! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import unittest | ||
from typing import Set | ||
from records_mover.db.redshift.records_unload import redshift_unload_options | ||
from records_mover.records import DelimitedRecordsFormat | ||
from ...records.datetime_cases import ( | ||
DATE_CASES, DATETIMETZ_CASES, DATETIME_CASES, TIMEONLY_CASES | ||
) | ||
|
||
|
||
class TestRecordsUnload(unittest.TestCase): | ||
def test_redshift_unload_options_datetimeformattz(self): | ||
# Redshift offers no options and only unloads YYYY-MM-DD | ||
# HH:MI:SSOF, so we should reject everything else. Double | ||
# check with the docs just in case, though--maybe that's | ||
# changed! | ||
expected_failures = { | ||
'YYYY-MM-DD HH:MI:SS', | ||
'MM/DD/YY HH24:MI', | ||
} | ||
for datetimeformattz in DATETIMETZ_CASES: | ||
hints = { | ||
'datetimeformattz': datetimeformattz, | ||
} | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints=hints) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
try: | ||
redshift_unload_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True) | ||
except NotImplementedError: | ||
if datetimeformattz in expected_failures: | ||
continue | ||
else: | ||
raise | ||
self.assertNotIn(datetimeformattz, expected_failures) | ||
|
||
def test_redshift_unload_options_datetimeformat(self): | ||
# Redshift offers no options and only unloads YYYY-MM-DD | ||
# HH:MI:SS, so we should reject everything else. Double | ||
# check with the docs just in case, though--maybe that's | ||
# changed! | ||
expected_failures = { | ||
'YYYY-MM-DD HH12:MI AM', | ||
'MM/DD/YY HH24:MI', | ||
} | ||
for datetimeformat in DATETIME_CASES: | ||
hints = { | ||
'datetimeformat': datetimeformat, | ||
} | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints=hints) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
try: | ||
redshift_unload_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True) | ||
except NotImplementedError: | ||
if datetimeformat in expected_failures: | ||
continue | ||
else: | ||
raise | ||
self.assertNotIn(datetimeformat, expected_failures) | ||
|
||
def test_redshift_unload_options_timeonlyformat(self): | ||
# Redshift didn't used to have a time only format, so Records | ||
# Mover doesn't yet support anything here and just treats | ||
# these as strings. Nothing should raise. | ||
expected_failures: Set[str] = set() | ||
for timeonlyformat in TIMEONLY_CASES: | ||
hints = { | ||
'timeonlyformat': timeonlyformat, | ||
} | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints=hints) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
try: | ||
redshift_unload_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True) | ||
except NotImplementedError: | ||
if timeonlyformat in expected_failures: | ||
continue | ||
else: | ||
raise | ||
self.assertNotIn(timeonlyformat, expected_failures) | ||
|
||
def test_redshift_unload_options_dateformat(self): | ||
# Redshift offers no options and only unloads YYYY-MM-DD, so | ||
# we should reject everything else. Double check with the | ||
# docs just in case, though--maybe that's changed! | ||
expected_failures = { | ||
'MM-DD-YYYY', | ||
'DD-MM-YYYY', | ||
'MM/DD/YY', | ||
'DD/MM/YY', | ||
'DD-MM-YY', | ||
} | ||
for dateformat in DATE_CASES: | ||
hints = { | ||
'dateformat': dateformat, | ||
} | ||
records_format =\ | ||
DelimitedRecordsFormat(variant='bluelabs', | ||
hints=hints) | ||
unhandled_hints = set(records_format.hints.keys()) | ||
try: | ||
redshift_unload_options(unhandled_hints, | ||
records_format, | ||
fail_if_cant_handle_hint=True) | ||
except NotImplementedError: | ||
if dateformat in expected_failures: | ||
continue | ||
else: | ||
raise | ||
self.assertNotIn(dateformat, expected_failures) |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above was hanging out in tests/unit; I promoted it to tests/component and added the code below.