Skip to content

Commit

Permalink
Merge pull request #542 from kingbuzzman/patch-1
Browse files Browse the repository at this point in the history
Adds return value to tick()
  • Loading branch information
bblommers authored Apr 22, 2024
2 parents 4ecad0c + 2436841 commit dfd5d9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
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():
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

0 comments on commit dfd5d9e

Please sign in to comment.