Skip to content

Commit

Permalink
Fix PeriodFactory bug & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitarOnGithub committed Aug 1, 2024
1 parent 5b68292 commit a495cd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
15 changes: 6 additions & 9 deletions temporals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ class PeriodFactory:
def __new__(cls,
start,
end,
force_datetime: bool = False,
**kwargs) -> TimePeriod | DatePeriod | DatetimePeriod:
if isinstance(start, str):
start = get_datetime(start, force_datetime)
if isinstance(end, str):
end = get_datetime(end, force_datetime)
force_datetime: bool = False) -> TimePeriod | DatePeriod | DatetimePeriod:
start = get_datetime(start, force_datetime)
end = get_datetime(end, force_datetime)
if type(start) is time and type(end) is time:
return TimePeriod(start, end, **kwargs)
return TimePeriod(start, end)
if type(start) is date and type(end) is date:
return DatePeriod(start, end, **kwargs)
return DatePeriod(start, end)
if type(start) is datetime and type(end) is datetime:
return DatetimePeriod(start, end, **kwargs)
return DatetimePeriod(start, end)
raise ValueError(f"Could not find suitable period type for the provided values")
16 changes: 16 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from datetime import time, date
from temporals import PeriodFactory
from temporals import TimePeriod, DatePeriod, DatetimePeriod

Expand Down Expand Up @@ -26,3 +27,18 @@ def test_datetime_period_creation(self):
a = self.factory("2024-11-01 13:00", "2024-11-01 15:00")
assert isinstance(a, DatetimePeriod)

def test_force_creation_time(self):
self.factory = PeriodFactory
a = self.factory(
time(10, 0),
time(12, 0),
force_datetime=True)
assert isinstance(a, DatetimePeriod)

def test_force_creation_date(self):
self.factory = PeriodFactory
a = self.factory(
date(2024, 1, 1),
date(2024, 1, 10),
force_datetime=True)
assert isinstance(a, DatetimePeriod)

0 comments on commit a495cd9

Please sign in to comment.