10000 Coding fixes and CR on tests for #4194 · gtkacz/python-telegram-bot@93b7fac · GitHub
[go: up one dir, main page]

Skip to content

Commit 93b7fac

Browse files
committed
Coding fixes and CR on tests for python-telegram-bot#4194
1 parent 103a13d commit 93b7fac

File tree

2 files changed

+68
-50
lines changed

2 files changed

+68
-50
lines changed

telegram/_business.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
# along with this program. If not, see [http://www.gnu.org/licenses/]
2020
"""This module contains the Telegram Business related classes."""
2121

22-
from datetime import date, datetime, time
23-
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Union
24-
2522
import zoneinfo
23+
from datetime import date, datetime, time
24+
from typing import TYPE_CHECKING, Optional, Sequence, Tuple
2625

2726
from telegram._chat import Chat
2827
from telegram._files.location import Location
@@ -466,7 +465,7 @@ def get_opening_hours_for_day(
466465
467466
Args:
468467
target_date (:class:`datetime.date`): The date for which to get the opening hours.
469-
tzinfo (:class:`zoneinfo.ZoneInfo`, optional): The timezone to use for the opening hours. If :obj:`None`, the time zone of the business is used, i.e. :attr:`time_zone_name`. Defaults to :obj:`None`.
468+
tzinfo (:class:`zoneinfo.ZoneInfo`, optional): The timezone to use for the opening hours. If :obj:`None`, the time zone of the business is used, i.e. :attr:`time_zone_name`. Defaults to :obj:`None`.
470469
471470
Returns:
472471
Tuple[Tuple[:class:`datetime.datetime`, :class:`datetime.datetime`], ...]: A tuple of tuples containing the opening and closing times for the day.
@@ -503,20 +502,16 @@ def time_to_minutes(weekday: int, hour: int, minute: int) -> int:
503502
if target_datetime.tzinfo is not None:
504503
target_datetime = target_datetime.astimezone(zoneinfo.ZoneInfo(self.time_zone_name))
505504

506-
return any(
507-
time_to_minutes(
508-
target_datetime.weekday(), target_datetime.hour, target_datetime.minute
509-
)
510-
>= time_to_minutes(*interval.opening_time)
511-
or time_to_minutes(
512-
target_datetime.weekday(), target_datetime.hour, target_datetime.minute
513-
)
514-
<= time_to_minutes(*interval.closing_time)
515-
if time_to_minutes(*interval.closing_time) < time_to_minutes(*interval.opening_time)
516-
else time_to_minutes(*interval.opening_time)
517-
<= time_to_minutes(
518-
target_datetime.weekday(), target_datetime.hour, target_datetime.minute
519-
)
520-
<= time_to_minutes(*interval.closing_time)
521-
for interval in self.opening_hours
505+
target_time_as_minutes = time_to_minutes(
506+
target_datetime.weekday(), target_datetime.hour, target_datetime.minute
522507
)
508+
509+
for interval in self.opening_hours:
510+
if (
511+
time_to_minutes(*interval.opening_time)
512+
<= target_time_as_minutes
513+
<= time_to_minutes(*interval.closing_time)
514+
):
515+
return True
516+
517+
return False

tests/test_business.py

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
#
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
import zoneinfo
1920
from datetime import date, datetime
2021

2122
import pytest
22-
import zoneinfo
2323

2424
from telegram import (
2525
BusinessConnection,
@@ -54,9 +54,14 @@ class TestBusinessBase:
5454
location = Location(-23.691288, 46.788279)
5555
opening_minute = 0
5656
closing_minute = 60
57-
time_zone_name = "Country/City"
57+
time_zone_name = "Europe/Berlin"
5858
opening_hours = [
59-
BusinessOpeningHoursInterval(opening, opening + 60) for opening in (0, 24 * 60)
59+
BusinessOpeningHoursInterval(
60+
opening_minute=420, closing_minute=1320
61+
), # Monday 07:00 - 22:00
62+
BusinessOpeningHoursInterval(
63+
opening_minute=1860, closing_minute=2760
64+
), # Tuesday 07:00 - 22:00
6065
]
6166

6267

@@ -114,20 +119,6 @@ def business_opening_hours():
114119
)
115120

116121

117-
@pytest.fixture(scope="module")
118-
def business_hours():
119-
tz_name = "Europe/Berlin"
120-
opening_hours = [
121-
BusinessOpeningHoursInterval(
122-
opening_minute=420, closing_minute=1320
123-
), # Monday 07:00 - 22:00
124-
BusinessOpeningHoursInterval(
125-
opening_minute=1860, closing_minute=2760
126-
), # Tuesday 07:00 - 22:00
127-
]
128-
return BusinessOpeningHours(time_zone_name=tz_name, opening_hours=opening_hours)
129-
130-
131122
class TestBusinessConnectionWithoutRequest(TestBusinessBase):
132123
def test_slots(self, business_connection):
133124
bc = business_connection
@@ -427,49 +418,81 @@ def test_equality(self):
427418
assert hash(boh1) != hash(boh3)
428419

429420
def test_get_opening_hours_for_day_without_timezone(
430-
self, business_hours: BusinessOpeningHours
421+
self, business_opening_hours: BusinessOpeningHours
431422
):
432423
target_date = date(2023, 7, 17) # Monday
433-
expected_tz = zoneinfo.ZoneInfo(business_hours.time_zone_name)
424+
target_date_plus_1 = date(2023, 7, 18) # Tuesday
425+
expected_tz = zoneinfo.ZoneInfo(business_opening_hours.time_zone_name)
434426
expected_opening_time = datetime(2023, 7, 17, 7, 0, tzinfo=expected_tz)
435427
expected_closing_time = datetime(2023, 7, 17, 22, 0, tzinfo=expected_tz)
436428

437-
opening_hours_for_day = business_hours.get_opening_hours_for_day(target_date)
429+
opening_hours_for_day = business_opening_hours.get_opening_hours_for_day(target_date)
438430

439431
assert opening_hours_for_day[0][0] == expected_opening_time
440432
assert opening_hours_for_day[0][1] == expected_closing_time
441433

434+
assert opening_hours_for_day[0][0].date() != target_date_plus_1
435+
assert opening_hours_for_day[0][1].date() != target_date_plus_1
436+
assert opening_hours_for_day[1][0].date() != target_date_plus_1
437+
assert opening_hours_for_day[1][1].date() != target_date_plus_1
438+
442439
def test_get_opening_hours_for_day_with_timezone(
443-
self, business_hours: BusinessOpeningHours
440+
self, business_opening_hours: BusinessOpeningHours
444441
):
445442
target_date = date(2023, 7, 17) # Monday
443+
target_date_plus_1 = date(2023, 7, 18) # Tuesday
446444
test_tz = zoneinfo.ZoneInfo("UTC")
447445
expected_opening_time = datetime(2023, 7, 17, 7, 0, tzinfo=test_tz)
448446
expected_closing_time = datetime(2023, 7, 17, 22, 0, tzinfo=test_tz)
449447

450-
opening_hours_for_day = business_hours.get_opening_hours_for_day(
448+
opening_hours_for_day = business_opening_hours.get_opening_hours_for_day(
451449
target_date, tzinfo=test_tz
452450
)
453451

454452
assert opening_hours_for_day[0][0] == expected_opening_time
455453
assert opening_hours_for_day[0][1] == expected_closing_time
456454

457-
def test_is_open_tznaive_closed(self, business_hours: BusinessOpeningHours):
455+
assert opening_hours_for_day[0][0].date() != target_date_plus_1
456+
assert opening_hours_for_day[0][1].date() != target_date_plus_1
457+
assert opening_hours_for_day[1][0].date() != target_date_plus_1
458+
assert opening_hours_for_day[1][1].date() != target_date_plus_1
459+
460+
def test_is_open_tznaive_closed(self, business_opening_hours: BusinessOpeningHours):
458461
test_datetime = datetime(2023, 7, 19, 12, 0) # Wednesday 12:00, tz naive
459-
assert business_hours.is_open(test_datetime) is False
462+
assert business_opening_hours.is_open(test_datetime) is False
460463

461-
def test_is_open_tznaive_open(self, business_hours: BusinessOpeningHours):
464+
def test_is_open_tznaive_open(self, business_opening_hours: BusinessOpeningHours):
462465
test_datetime = datetime(2023, 7, 17, 12, 0) # Monday 12:00, tz naive
463-
assert business_hours.is_open(test_datetime) is True
466+
assert business_opening_hours.is_open(test_datetime) is True
464467

465-
def test_is_open_tzaware_closed(self, business_hours: BusinessOpeningHours):
468+
def test_is_open_tzaware_closed(self, business_opening_hours: BusinessOpeningHours):
466469
test_datetime = datetime(
467470
2023, 7, 19, 12, 0, tzinfo=zoneinfo.ZoneInfo("UTC")
468471
) # Wednesday 12:00 UTC
469-
assert business_hours.is_open(test_datetime) is False
472+
assert business_opening_hours.is_open(test_datetime) is False
470473

471-
def test_is_open_tzaware_open(self, business_hours: BusinessOpeningHours):
474+
def test_is_open_tzaware_open(self, business_opening_hours: BusinessOpeningHours):
472475
test_datetime = datetime(
473476
2023, 7, 17, 12, 0, tzinfo=zoneinfo.ZoneInfo("UTC")
474477
) # Monday 12:00 UTC
475-
assert business_hours.is_open(test_datetime) is True
478+
assert business_opening_hours.is_open(test_datetime) is True
479+
480+
def test_is_open_tzaware_tzdelta(self, business_opening_hours: BusinessOpeningHours):
481+
test_datetime_utc = datetime(
482+
2023, 7, 17, 6, 0, tzinfo=zoneinfo.ZoneInfo("UTC")
483+
) # Monday 07:00 UTC
484+
test_datetime_berlin = datetime(
485+
2023, 7, 17, 6, 0, tzinfo=zoneinfo.ZoneInfo("Europe/Berlin")
486+
) # Monday 09:00 UTC
487+
488+
assert business_opening_hours.is_open(test_datetime_utc) is True
489+
assert business_opening_hours.is_open(test_datetime_berlin) is False
490+
491+
def test_is_open_tzaware_tzdelta_no_tz(self, business_opening_hours: BusinessOpeningHours):
492+
test_datetime_utc = datetime(
493+
2023, 7, 17, 6, 0, tzinfo=zoneinfo.ZoneInfo("UTC")
494+
) # Monday 07:00 UTC
495+
test_datetime_berlin = datetime(2023, 7, 17, 6, 0) # Monday 09:00 UTC
496+
497+
assert business_opening_hours.is_open(test_datetime_utc) is True
498+
assert business_opening_hours.is_open(test_datetime_berlin) is False

0 commit comments

Comments
 (0)
0