-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6666bb
commit fe7faba
Showing
8 changed files
with
199 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from datetime import date | ||
|
||
|
||
class DateRange: | ||
def __init__( | ||
self, | ||
start: date | None = None, | ||
end: date | None = None, | ||
): | ||
self.start = start | ||
self.end = end | ||
|
||
@staticmethod | ||
def from_str(string: str): | ||
try: | ||
start_date_str, end_date_str = string.split("..") | ||
start_date = ( | ||
date.fromisoformat(start_date_str) if start_date_str != "" else None | ||
) | ||
end_date = date.fromisoformat(end_date_str) if end_date_str != "" else None | ||
return DateRange(start_date, end_date) | ||
except ValueError: | ||
raise ValueError( | ||
"Invalid date range string format. Expected 'YYYY-MM-DD..YYYY-MM-DD'." | ||
) | ||
|
||
def __str__(self): | ||
start_str = self.start if self.start is not None else "" | ||
end_str = self.end if self.end is not None else "" | ||
return f"{start_str}..{end_str}" | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if isinstance(other, type(self)): | ||
return (self.start, self.end) == (other.start, other.end) | ||
|
||
return False | ||
|
||
|
||
def convert_date_range_to_str_for_search_query(date_range: DateRange) -> str | None: | ||
if date_range.start is not None and date_range.end is not None: | ||
return str(date_range) | ||
elif date_range.start is not None and date_range.end is None: | ||
return f">={str(date_range.start)}" | ||
elif date_range.start is None and date_range.end is not None: | ||
return f"<={str(date_range.end)}" | ||
else: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from datetime import date | ||
from depwatch.date_utils import DateRange, convert_date_range_to_str_for_search_query | ||
|
||
|
||
class TestDateUtils: | ||
def test_date_range_from_str(self): | ||
datasets = [ | ||
"2023-01-01..2023-03-31", | ||
"2023-01-01..", | ||
"..2023-03-31", | ||
] | ||
|
||
for string in datasets: | ||
date_range = DateRange.from_str(string) | ||
|
||
assert str(date_range) == string | ||
|
||
def test_date_range_from_str_when_invalid_string_is_provided(self): | ||
try: | ||
DateRange.from_str("2023-01-01") | ||
assert False | ||
except ValueError as e: | ||
assert ( | ||
str(e) | ||
== "Invalid date range string format. Expected 'YYYY-MM-DD..YYYY-MM-DD'." | ||
) | ||
|
||
def test_date_convert_date_range_to_str_for_search_query(self): | ||
datasets = [ | ||
[ | ||
date.fromisoformat("2023-01-01"), | ||
date.fromisoformat("2023-03-31"), | ||
"2023-01-01..2023-03-31", | ||
], | ||
[date.fromisoformat("2023-01-01"), None, ">=2023-01-01"], | ||
[None, date.fromisoformat("2023-03-31"), "<=2023-03-31"], | ||
[None, None, None], | ||
] | ||
|
||
for start, end, string in datasets: | ||
date_range = DateRange(start, end) | ||
|
||
assert convert_date_range_to_str_for_search_query(date_range) == string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters