Debian Codegraph @

tests/test_smoke.py

raw
1 """
2 These tests do not verify any results and should not be run when
3 looking at improving test coverage. They just verify that basic
4 operations don't fail due to odd corner cases on any locale that
5 we ship.
6 """
7 import datetime
8 import decimal
9
10 import pytest
11
12 from babel import Locale, dates, numbers, units
13
14 NUMBERS = (
15 decimal.Decimal("-33.76"), # Negative Decimal
16 decimal.Decimal("13.37"), # Positive Decimal
17 1.2 - 1.0, # Inaccurate float
18 10, # Plain old integer
19 0, # Zero
20 1000, # A thousand (previously raised KeyError in the nl locale for compact currencies)
21 )
22
23
24 @pytest.mark.all_locales
25 def test_smoke_dates(locale):
26 locale = Locale.parse(locale)
27 instant = datetime.datetime.now()
28 for width in ("full", "long", "medium", "short"):
29 assert dates.format_date(instant, format=width, locale=locale)
30 assert dates.format_datetime(instant, format=width, locale=locale)
31 assert dates.format_time(instant, format=width, locale=locale)
32 # Interval test
33 past = instant - datetime.timedelta(hours=23)
34 assert dates.format_interval(past, instant, locale=locale)
35 # Duration test - at the time of writing, all locales seem to have `short` width,
36 # so let's test that.
37 duration = instant - instant.replace(hour=0, minute=0, second=0)
38 for granularity in ('second', 'minute', 'hour', 'day'):
39 assert dates.format_timedelta(duration, granularity=granularity, format="short", locale=locale)
40
41
42 @pytest.mark.all_locales
43 def test_smoke_numbers(locale):
44 locale = Locale.parse(locale)
45 for number in NUMBERS:
46 assert numbers.format_decimal(number, locale=locale)
47 assert numbers.format_decimal(number, locale=locale, numbering_system="default")
48 assert numbers.format_currency(number, "EUR", locale=locale)
49 assert numbers.format_currency(number, "EUR", locale=locale, numbering_system="default")
50 assert numbers.format_compact_currency(number, "EUR", locale=locale)
51 assert numbers.format_compact_currency(number, "EUR", locale=locale, numbering_system="default")
52 assert numbers.format_scientific(number, locale=locale)
53 assert numbers.format_scientific(number, locale=locale, numbering_system="default")
54 assert numbers.format_percent(number / 100, locale=locale)
55 assert numbers.format_percent(number / 100, locale=locale, numbering_system="default")
56
57
58 @pytest.mark.all_locales
59 def test_smoke_units(locale):
60 locale = Locale.parse(locale)
61 for unit in ('length-meter', 'mass-kilogram', 'energy-calorie', 'volume-liter'):
62 for number in NUMBERS:
63 assert units.format_unit(number, measurement_unit=unit, locale=locale)
64 assert units.format_unit(number, measurement_unit=unit, locale=locale, numbering_system="default")