Skip to content

Commit

Permalink
Add the reason the test timestamp is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
movermeyer committed Nov 28, 2022
1 parent be31bc5 commit 751b3bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions generate_test_timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,40 +228,40 @@ def generate_invalid_timestamp(year=2014, month=2, day=3, iso_week=6, iso_day=1,
str_value = str(__pad_params(**{field_name: kwargs[field_name]})[field_name])[0:length]
mangled_kwargs[field_name] = "{{:0>{length}}}".format(length=length).format(str_value)
timestamp = timestamp_format.format(**mangled_kwargs)
yield timestamp
yield (timestamp, "{0} has too few characters".format(field_name))

# Too many characters
if field.max_width is not None:
mangled_kwargs[field_name] = "{{:0>{length}}}".format(length=field.max_width + 1).format(kwargs[field_name])
timestamp = timestamp_format.format(**mangled_kwargs)
yield timestamp
yield (timestamp, "{0} has too many characters".format(field_name))

# Too small of value
if (field.min_value - 1) >= 0:
mangled_kwargs[field_name] = __pad_params(**{field_name: field.min_value - 1})[field_name]
timestamp = timestamp_format.format(**mangled_kwargs)
yield timestamp
yield (timestamp, "{0} has too small value".format(field_name))

# Too large of value
if field.max_value is not None:
mangled_kwargs[field_name] = __pad_params(**{field_name: field.max_value + 1})[field_name]
timestamp = timestamp_format.format(**mangled_kwargs)
yield timestamp
yield (timestamp, "{0} has too large value".format(field_name))

# Invalid characters
max_invalid_characters = field.max_width if field.max_width is not None else 1
# ex. 2014 -> a, aa, aaa
for length in range(1, max_invalid_characters):
mangled_kwargs[field_name] = "a" * length
timestamp = timestamp_format.format(**mangled_kwargs)
yield timestamp
yield (timestamp, "{0} has invalid characters".format(field_name))
# ex. 2014 -> aaaa, 2aaa, 20aa, 201a
for length in range(0, max_invalid_characters):
str_value = str(__pad_params(**{field_name: kwargs[field_name]})[field_name])[0:length]
mangled_kwargs[field_name] = "{{:a<{length}}}".format(length=max_invalid_characters).format(str_value)
timestamp = timestamp_format.format(**mangled_kwargs)
yield timestamp
yield (timestamp, "{0} has invalid characters".format(field_name))

# Trailing characters
timestamp = timestamp_format.format(**__pad_params(**kwargs)) + "EXTRA"
yield timestamp
yield (timestamp, "{0} has extra characters".format(field_name))
6 changes: 3 additions & 3 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ class InvalidTimestampTestCase(unittest.TestCase):
# See `generate_test_timestamps.generate_invalid_timestamp` for details.

def test_parse_auto_generated_invalid_formats(self):
for timestamp in generate_invalid_timestamp():
for timestamp, reason in generate_invalid_timestamp():
try:
with self.assertRaises(ValueError, msg="Timestamp '{0}' was supposed to be invalid, but parsing it didn't raise ValueError.".format(timestamp)):
with self.assertRaises(ValueError, msg="Timestamp '{0}' was supposed to be invalid ({1}), but parsing it didn't raise ValueError.".format(timestamp, reason)):
parse_datetime(timestamp)
except Exception as exc:
print("Timestamp '{0}' was supposed to raise ValueError, but raised {1} instead".format(timestamp, type(exc).__name__))
print("Timestamp '{0}' was supposed to raise ValueError ({1}), but raised {2} instead".format(timestamp, reason, type(exc).__name__))
raise

def test_non_ascii_characters(self):
Expand Down

0 comments on commit 751b3bf

Please sign in to comment.