From 170c4f912e6d8f93a2b9dfa1cb7d3998a07069c1 Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Wed, 28 Feb 2024 21:34:28 +0100 Subject: [PATCH] data_models: allow future dates (#25) --- rendercv/data_models.py | 41 ++++++++++++++------------------------- tests/test_data_models.py | 7 ++----- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/rendercv/data_models.py b/rendercv/data_models.py index fa7b0e9..2d72506 100644 --- a/rendercv/data_models.py +++ b/rendercv/data_models.py @@ -208,22 +208,15 @@ class EntryBase(RenderCVBaseModel): end_date_is_provided = True if date_is_provided: - try: - date_object = get_date_object(model.date) # type: ignore - except ValueError: - # Then it is a custom date string (e.g., "My Custom Date") - pass - else: - today_object = Date.today() - if date_object > today_object: - raise ValueError( - '"date" cannot be in the future!', - "date", # this is the location of the error - model.date, # this is value of the error - ) + model.start_date = None + model.end_date = None - elif start_date_is_provided and not end_date_is_provided: - model.end_date = "present" + if re.fullmatch(date_pattern_for_json_schema, model.date): + # Then it is in YYYY-MM-DD, YYYY-MM, or YYYY format + try: + get_date_object(model.date) + except ValueError as e: + raise ValueError(str(e), "date", str(model.date)) elif not start_date_is_provided and end_date_is_provided: raise ValueError( @@ -232,8 +225,11 @@ class EntryBase(RenderCVBaseModel): "start_date", # this is the location of the error "", # this supposed to be the value of the error ) + elif start_date_is_provided: + if not end_date_is_provided: + model.end_date = "present" - if model.start_date is not None and model.end_date is not None: + # Check if start_date and end_date are provided correctly: try: end_date = get_date_object(model.end_date) except ValueError as e: @@ -250,12 +246,6 @@ class EntryBase(RenderCVBaseModel): "start_date", # this is the location of the error str(model.start_date), # this is value of the error ) - elif end_date > Date.today(): - raise ValueError( - '"end_date" cannot be in the future!', - "end_date", # this is the location of the error - str(model.end_date), # this is value of the error - ) return model @@ -515,10 +505,9 @@ class PublicationEntry(RenderCVBaseModel): @pydantic.field_validator("date") @classmethod def check_date(cls, date: int | RenderCVDate) -> int | RenderCVDate: - """Check if the date is in the past.""" - date_object = get_date_object(date) - if date_object > Date.today(): - raise ValueError("The publication date cannot be in the future!") + """Check if the date is a valid date.""" + # The function below will raise an error if the date is not valid: + get_date_object(date) return date diff --git a/tests/test_data_models.py b/tests/test_data_models.py index c412f0a..f276069 100644 --- a/tests/test_data_models.py +++ b/tests/test_data_models.py @@ -265,7 +265,7 @@ def test_publication_dates(publication_entry, date, expected_date_string): assert publication_entry.date_string == expected_date_string -@pytest.mark.parametrize("date", ["aaa", None, "2025"]) +@pytest.mark.parametrize("date", ["aaa", None, "2025-23-23"]) def test_invalid_publication_dates(publication_entry, date): with pytest.raises(pydantic.ValidationError): publication_entry["date"] = date @@ -279,16 +279,13 @@ def test_invalid_publication_dates(publication_entry, date): ("2020-01-01", "aaa", None), (None, "2020-01-01", None), ("2023-01-01", "2021-01-01", None), - ("2999-01-01", None, None), - ("2020-01-01", "2999-01-01", None), ("2022", "2021", None), - ("2021", "2060", None), ("2025", "2021", None), - (None, None, "2028"), ("2020-01-01", "invalid_end_date", None), ("invalid_start_date", "2021-01-01", None), ("2020-99-99", "2021-01-01", None), ("2020-10-12", "2020-99-99", None), + (None, None, "2020-20-20") ], ) def test_invalid_dates(start_date, end_date, date):