From 838b44f29975fc3c591a6d45637697e4cc6a194a Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Fri, 7 Jan 2022 12:06:54 +0000 Subject: [PATCH] Fix parsing of dates not parsed by the YAML parser itself The documentation told users they could use such dates but the implementation didn't allow that. --- src/strava_gear/input/rules.py | 8 +++++++- tests/test_input_rules.py | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/strava_gear/input/rules.py b/src/strava_gear/input/rules.py index c8b12c5..90c94fe 100644 --- a/src/strava_gear/input/rules.py +++ b/src/strava_gear/input/rules.py @@ -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: diff --git a/tests/test_input_rules.py b/tests/test_input_rules.py index 160aeb8..3a8f71f 100644 --- a/tests/test_input_rules.py +++ b/tests/test_input_rules.py @@ -16,6 +16,8 @@ def rd(yaml, **kwargs): def test_minimal(): + assert rd("rules: [{}]") == Rules(bike_names={}, components=[], rules=[Rule()]) + assert rd( """ rules: @@ -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) @@ -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