| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import datetime |
| 4 |
|
| 5 |
from babel import Locale |
| 6 |
from babel.dates import ( |
| 7 |
_cached_parse_pattern, |
| 8 |
format_date, |
| 9 |
format_datetime, |
| 10 |
format_interval, |
| 11 |
format_skeleton, |
| 12 |
format_time, |
| 13 |
format_timedelta, |
| 14 |
get_timezone_name, |
| 15 |
parse_date, |
| 16 |
parse_pattern, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
def test_format_date_standalone_month(benchmark, fi_locale): |
| 21 |
d = datetime.date(2025, 10, 15) |
| 22 |
assert benchmark(lambda: format_date(d, "LLLL", fi_locale)) == "lokakuu" |
| 23 |
|
| 24 |
|
| 25 |
def test_locale_deep_data_read(benchmark): |
| 26 |
# Repeated alias-resolving deep reads through a long-lived Locale. |
| 27 |
locale = Locale.parse("fi") |
| 28 |
assert locale.months["stand-alone"]["wide"][10] == "lokakuu" |
| 29 |
benchmark(lambda: locale.months["stand-alone"]["wide"][10]) |
| 30 |
|
| 31 |
|
| 32 |
def test_format_datetime_medium(benchmark, helsinki_tz, fi_locale): |
| 33 |
dt = datetime.datetime(2025, 10, 15, 13, 45, 30) |
| 34 |
assert benchmark(lambda: format_datetime(dt, locale=fi_locale)) == "15.10.2025 13.45.30" |
| 35 |
|
| 36 |
|
| 37 |
def test_format_time_with_tzinfo(benchmark, fi_locale, helsinki_tz): |
| 38 |
# Aware datetime, converted into another zone on the way out. |
| 39 |
dt = datetime.datetime(2025, 10, 15, 13, 45, 30, tzinfo=datetime.timezone.utc) |
| 40 |
assert ( |
| 41 |
benchmark(lambda: format_time(dt, locale=fi_locale, tzinfo=helsinki_tz)) == "16.45.30" |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
def test_format_timedelta_long(benchmark, fi_locale): |
| 46 |
delta = datetime.timedelta(days=3, hours=5) |
| 47 |
expected = "3 päivää" |
| 48 |
assert format_timedelta(delta, locale=fi_locale) == expected |
| 49 |
assert benchmark(lambda: format_timedelta(delta, locale=fi_locale)) == expected |
| 50 |
|
| 51 |
|
| 52 |
def test_format_timedelta_short_hours(benchmark, fi_locale): |
| 53 |
delta = datetime.timedelta(days=3, hours=5) |
| 54 |
assert ( |
| 55 |
benchmark( |
| 56 |
lambda: format_timedelta( |
| 57 |
delta, |
| 58 |
granularity="hour", |
| 59 |
format="short", |
| 60 |
locale=fi_locale, |
| 61 |
), |
| 62 |
) |
| 63 |
== "3 pv" |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
def test_format_skeleton(benchmark, fi_locale): |
| 68 |
dt = datetime.datetime(2025, 10, 15, 13, 45, 30) |
| 69 |
assert benchmark(lambda: format_skeleton("yMMMd", dt, locale=fi_locale)) == "15.10.2025" |
| 70 |
|
| 71 |
|
| 72 |
def test_format_skeleton_fuzzy(benchmark, fi_locale): |
| 73 |
# "EyMMMd" is not in the locale's skeleton table, so match_skeleton() has to run. |
| 74 |
dt = datetime.datetime(2025, 10, 15, 13, 45, 30) |
| 75 |
assert ( |
| 76 |
benchmark(lambda: format_skeleton("EyMMMd", dt, fuzzy=True, locale=fi_locale)) |
| 77 |
== "ke 15.10.2025" |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
def test_format_interval_same_day(benchmark, fi_locale): |
| 82 |
start = datetime.datetime(2025, 10, 15, 9, 0) |
| 83 |
end = datetime.datetime(2025, 10, 15, 17, 30) |
| 84 |
assert ( |
| 85 |
benchmark(lambda: format_interval(start, end, "Hm", locale=fi_locale)) == "9.00–17.30" |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
def test_parse_pattern_cached(benchmark): |
| 90 |
assert benchmark(lambda: parse_pattern("MMM d, yyyy").format) == "%(MMM)s %(d)s, %(yyyy)s" |
| 91 |
|
| 92 |
|
| 93 |
def test_parse_pattern_uncached(benchmark): |
| 94 |
# Bypass the lru_cache to measure actual tokenization + parsing. |
| 95 |
parse = _cached_parse_pattern.__wrapped__ |
| 96 |
assert benchmark(lambda: parse("MMM d, yyyy").format) == "%(MMM)s %(d)s, %(yyyy)s" |
| 97 |
|
| 98 |
|
| 99 |
def test_parse_date_short(benchmark, fi_locale): |
| 100 |
assert benchmark(lambda: parse_date("15.10.2025", locale=fi_locale)) == datetime.date( |
| 101 |
2025, |
| 102 |
10, |
| 103 |
15, |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
def test_get_timezone_name(benchmark, fi_locale, helsinki_tz): |
| 108 |
assert ( |
| 109 |
benchmark(lambda: get_timezone_name(helsinki_tz, locale=fi_locale)) |
| 110 |
== "Itä-Euroopan aika" |
| 111 |
) |