Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds return value to tick() #542

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ def tick(self, delta=datetime.timedelta(seconds=1)):
self.move_to(self.time_to_freeze + datetime.timedelta(seconds=delta))
else:
self.move_to(self.time_to_freeze + delta)
return self.time_to_freeze

def move_to(self, target_datetime):
"""Moves frozen date to the given ``target_datetime``"""
Expand All @@ -523,6 +524,7 @@ def tick(self, delta=datetime.timedelta(seconds=1)):
self.time_to_freeze += datetime.timedelta(seconds=delta)
else:
self.time_to_freeze += delta
return self.time_to_freeze

def move_to(self, target_datetime):
"""Moves frozen date to the given ``target_datetime``"""
Expand All @@ -546,6 +548,7 @@ def tick(self, delta=None):
if not delta:
delta = datetime.timedelta(seconds=self.step_width)
self.time_to_freeze += delta
return self.time_to_freeze

def update_step_width(self, step_width):
self.step_width = step_width
Expand Down
5 changes: 3 additions & 2 deletions freezegun/api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ _Freezable: TypeAlias = str | datetime | date | timedelta
class TickingDateTimeFactory:
def __init__(self, time_to_freeze: datetime, start: datetime) -> None: ...
def __call__(self) -> datetime: ...
def tick(self, delta: float | Real | timedelta = ...) -> datetime: ...

class FrozenDateTimeFactory:
def __init__(self, time_to_freeze: datetime) -> None: ...
def __call__(self) -> datetime: ...
def tick(self, delta: float | Real | timedelta = ...) -> None: ...
def tick(self, delta: float | Real | timedelta = ...) -> datetime: ...
def move_to(self, target_datetime: _Freezable | None) -> None: ...

class StepTickTimeFactory:
def __init__(self, time_to_freeze: datetime, step_width: float) -> None: ...
def __call__(self) -> datetime: ...
def tick(self, delta: timedelta | None = ...) -> None: ...
def tick(self, delta: timedelta | None = ...) -> datetime: ...
def update_step_width(self, step_width: float) -> None: ...
def move_to(self, target_datetime: _Freezable | None) -> None: ...

Expand Down
31 changes: 10 additions & 21 deletions tests/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,33 +162,22 @@ def test_time_with_dst():


def test_manual_increment():
initial_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
with freeze_time(initial_datetime) as frozen_datetime:
assert frozen_datetime() == initial_datetime

frozen_datetime.tick()
initial_datetime += datetime.timedelta(seconds=1)
assert frozen_datetime() == initial_datetime

frozen_datetime.tick(delta=datetime.timedelta(seconds=10))
initial_datetime += datetime.timedelta(seconds=10)
assert frozen_datetime() == initial_datetime


def test_manual_increment_seconds():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that test_manual_increment_seconds and test_manual_increment were doing the same exact thing. So i deleted one of them.

initial_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
with freeze_time(initial_datetime) as frozen_datetime:
assert frozen_datetime() == initial_datetime

frozen_datetime.tick()
initial_datetime += datetime.timedelta(seconds=1)
assert frozen_datetime() == initial_datetime
expected = initial_datetime + datetime.timedelta(seconds=1)
assert frozen_datetime.tick() == expected
assert frozen_datetime() == expected

frozen_datetime.tick(10)
initial_datetime += datetime.timedelta(seconds=10)
assert frozen_datetime() == initial_datetime
expected = initial_datetime + datetime.timedelta(seconds=11)
assert frozen_datetime.tick(10) == expected
assert frozen_datetime() == expected

expected = initial_datetime + datetime.timedelta(seconds=21)
assert frozen_datetime.tick(delta=datetime.timedelta(seconds=10)) == expected
assert frozen_datetime() == expected


def test_move_to():
Expand Down
Loading