|
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 | +import zoneinfo |
19 | 20 | from datetime import date, datetime
|
20 | 21 |
|
21 | 22 | import pytest
|
22 |
| -import zoneinfo |
23 | 23 |
|
24 | 24 | from telegram import (
|
25 | 25 | BusinessConnection,
|
@@ -54,9 +54,14 @@ class TestBusinessBase:
|
54 | 54 | location = Location(-23.691288, 46.788279)
|
55 | 55 | opening_minute = 0
|
56 | 56 | closing_minute = 60
|
57 |
| - time_zone_name = "Country/City" |
| 57 | + time_zone_name = "Europe/Berlin" |
58 | 58 | 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 |
60 | 65 | ]
|
61 | 66 |
|
62 | 67 |
|
@@ -114,20 +119,6 @@ def business_opening_hours():
|
114 | 119 | )
|
115 | 120 |
|
116 | 121 |
|
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 |
| - |
131 | 122 | class TestBusinessConnectionWithoutRequest(TestBusinessBase):
|
132 | 123 | def test_slots(self, business_connection):
|
133 | 124 | bc = business_connection
|
@@ -427,49 +418,81 @@ def test_equality(self):
|
427 | 418 | assert hash(boh1) != hash(boh3)
|
428 | 419 |
|
429 | 420 | def test_get_opening_hours_for_day_without_timezone(
|
430 |
| - self, business_hours: BusinessOpeningHours |
| 421 | + self, business_opening_hours: BusinessOpeningHours |
431 | 422 | ):
|
432 | 423 | 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) |
434 | 426 | expected_opening_time = datetime(2023, 7, 17, 7, 0, tzinfo=expected_tz)
|
435 | 427 | expected_closing_time = datetime(2023, 7, 17, 22, 0, tzinfo=expected_tz)
|
436 | 428 |
|
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) |
438 | 430 |
|
439 | 431 | assert opening_hours_for_day[0][0] == expected_opening_time
|
440 | 432 | assert opening_hours_for_day[0][1] == expected_closing_time
|
441 | 433 |
|
| 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 | + |
442 | 439 | def test_get_opening_hours_for_day_with_timezone(
|
443 |
| - self, business_hours: BusinessOpeningHours |
| 440 | + self, business_opening_hours: BusinessOpeningHours |
444 | 441 | ):
|
445 | 442 | target_date = date(2023, 7, 17) # Monday
|
| 443 | + target_date_plus_1 = date(2023, 7, 18) # Tuesday |
446 | 444 | test_tz = zoneinfo.ZoneInfo("UTC")
|
447 | 445 | expected_opening_time = datetime(2023, 7, 17, 7, 0, tzinfo=test_tz)
|
448 | 446 | expected_closing_time = datetime(2023, 7, 17, 22, 0, tzinfo=test_tz)
|
449 | 447 |
|
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( |
451 | 449 | target_date, tzinfo=test_tz
|
452 | 450 | )
|
453 | 451 |
|
454 | 452 | assert opening_hours_for_day[0][0] == expected_opening_time
|
455 | 453 | assert opening_hours_for_day[0][1] == expected_closing_time
|
456 | 454 |
|
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): |
458 | 461 | 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 |
460 | 463 |
|
461 |
| - def test_is_open_tznaive_open(self, business_hours: BusinessOpeningHours): |
| 464 | + def test_is_open_tznaive_open(self, business_opening_hours: BusinessOpeningHours): |
462 | 465 | 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 |
464 | 467 |
|
465 |
| - def test_is_open_tzaware_closed(self, business_hours: BusinessOpeningHours): |
| 468 | + def test_is_open_tzaware_closed(self, business_opening_hours: BusinessOpeningHours): |
466 | 469 | test_datetime = datetime(
|
467 | 470 | 2023, 7, 19, 12, 0, tzinfo=zoneinfo.ZoneInfo("UTC")
|
468 | 471 | ) # 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 |
470 | 473 |
|
471 |
| - def test_is_open_tzaware_open(self, business_hours: BusinessOpeningHours): |
| 474 | + def test_is_open_tzaware_open(self, business_opening_hours: BusinessOpeningHours): |
472 | 475 | test_datetime = datetime(
|
473 | 476 | 2023, 7, 17, 12, 0, tzinfo=zoneinfo.ZoneInfo("UTC")
|
474 | 477 | ) # 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