| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from decimal import Decimal |
| 4 |
|
| 5 |
from babel.numbers import ( |
| 6 |
format_compact_currency, |
| 7 |
format_compact_decimal, |
| 8 |
format_currency, |
| 9 |
format_decimal, |
| 10 |
format_percent, |
| 11 |
format_scientific, |
| 12 |
get_currency_name, |
| 13 |
get_territory_currencies, |
| 14 |
parse_decimal, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def test_format_decimal(benchmark, fi_locale): |
| 19 |
number = Decimal("1234567.891") |
| 20 |
assert benchmark(lambda: format_decimal(number, locale=fi_locale)) == "1\xa0234\xa0567,891" |
| 21 |
|
| 22 |
|
| 23 |
def test_format_currency(benchmark, fi_locale): |
| 24 |
number = Decimal("1234.5") |
| 25 |
assert ( |
| 26 |
benchmark(lambda: format_currency(number, "EUR", locale=fi_locale)) |
| 27 |
== "1\xa0234,50\xa0€" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
def test_format_currency_name(benchmark): |
| 32 |
# The "name" format type additionally does a plural-form currency name lookup. |
| 33 |
number = Decimal("1234.5") |
| 34 |
assert ( |
| 35 |
benchmark(lambda: format_currency(number, "EUR", locale="fi", format_type="name")) |
| 36 |
== "1\xa0234,50 euroa" |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def test_format_compact_decimal(benchmark): |
| 41 |
assert ( |
| 42 |
benchmark( |
| 43 |
lambda: format_compact_decimal( |
| 44 |
1234567, |
| 45 |
format_type="long", |
| 46 |
locale="fi", |
| 47 |
fraction_digits=2, |
| 48 |
), |
| 49 |
) |
| 50 |
== "1,23 miljoonaa" |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
def test_format_compact_currency(benchmark): |
| 55 |
assert ( |
| 56 |
benchmark( |
| 57 |
lambda: format_compact_currency(123456789, "EUR", locale="fi", fraction_digits=1), |
| 58 |
) |
| 59 |
== "123,5\xa0milj.\xa0€" |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
def test_format_percent(benchmark): |
| 64 |
number = Decimal("0.3456") |
| 65 |
assert benchmark(lambda: format_percent(number, locale="fi")) == "35\xa0%" |
| 66 |
|
| 67 |
|
| 68 |
def test_format_scientific(benchmark): |
| 69 |
number = Decimal("1234567.891") |
| 70 |
assert benchmark(lambda: format_scientific(number, locale="fi")) == "1,234567891E6" |
| 71 |
|
| 72 |
|
| 73 |
def test_parse_decimal(benchmark, fi_locale): |
| 74 |
string = "1\xa0234\xa0567,891" |
| 75 |
assert benchmark(lambda: parse_decimal(string, locale=fi_locale, strict=True)) == Decimal( |
| 76 |
"1234567.891", |
| 77 |
) |
| 78 |
|
| 79 |
|
| 80 |
def test_get_currency_name(benchmark): |
| 81 |
assert benchmark(lambda: get_currency_name("EUR", count=2, locale="fi")) == "euroa" |
| 82 |
|
| 83 |
|
| 84 |
def test_get_territory_currencies(benchmark): |
| 85 |
assert benchmark(lambda: get_territory_currencies("FI")) == ["EUR"] |