Skip to content

Commit

Permalink
Remove obsolete test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Sep 19, 2022
1 parent ddb23ce commit 2106d13
Show file tree
Hide file tree
Showing 34 changed files with 178 additions and 263 deletions.
11 changes: 1 addition & 10 deletions pendulum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@
from pendulum.formatting import Formatter
from pendulum.helpers import format_diff
from pendulum.helpers import get_locale
from pendulum.helpers import get_test_now
from pendulum.helpers import has_test_now
from pendulum.helpers import locale
from pendulum.helpers import set_locale
from pendulum.helpers import set_test_now
from pendulum.helpers import test
from pendulum.helpers import week_ends_at
from pendulum.helpers import week_starts_at
from pendulum.parser import parse
Expand Down Expand Up @@ -240,8 +236,7 @@ def from_format(
"""
Creates a DateTime instance from a specific format.
"""
parts = _formatter.parse(string, fmt, now(), locale=locale)

parts = _formatter.parse(string, fmt, now(tz=tz), locale=locale)
if parts["tz"] is None:
parts["tz"] = tz

Expand Down Expand Up @@ -338,17 +333,13 @@ def period(start: DateTime, end: DateTime, absolute: bool = False) -> Period:
"from_format",
"from_timestamp",
"get_locale",
"get_test_now",
"has_test_now",
"instance",
"local",
"locale",
"naive",
"now",
"period",
"set_locale",
"set_test_now",
"test",
"week_ends_at",
"week_starts_at",
"parse",
Expand Down
5 changes: 0 additions & 5 deletions pendulum/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
from pendulum.constants import YEARS_PER_DECADE
from pendulum.exceptions import PendulumException
from pendulum.helpers import add_duration
from pendulum.helpers import get_test_now
from pendulum.helpers import has_test_now
from pendulum.mixins.default import FormattableMixin
from pendulum.period import Period

Expand Down Expand Up @@ -733,9 +731,6 @@ def average(self, dt: date | None = None) -> Date:

@classmethod
def today(cls) -> Date:
if has_test_now():
return cast(pendulum.DateTime, get_test_now()).date()

dt = date.today()

return cls(dt.year, dt.month, dt.day)
Expand Down
11 changes: 0 additions & 11 deletions pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
from pendulum.date import Date
from pendulum.exceptions import PendulumException
from pendulum.helpers import add_duration
from pendulum.helpers import get_test_now
from pendulum.helpers import has_test_now
from pendulum.period import Period
from pendulum.time import Time
from pendulum.tz import UTC
Expand Down Expand Up @@ -135,15 +133,6 @@ def now(
"""
Get a DateTime instance for the current date and time.
"""
if has_test_now():
test_instance: DateTime = cast(DateTime, get_test_now())
_tz = pendulum._safe_timezone(tz)

if tz is not None and _tz != test_instance.timezone:
test_instance = test_instance.in_tz(_tz)

return test_instance

if tz is None or tz == "local":
dt = datetime.datetime.now(local_timezone())
elif tz is UTC or tz == "UTC":
Expand Down
27 changes: 0 additions & 27 deletions pendulum/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import os
import struct

from contextlib import contextmanager
from datetime import date
from datetime import datetime
from datetime import timedelta
from math import copysign
from typing import TYPE_CHECKING
from typing import Iterator
from typing import TypeVar
from typing import overload

Expand Down Expand Up @@ -178,27 +176,6 @@ def _sign(x: float) -> int:
# Global helpers


@contextmanager
def test(mock: pendulum.DateTime) -> Iterator[None]:
set_test_now(mock)
try:
yield
finally:
set_test_now()


def set_test_now(test_now: pendulum.DateTime | None = None) -> None:
pendulum._TEST_NOW = test_now


def get_test_now() -> pendulum.DateTime | None:
return pendulum._TEST_NOW


def has_test_now() -> bool:
return pendulum._TEST_NOW is not None


def locale(name: str) -> Locale:
return Locale.load(name)

Expand Down Expand Up @@ -238,10 +215,6 @@ def week_ends_at(wday: int) -> None:
"week_day",
"add_duration",
"format_diff",
"test",
"set_test_now",
"get_test_now",
"has_test_now",
"locale",
"set_locale",
"get_locale",
Expand Down
2 changes: 1 addition & 1 deletion pendulum/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

def parse(text: str, **options: t.Any) -> Date | Time | DateTime | Duration:
# Use the mock now value if it exists
options["now"] = options.get("now", pendulum.get_test_now())
options["now"] = options.get("now")

return _parse(text, **options)

Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def setup():

yield

pendulum.set_test_now()
pendulum.set_locale("en")
pendulum.set_local_timezone()
pendulum.week_starts_at(pendulum.MONDAY)
Expand Down
24 changes: 12 additions & 12 deletions tests/date/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ def test_diff_for_humans_now_and_nearly_month(today):


def test_diff_for_humans_now_and_month():
with pendulum.test(pendulum.datetime(2016, 4, 1)):
with pendulum.travel_to(pendulum.datetime(2016, 4, 1)):
today = pendulum.today().date()

assert today.subtract(weeks=4).diff_for_humans() == "4 weeks ago"
assert today.subtract(months=1).diff_for_humans() == "1 month ago"

with pendulum.test(pendulum.datetime(2017, 3, 1)):
with pendulum.travel_to(pendulum.datetime(2017, 3, 1)):
today = pendulum.today().date()

assert today.subtract(weeks=4).diff_for_humans() == "1 month ago"
Expand Down Expand Up @@ -182,23 +182,23 @@ def test_diff_for_humans_now_and_nearly_future_month(today):


def test_diff_for_humans_now_and_future_month():
with pendulum.test(pendulum.datetime(2016, 3, 1)):
with pendulum.travel_to(pendulum.datetime(2016, 3, 1)):
today = pendulum.today("UTC").date()

assert today.add(weeks=4).diff_for_humans() == "in 4 weeks"
assert today.add(months=1).diff_for_humans() == "in 1 month"

with pendulum.test(pendulum.datetime(2017, 3, 31)):
with pendulum.travel_to(pendulum.datetime(2017, 3, 31)):
today = pendulum.today("UTC").date()

assert today.add(months=1).diff_for_humans() == "in 1 month"

with pendulum.test(pendulum.datetime(2017, 4, 30)):
with pendulum.travel_to(pendulum.datetime(2017, 4, 30)):
today = pendulum.today("UTC").date()

assert today.add(months=1).diff_for_humans() == "in 1 month"

with pendulum.test(pendulum.datetime(2017, 1, 31)):
with pendulum.travel_to(pendulum.datetime(2017, 1, 31)):
today = pendulum.today("UTC").date()

assert today.add(weeks=4).diff_for_humans() == "in 1 month"
Expand Down Expand Up @@ -245,23 +245,23 @@ def test_diff_for_humans_other_and_nearly_month(today):


def test_diff_for_humans_other_and_month():
with pendulum.test(pendulum.datetime(2016, 3, 1)):
with pendulum.travel_to(pendulum.datetime(2016, 3, 1)):
today = pendulum.today().date()

assert today.diff_for_humans(today.add(weeks=4)) == "4 weeks before"
assert today.diff_for_humans(today.add(months=1)) == "1 month before"

with pendulum.test(pendulum.datetime(2017, 3, 31)):
with pendulum.travel_to(pendulum.datetime(2017, 3, 31)):
today = pendulum.today().date()

assert today.diff_for_humans(today.add(months=1)) == "1 month before"

with pendulum.test(pendulum.datetime(2017, 4, 30)):
with pendulum.travel_to(pendulum.datetime(2017, 4, 30)):
today = pendulum.today().date()

assert today.diff_for_humans(today.add(months=1)) == "1 month before"

with pendulum.test(pendulum.datetime(2017, 1, 31)):
with pendulum.travel_to(pendulum.datetime(2017, 1, 31)):
today = pendulum.today().date()

assert today.diff_for_humans(today.add(weeks=4)) == "1 month before"
Expand Down Expand Up @@ -308,13 +308,13 @@ def test_diff_for_humans_other_and_nearly_future_month(today):


def test_diff_for_humans_other_and_future_month():
with pendulum.test(pendulum.datetime(2016, 3, 1)):
with pendulum.travel_to(pendulum.datetime(2016, 3, 1)):
today = pendulum.today().date()

assert today.diff_for_humans(today.subtract(weeks=4)) == "4 weeks after"
assert today.diff_for_humans(today.subtract(months=1)) == "1 month after"

with pendulum.test(pendulum.datetime(2017, 2, 28)):
with pendulum.travel_to(pendulum.datetime(2017, 2, 28)):
today = pendulum.today().date()

assert today.diff_for_humans(today.subtract(weeks=4)) == "1 month after"
Expand Down
4 changes: 2 additions & 2 deletions tests/datetime/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def test_less_than_or_equal_with_timezone_false():


def test_is_anniversary():
with pendulum.test(pendulum.now()):
with pendulum.travel_to(pendulum.now()):
d = pendulum.now()
an_anniversary = d.subtract(years=1)
assert an_anniversary.is_anniversary()
Expand All @@ -258,7 +258,7 @@ def test_is_anniversary():


def test_is_birthday(): # backward compatibility
with pendulum.test(pendulum.now()):
with pendulum.travel_to(pendulum.now()):
d = pendulum.now()
an_anniversary = d.subtract(years=1)
assert an_anniversary.is_birthday()
Expand Down
Loading

0 comments on commit 2106d13

Please sign in to comment.