Skip to content

Commit

Permalink
Date-ranges-test-fixes (#3307)
Browse files Browse the repository at this point in the history
* fix tests

* remove unused case

* Add a test failure description

---------

Co-authored-by: Cornelius Roemer <[email protected]>
  • Loading branch information
anna-parker and corneliusroemer committed Dec 2, 2024
1 parent 2605e8a commit eaf8c1c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ def parse_date_into_range(
errors=[],
)

logger.debug(f"release_date: {release_date}")

formats_to_messages = {
"%Y-%m-%d": None,
"%Y-%m": "Day is missing. Assuming date is some time in the month.",
Expand Down Expand Up @@ -277,12 +275,6 @@ class DateRange:
date_range_lower=parsed_date.replace(month=1, day=1),
date_range_upper=parsed_date.replace(month=12, day=31),
)
case "_":
datum = DateRange(
date_range_string=None,
date_range_lower=None,
date_range_upper=max_upper_limit,
)

logger.debug(f"parsed_date: {datum}")

Expand Down
88 changes: 67 additions & 21 deletions preprocessing/nextclade/tests/test_processing_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,27 +483,73 @@ def test_format_authors() -> None:
raise AssertionError(msg)


def test_parse_date_into_range():
assert ProcessingFunctions.parse_date_into_range(
{"date": "2021-12"}, "field_name", {"fieldType": "dateRangeString"}
), "2021-12"
assert ProcessingFunctions.parse_date_into_range(
{"date": "2021-12"}, "field_name", {"fieldType": "dateRangeLower"}
), "2021-12-01"
assert ProcessingFunctions.parse_date_into_range(
{"date": "2021-12"}, "field_name", {"fieldType": "dateRangeUpper"}
), "2021-12-31"
assert ProcessingFunctions.parse_date_into_range(
{"date": "2021-02"}, "field_name", {"fieldType": "dateRangeUpper"}
), "2021-02-28"
assert ProcessingFunctions.parse_date_into_range(
{"date": "2021"}, "field_name", {"fieldType": "dateRangeUpper"}
), "2021-12-31"
assert ProcessingFunctions.parse_date_into_range(
{"date": "2021-12", "releaseDate": "2021-12-15"},
"field_name",
{"fieldType": "dateRangeUpper"},
), "2021-12-15"
def test_parse_date_into_range() -> None:
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "2021-12"}, "field_name", {"fieldType": "dateRangeString"}
).datum
== "2021-12"
), "dateRangeString: 2021-12 should be returned as is."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "2021-12"}, "field_name", {"fieldType": "dateRangeLower"}
).datum
== "2021-12-01"
), "dateRangeLower: 2021-12 should be returned as 2021-12-01."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "2021-12"}, "field_name", {"fieldType": "dateRangeUpper"}
).datum
== "2021-12-31"
), "dateRangeUpper: 2021-12 should be returned as 2021-12-31."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "2021-02"}, "field_name", {"fieldType": "dateRangeUpper"}
).datum
== "2021-02-28"
), "dateRangeUpper: 2021-02 should be returned as 2021-02-28."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "2021"}, "field_name", {"fieldType": "dateRangeUpper"}
).datum
== "2021-12-31"
), "dateRangeUpper: 2021 should be returned as 2021-12-31."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "2021-12", "releaseDate": "2021-12-15"},
"field_name",
{"fieldType": "dateRangeUpper"},
).datum
== "2021-12-15"
), "dateRangeUpper: 2021-12 with releaseDate 2021-12-15 should be returned as 2021-12-15."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "", "releaseDate": "2021-12-15"},
"field_name",
{"fieldType": "dateRangeUpper"},
).datum
== "2021-12-15"
), "dateRangeUpper: empty date with releaseDate 2021-12-15 should be returned as 2021-12-15."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": ""}, "field_name", {"fieldType": "dateRangeString"}
).datum
is None
), "dateRangeString: empty date should be returned as None."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "not.date"}, "field_name", {"fieldType": "dateRangeString"}
).datum
is None
), "dateRangeString: invalid date should be returned as None."
assert (
ProcessingFunctions.parse_date_into_range(
{"date": "", "releaseDate": "2021-12-15"},
"field_name",
{"fieldType": "dateRangeLower"},
).datum
is None
), "dateRangeLower: empty date should be returned as None."


if __name__ == "__main__":
Expand Down

0 comments on commit eaf8c1c

Please sign in to comment.