Skip to content

Commit

Permalink
Fix parsing of dates not parsed by the YAML parser itself
Browse files Browse the repository at this point in the history
The documentation told users they could use such dates but the
implementation didn't allow that.
  • Loading branch information
liskin committed Jan 7, 2022
1 parent 1d6daee commit 838b44f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/strava_gear/input/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@

@config_format_checker.checks('datetime')
def format_checker_datetime(d):
return isinstance(d, (datetime.datetime, datetime.date,))
if isinstance(d, str):
try:
return parse_datetime(d) is not None
except Exception:
return False
else:
return isinstance(d, (datetime.datetime, datetime.date,))


def process_component(k: str, v) -> Component:
Expand Down
26 changes: 25 additions & 1 deletion tests/test_input_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def rd(yaml, **kwargs):


def test_minimal():
assert rd("rules: [{}]") == Rules(bike_names={}, components=[], rules=[Rule()])

assert rd(
"""
rules:
Expand Down Expand Up @@ -85,7 +87,14 @@ def test_undeclared_components():
)


def test_validation():
def test_validation_ok():
assert rd("rules:\n - since: 2021-01-01")
assert rd("rules:\n - since: 2016-05-23T19:30:00+02:00")
assert rd("rules:\n - since: 2020-05-01T14:00")
assert rd("rules:\n - since: 2020-05-01T14:00+02:00")


def test_validation_fails():
with pytest.raises(ValidationError) as e:
rd("rules: []")
assert "[] is too short" in str(e.value)
Expand All @@ -100,6 +109,21 @@ def test_validation():
rd("rules: [{b1: {r1: c1, r2: c1}}]")
assert "Duplicate components" in str(e.value)

with pytest.raises(ValidationError) as e:
rd("rules: [since:]")
assert "is not a 'datetime'" in str(e.value)
assert list(e.value.absolute_path) == ['rules', 0, 'since']

with pytest.raises(ValidationError) as e:
rd("rules: [since: 1]")
assert "is not a 'datetime'" in str(e.value)
assert list(e.value.absolute_path) == ['rules', 0, 'since']

with pytest.raises(ValidationError) as e:
rd("rules: [since: 2021-01-1]")
assert "is not a 'datetime'" in str(e.value)
assert list(e.value.absolute_path) == ['rules', 0, 'since']

# TODO: more tests for validation


Expand Down

0 comments on commit 838b44f

Please sign in to comment.