|
16 | 16 | #
|
17 | 17 | # You should have received a copy of the GNU Lesser Public License
|
18 | 18 | # along with this program. If not, see [http://www.gnu.org/licenses/].
|
19 |
| -from datetime import datetime |
| 19 | +from datetime import date, datetime |
20 | 20 |
|
21 | 21 | import pytest
|
| 22 | +import zoneinfo |
22 | 23 |
|
23 | 24 | from telegram import (
|
24 | 25 | BusinessConnection,
|
|
35 | 36 | from telegram._utils.datetime import UTC, to_timestamp
|
36 | 37 | from tests.auxil.slots import mro_slots
|
37 | 38 |
10000
|
38 |
| -try: |
39 |
| - import zoneinfo |
40 |
| -except ImportError: |
41 |
| - from backports import zoneinfo |
42 |
| - |
43 | 39 |
|
44 | 40 | class TestBusinessBase:
|
45 | 41 | id_ = "123"
|
@@ -118,6 +114,20 @@ def business_opening_hours():
|
118 | 114 | )
|
119 | 115 |
|
120 | 116 |
|
| 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 | + |
121 | 131 | class TestBusinessConnectionWithoutRequest(TestBusinessBase):
|
122 | 132 | def test_slots(self, business_connection):
|
123 | 133 | bc = business_connection
|
@@ -415,74 +425,51 @@ def test_equality(self):
|
415 | 425 |
|
416 | 426 | assert boh1 != boh3
|
417 | 427 | assert hash(boh1) != hash(boh3)
|
418 |
| - |
419 |
| - # def test_get_opening_hours_for_day_success_without_timezone(self): |
420 |
| - # boh = BusinessOpeningHours("Europe/Berlin", [ |
421 |
| - # BusinessOpeningHoursInterval(0, 60), |
422 |
| - # BusinessOpeningHoursInterval(60, 120), |
423 |
| - # BusinessOpeningHoursInterval(120, 180), |
424 |
| - # BusinessOpeningHoursInterval(180, 240), |
425 |
| - # BusinessOpeningHoursInterval(240, 300), |
426 |
| - # BusinessOpeningHoursInterval(300, 360), |
427 |
| - # BusinessOpeningHoursInterval(360, 420), |
428 |
| - # ]) |
429 |
| - |
430 |
| - # assert boh.get_opening_hours_for_day(datetime(2021, 1, 1, 0, 0, 0)) == (0, 60) |
431 |
| - |
432 |
| - # def test_get_opening_hours_for_day_success_with_timezone(self): |
433 |
| - # boh = BusinessOpeningHours("Europe/Berlin", [ |
434 |
| - # BusinessOpeningHoursInterval(0, 60), |
435 |
| - # BusinessOpeningHoursInterval(60, 120), |
436 |
| - # BusinessOpeningHoursInterval(120, 180), |
437 |
| - # BusinessOpeningHoursInterval(180, 240), |
438 |
| - # BusinessOpeningHoursInterval(240, 300), |
439 |
| - # BusinessOpeningHoursInterval(300, 360), |
440 |
| - # BusinessOpeningHoursInterval(360, 420), |
441 |
| - # ]) |
442 |
| - |
443 |
| - # assert boh.get_opening_hours_for_day(datetime(2021, 1, 1, 0, 0, 0), zoneinfo.ZoneInfo("Europe/Berlin")) == (0, 60) |
444 |
| - |
445 |
| - def test_is_open_tznaive_closed(self): |
446 |
| - boh = BusinessOpeningHours("Europe/Berlin", [ |
447 |
| - BusinessOpeningHoursInterval(0, 6969), |
448 |
| - ]) |
449 |
| - |
450 |
| - # (0, 0, 0) -> (4, 20, 9) |
451 |
| - assert boh.is_open(datetime(2024, 6, 29, 0, 0, 0)) == False |
452 |
| - assert boh.is_open(datetime(2024, 6, 28, 21, 0, 0)) == False |
453 |
| - assert boh.is_open(datetime(2024, 6, 28, 20, 10, 0)) == False |
454 |
| - |
455 |
| - def test_is_open_tznaive_open(self): |
456 |
| - boh = BusinessOpeningHours("Europe/Berlin", [ |
457 |
| - BusinessOpeningHoursInterval(0, 6969), |
458 |
| - ]) |
459 |
| - |
460 |
| - # (0, 0, 0) -> (4, 20, 9) |
461 |
| - assert boh.is_open(datetime(2024, 6, 24, 0, 0, 0)) == True |
462 |
| - assert boh.is_open(datetime(2024, 6, 28, 10, 0, 0)) == True |
463 |
| - assert boh.is_open(datetime(2024, 6, 25, 20, 8, 0)) == True |
464 |
| - |
465 |
| - def test_is_open_tzaware_closed(self): |
466 |
| - boh = BusinessOpeningHours("Europe/Berlin", [ |
467 |
| - BusinessOpeningHoursInterval(0, 6969), |
468 |
| - ]) |
469 |
| - |
470 |
| - # London is 2 hours behind of Berlin |
471 |
| - tz = zoneinfo.ZoneInfo("Europe/London") |
472 |
| - |
473 |
| - # (0, 0, 0) -> (4, 20, 9) |
474 |
| - assert boh.is_open(datetime(2024, 6, 23, 23, 0, 0, tzinfo=tz)) == True |
475 |
| - assert boh.is_open(datetime(2024, 6, 28, 10, 0, 0, tzinfo=tz)) == True |
476 |
| - |
477 |
| - def test_is_open_tzaware_open(self): |
478 |
| - boh = BusinessOpeningHours("Europe/Berlin", [ |
479 |
| - BusinessOpeningHoursInterval(0, 6969), |
480 |
| - ]) |
481 |
| - |
482 |
| - # London is 2 hours behind of Berlin |
483 |
| - tz = zoneinfo.ZoneInfo("Europe/London") |
484 |
| - |
485 |
| - # (0, 0, 0) -> (4, 20, 9) |
486 |
| - assert boh.is_open(datetime(2024, 6, 24, 2, 0, 0, tzinfo=tz)) == True |
487 |
| - assert boh.is_open(datetime(2024, 6, 28, 12, 0, 0, tzinfo=tz)) == True |
488 |
| - assert boh.is_open(datetime(2024, 6, 25, 21, 8, 0, tzinfo=tz)) == True |
| 428 | + |
| 429 | + def test_get_opening_hours_for_day_success_without_timezone( |
| 430 | + self, business_hours: BusinessOpeningHours |
| 431 | + ): |
| 432 | + target_date = date(2023, 7, 17) # Monday |
| 433 | + expected_tz = zoneinfo.ZoneInfo(business_hours.time_zone_name) |
| 434 | + expected_opening_time = datetime(2023, 7, 17, 7, 0, tzinfo=expected_tz) |
| 435 | + expected_closing_time = datetime(2023, 7, 17, 22, 0, tzinfo=expected_tz) |
| 436 | + |
| 437 | + opening_hours_for_day = business_hours.get_opening_hours_for_day(target_date) |
| 438 | + |
| 439 | + assert opening_hours_for_day[0][0] == expected_opening_time |
| 440 | + assert opening_hours_for_day[0][1] == expected_closing_time |
| 441 | + |
| 442 | + def test_get_opening_hours_for_day_success_with_timezone( |
| 443 | + self, business_hours: BusinessOpeningHours |
| 444 | + ): |
| 445 | + target_date = date(2023, 7, 17) # Monday |
| 446 | + test_tz = zoneinfo.ZoneInfo("UTC") |
| 447 | + expected_opening_time = datetime(2023, 7, 17, 7, 0, tzinfo=test_tz) |
| 448 | + expected_closing_time = datetime(2023, 7, 17, 22, 0, tzinfo=test_tz) |
| 449 | + |
| 450 | + opening_hours_for_day = business_hours.get_opening_hours_for_day( |
| 451 | + target_date, tzinfo=test_tz |
| 452 | + ) |
| 453 | + |
| 454 | + assert opening_hours_for_day[0][0] == expected_opening_time |
| 455 | + assert opening_hours_for_day[0][1] == expected_closing_time |
| 456 | + |
| 457 | + def test_is_open_tznaive_closed(self, business_hours: BusinessOpeningHours): |
| 458 | + test_datetime = datetime(2023, 7, 19, 12, 0) # Wednesday 12:00, tz naive |
| 459 | + assert business_hours.is_open(test_datetime) is False |
| 460 | + |
| 461 | + def test_is_open_tznaive_open(self, business_hours: BusinessOpeningHours): |
| 462 | + test_datetime = datetime(2023, 7, 17, 12, 0) # Monday 12:00, tz naive |
| 463 | + assert business_hours.is_open(test_datetime) is True |
| 464 | + |
| 465 | + def test_is_open_tzaware_closed(self, business_hours: BusinessOpeningHours): |
| 466 | + test_datetime = datetime( |
| 467 | + 2023, 7, 19, 12, 0, tzinfo=zoneinfo.ZoneInfo("UTC") |
| 468 | + ) # Wednesday 12:00 UTC |
| 469 | + assert business_hours.is_open(test_datetime) is False |
| 470 | + |
| 471 | + def test_is_open_tzaware_open(self, business_hours: BusinessOpeningHours): |
| 472 | + test_datetime = datetime( |
| 473 | + 2023, 7, 17, 12, 0, tzinfo=zoneinfo.ZoneInfo("UTC") |
| 474 | + ) # Monday 12:00 UTC |
| 475 | + assert business_hours.is_open(test_datetime) is True |
0 commit comments